0

I am new to PHP, so please be patient if I am making a silly mistake. My problem is that if 2 users open up two of more conversations, my messaging system is displaying the conversation from the first subject in the conversation with the second subject. I am not allowed to post an image, so I'll try to type out an explanation: Right now I am getting results looking something like this:

Subject1: Hello - Hey, how are you today? - I'm fine, thanks!


Subject2: Goodbye - Hey, how are you today? - I'm fine, thanks! - It was great talking to you! - Take care!


In the example above, you can see the message from the first conversation is also being displayed in the second conversation.

I want the conversation from each subject to only be displayed in its appropriate subject. I am trying to get only the last subject in each loop to use in my MySQL query, but when I take the last array value from a foreach loop, it still returns the previous subjects in MySQL. I'm sorry if my request or my code is hard to understand. I've been working on this nonstop for days and have been getting nowhere closer to my goal. Please help!

Here is a snippet of my code:

$sql = mysql_query("SELECT * FROM private_messages WHERE to_id='$my_id' AND         recipientDelete='0' GROUP BY subject ORDER BY id DESC LIMIT 200");

$resultArrays2 = array();
while($row = mysql_fetch_array($sql)){
$resultArrays2 = array( 
$subject = $row['subject']);

foreach ($resultArrays2 as $value2 => $i2){
//foreach ($i as $second){
$array2[] = $i2;
    }

$my_uname = $row['message'];
$date = strftime("%b %d, %Y",strtotime($row['time_sent']));
if($row['opened'] == "0"){ 
        $textWeight = 'msgDefault';
} else {
        $textWeight = 'msgRead';
}
$fr_id = $row['from_id'];
$stringsubfirst = (end($array2));    
// SQL - Collect username for sender inside loop
$more = mysql_query("SELECT id, username FROM mymembers WHERE id='$p' LIMIT 1");
while($rz = mysql_fetch_array($more)){ $MEid = $rz['id']; $ME = $rz['username']; }
$ret = mysql_query("SELECT id, username FROM mymembers WHERE id='$fr_id' LIMIT 1");
while($raw = mysql_fetch_array($ret)){ $Sid = $raw['id']; $Sname = $raw['username']; }

$reachingsub = mysql_query("SELECT * FROM private_messages WHERE     subject='$stringsubfirst' AND recipientDelete !='1' LIMIT 100") or die(mysql_error());
$resultArrays = array();
while($rn = mysql_fetch_array($reachingsub)){
$resultArrays = array(
$finalsub = $rn["subject"]);    
foreach ($resultArrays as $value => $i){

$array[] = $i;
    }
}
$stringsub = (end($array));
$stringcur = (current($array));
$stringnext = (next($array));
$stringprev = (prev($array));
$stringfirst = (reset($array));



$reaching = mysql_query("SELECT * FROM private_messages WHERE subject='$stringsub' AND recipientDelete !='1' ORDER BY id DESC LIMIT 200") or die(mysql_error());
    $resultArrays2 = array();
while($rw = mysql_fetch_array($reaching)){
    $resultArrays2 = array(
    $subsub = $rw["subject"]);
        foreach ($resultArrays2 as $value2 => $i2){

        $array2[] = $i2;
        $array3[] = $value2;
        }
        $stringsub2 = (end($array2));
        $stringcur2 = (current($array2));
        $stringnext2 = (next($array2));
        $stringprev2 = (prev($array2));
        $stringfirst2 = (reset($array2));
    $subsub2 = $rw["subject"];
    $usingid = $rw["id"];
    $insidemessage = $rw["message"];
    $fi = $rw["from_id"];
    $ti = $rw["to_id"];
    $minutes = strftime("%b %d, %r",strtotime($rw['time_sent']));
    if($p !== $ti){$listname = $ME;}else{$listname = $Sname;}

    if($subject !== $stringsub2 || $stringcur2 !== $stringnext2){$othermessage .= '<div  style="display:none;">'.$ti.$subsub2.$subject.$stringsub2.$stringsubfirst.'</div>';}

    if($subject == $stringsub2 || $stringcur2 == $stringnext2){$othermessage .= '<br />'.'<div style="display:none;">'.$ti.$subsub2.$subject.$stringsub2.$stringsubfirst.'</div>'.
    '<div style="font-weight:bold;">'.$listname.': '.$minutes.'</div>'.$insidemessage.'<br />';}
}
1
  • 2
    This would be a lot easier to debug if you indented the code properly. As it is, it's very hard to see what lines are called from where. Commented Sep 16, 2012 at 15:07

1 Answer 1

2

I'm not sure if I understood your whole problem correctly but... a very simple solution would be to give each conversation a unique identifier and do the select using that instead of the subject which is probably not unique

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.