我是新手PHP
,所以如果我犯了一个愚蠢的错误,请耐心等待。我的问题是,如果 2 个用户打开了两个以上的对话,我的消息系统将显示来自第一个主题与第二个主题的对话。我不允许发布图片,所以我会尝试输入解释:现在我得到的结果看起来像这样:
对象1:你好-嘿,你今天好吗?- 我很好,谢谢!
对象2:再见——嘿,你今天好吗?- 我很好,谢谢!- 和你聊天真是太好了!- 小心!
在上面的示例中,您可以看到来自第一个对话的消息也显示在第二个对话中。
我希望每个主题的对话只显示在其适当的主题中。我试图只获取每个循环中的最后一个主题以在我的MySQL
查询中使用,但是当我从 foreach 循环中获取最后一个数组值时,它仍然返回MySQL
. 如果我的请求或我的代码难以理解,我很抱歉。几天来,我一直在不停地工作,但离我的目标还很遥远。请帮忙!
这是我的代码片段:
$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 />';}
}