我是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 />';}
}
答案 0 :(得分:2)
我不确定我是否正确地理解了你的整个问题,但是...一个非常简单的解决方案是给每个对话一个唯一的标识符并使用那个而不是可能不是唯一的主题进行选择