我是php的新手,所以如果我的问题不明确,请向我道歉。
我在mysql中设置了一个消息表。我能够回复发送给用户的send_by和sent_消息及其发送时间以及如何按时间排序。 我的问题是:如何按时间以sent_by和sent_to顺序输出消息,但是像这样
user1: sent 2011-02-12 13:34:26
hi, how are you
user2: sent 2011-02-12 13:37:26
who is this?
user1: sent 2011-02-12 13:38:26
its me
user1: sent 2011-02-12 13:38:50
from the bar
user1: sent 2011-02-12 13:40:26
the one in the city
user2: sent 2011-02-12 13:45:26
Oh hi
我希望显示user2以红色发送的所有内容以及user1以黑色发送的所有内容。我怎么能这样做?
这就是我所做的,但我现在想要的方式不是这样的。
$tomsg = mysql_query("SELECT * FROM msg WHERE sent_to = 'user1' ");
$inboxmsg_count = mysql_num_rows($tomsg );
if ($inboxmsg_count>0)
{
echo "messages between You and name<br/>";
echo 'total message ('.$inboxmsg_count.')<hr/><br/>';
while ($msg = mysql_fetch_array($tomsg )){
$cmsg = $msg ['cmsg'];
$time_sent = $msg ['time_sent'];
$subj = $msg ['subj'];
echo '<div style="background-color:gray; width:542px; height:auto; padding-left:10px; padding-right:10px"><span style="float:right;">' .$time_sent.'</span><br/><b>' .$subj.'</b><br/> '.$cmsg.'<hr/></div>';
}
$frommsg = mysql_query("SELECT * FROM msg WHERE sent_by = 'user1' ");
if($inboxmsg_count>0)
{
while ($msg = mysql_fetch_array($frommsg )){
$cmsg = $msg ['cmsg'];
$time_sent = $msg ['time_sent'];
$subj = $msg ['subj'];
echo '<div style="background-color:blue; width:542px; height:auto; padding-left:10px; padding-right:10px"><span style="float:right;">' .$time_sent.'</span><br/><b>' .$subj.'</b><br/> '.$cmsg.'<hr/></div>';
}
}
}
else {echo "you have no messages y}
答案 0 :(得分:1)
无需进行两次查询,只需执行类似
的操作即可SELECT username, the, other, fields, you need
FROM messagestable
ORDER BY timesent
在您的PHP代码中,您可以
while($row = msyql_fetch_assoc(...)) {
... pick a color based on $row['username'] ...
... display the message using that color ...
}