我今天有一个相当困难的问题而且我不知道是否可以这样做,但是如果是的话它会帮助我。这是我的代码:
$chatquery = mysql_query("SELECT * FROM conversation
WHERE yourusername='$username' AND
theirusername='$otherchatuser'
ORDER BY ID DESC");
while ($runrows = mysql_fetch_assoc($chatquery))
{
$chatname = $runrows['realuserusername'];
$chatmessage = nl2br($runrows['message']);
$chatmessage = preg_replace('/(((f|ht){1}tp:\/\/)[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/i',
'<a href="\\1" style="color:#36648B;" target="_blank">\\1</a>',
$chatmessage);
$chatmessage = preg_replace('/([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/i',
'\\1<a href="http://\\2" style="color:#36648B;" target="_blank">\\2</a>',
$chatmessage);
$chatlocation = $runrows['imagelocation'];
$chatimage = "<img src='$chatlocation' style='width:25px; height:20px;' />";
echo '<table style="margin-bottom:5px;">
<tr>
<td valign="top" style="padding-right:5px;">'.$chatimage.'</td>
<td><div style="max-width:220px; word-wrap:break-word;">'.$chatmessage.'<div></td>
</tr>
</table>';
}
这是一个聊天插件,现在所有消息都以DESC
顺序显示,这就是我想要的。但是,我想要做的是,如果我的查询连续多次获得相同的用户$chatname
,我希望它以ASC
顺序显示这些消息而不显示{{1再次。然后,一旦获得不同的$chatname
,它就会继续按$chatname
顺序排列。所以这就像是颠倒了一个查询。我不知道是否可能,但任何帮助都会受到赞赏。
我的最后一个问题,基于DaveRandom的反应并使用他下面的代码。在重复用户名的地方,我想要代码
DESC
如果没有,我想拥有
$usermessage = '<table><tr><td valign="top" style="padding-right:5px;"></td><td><div style="max-width:220px; word-wrap:break-word;">'.$chatmessage.'<div></td></tr></table>';
我将如何做到这一点?
答案 0 :(得分:1)
我认为为了做到这一点,你必须循环数据两次,一次建立订单,一次输出信息。例如:
function make_row_output ($row, $isRepeated = FALSE) {
$chatname = $row['realuserusername'];
$chatmessage = preg_replace(
array(
'/(((f|ht){1}tp:\/\/)[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/i',
'/([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/i'
),
array(
'<a href="\\1" style="color:#36648B;" target="_blank">\\1</a>',
'\\1<a href="http://\\2" style="color:#36648B;" target="_blank">\\2</a>'
),
nl2br($row['message'])
);
$chatlocation = $row['imagelocation'];
$chatimage = "<img src='$chatlocation' style='width:25px; height:20px;' />";
return ($isRepeated)
? '<table><tr><td valign="top" style="padding-right:5px;"></td><td><div style="max-width:220px; word-wrap:break-word;">'.$chatmessage.'<div></td></tr></table>'
: '<table style="margin-bottom:5px;"><tr><td valign="top" style="padding-right:5px;">'.$chatimage.'</td><td><div style="max-width:220px; word-wrap:break-word;">'.$chatmessage.'<div></td></tr></table>';
}
// Do the query
$query = "SELECT *
FROM conversation
WHERE yourusername = '$username'
AND theirusername = '$otherchatuser'
ORDER BY ID DESC"
$result = mysql_query($query);
// Loop the data and build an ordered array
$last = FALSE;
$rows = $userRows = array();
while ($row = mysql_fetch_assoc($result)) {
if ($last !== FALSE && $row['realuserusername'] != $last) {
$userRows = array_reverse($userRows);
echo make_row_output(array_shift($userRows));
foreach ($userRows as $row2) {
echo make_row_output($row2, TRUE);
}
$userRows = array($row);
} else {
$userRows[] = $row;
}
$last = $row['realuserusername'];
}
// Last loop to catch any entries still in $userrows
$userRows = array_reverse($userRows);
echo make_row_output(array_shift($userRows));
foreach ($userRows as $row2) {
echo make_row_output($row2, TRUE);
}