简体   繁体   English

首先通过最新对话订购MySQL收件箱?

[英]Order MySQL inbox by newest conversation first?

I've a MySQL/PHP code for a private messaging system that I built. 我为自己构建的专用消息传递系统提供了MySQL / PHP代码。 It works great although I'm fairly new to it all so struggling to get the message threads to display by newest first. 尽管我对它还很陌生,但它却很难使消息线程显示在最新的位置,因此效果很好。 Is there any chance you could offer advice? 您有机会提供建议吗? The current code is as follows: 当前代码如下:

$result = '';

$nowTime = time();

$getmessages = mysql_query("SELECT * FROM messages WHERE msg_to = '$session_memberid' GROUP BY msg_from ORDER BY ID DESC");
while($iamessages = mysql_fetch_array($getmessages))
{
$msg_id = $iamessages['ID'];
$msg_from = $iamessages['msg_from'];
$msg_conversation = $iamessages['conversation'];



$getmsgdata = mysql_query("SELECT * FROM messages WHERE msg_to = '$session_memberid' AND msg_from = '$msg_from' ORDER BY ID DESC LIMIT 1");
while($imsd = mysql_fetch_array($getmsgdata))
{
$msg_message = $imsd['msg_message'];
$msg_time = $imsd['time'];
$msg_read = $imsd['msg_read'];
}

$msg_conversation = suitcrypt($msg_conversation);

if ( $msg_read == 'no' ) { $msgclass = "messagepostunread"; } else { $msgclass = "messagepost"; }

$getfromdata = mysql_query("SELECT FullName, Username, status FROM members WHERE ID = '$msg_from'");
while($ifd = mysql_fetch_array($getfromdata))
{
$msg_from_name = $ifd['FullName'];
$msg_from_username = $ifd['Username'];
$msg_from_status = $ifd['status'];
}

$getfromdata1 = mysql_query("SELECT Link FROM images WHERE MemberID = '$msg_from' AND is_primary = 'yes'");
while($ifd1 = mysql_fetch_array($getfromdata1)) {
$msg_from_img = $ifd1['Link'];
}

$timepass = timing($msg_time);
if ( $timepass == 'data' ) {
$timepass = date("dS M", $msg_time);
}

if ( ( $msg_from_status == 'full' ) || ( $msg_from_status == 'active' ) ) {

$result .= "
        <div class=\"$msgclass\" onclick=\"showconversation('$msg_conversation');\">
        <img src=\"m/members_image/$msg_from/thumb-$msg_from_img\" class=\"feedpic\" width=\"55\" height=\"55\" /><div class=\"messageposttext\"><a href=\"/$msg_from_username\">$msg_from_name</a>:<div class=\"inboxfeedreply\">Reply &nbsp; &middot; &nbsp; $timepass</div><br />$msg_message</div>
        </div>
        <div class=\"splittermessages\"></div>
";
}

Within each table entry in the messages table there is a 'time' stamp. 消息表中的每个表条目中都有一个“时间戳”。 Here's an example of the time entries: 1367680391. What's the best way to order the threads by newest reply first? 以下是时间输入的示例:1367680391。按最新回复顺序排序线程的最佳方法是什么?

First off I think you should group by $msg_conversation and find the last date on each conversation. 首先,我认为您应该按$ msg_conversation分组,并找到每次对话的最后日期。 With the code below you have the conversatons ordered by the last message each conversation/thread had. 使用下面的代码,可以使每个会话/线程拥有的最后一条消息按顺序排列会话。

$result = '';

$nowTime = time();

$getmessages = mysql_query("SELECT conversation, max(date) FROM messages WHERE msg_to = '$session_memberid' GROUP BY conversation ORDER BY max(date)");
while($iamessages = mysql_fetch_array($getmessages))
{
$msg_conversation = $iamessages['conversation'];
...

Further on you can get the messages for each conversation by date descending. 进一步,您可以按日期降序获取每个对话的消息。

我找人帮忙,答案是将查询更新为以下内容:

$getmessages = mysql_query("SELECT * FROM messages WHERE msg_to = '$session_memberid' GROUP BY msg_from ORDER BY MAX(time) DESC");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM