gpt4 book ai didi

php - 如何获取用户之间对话的最新消息? (聊天系统)

转载 作者:行者123 更新时间:2023-11-29 21:40:46 25 4
gpt4 key购买 nike

我正在构建一个私有(private)消息系统。

我想根据对话时间获取最新消息。现在它按时间顺序显示消息,但我希望以逆时间顺序显示消息,最新消息在顶部等等。

表格

SELECT `messages`.`id`,
`messages`.`sender`,
`messages`.`receiver`,
`messages`.`message`,
`messages`.`datetime`,
`messages`.`classifield_id`
FROM `augl`.`messages`;

函数

public function getMessageList()
{
$current_user = $this->accounts->getUserId();

$query = $this->db->query("SELECT * FROM(
SELECT id, LEAST(receiver) as receiver, LEAST(sender) AS sender, message, datetime, classifield_id
FROM messages
WHERE sender='$current_user' OR receiver = '$current_user'
Group By (if(sender > receiver, sender, receiver))
, (if(sender > receiver, receiver, sender))
)
as id");

$result = $query->result();

//var_dump($result); die;

foreach ($result as $messages )
{
$return[$messages->id] = $messages;

if ($messages->receiver === $current_user)
{
$return[$messages->id]->userr = $this->accounts->getUsername($messages->sender);
$return[$messages->id]->userid = $messages->sender;
//$return[$messages->id]->message = $messages->message;

}
else
{
$return[$messages->id]->userr = $this->accounts->getUsername($messages->receiver);
$return[$messages->id]->userid = $messages->receiver;
//$return[$messages->id]->message = $messages->message;
}


}

return $return;
}

最佳答案

看起来如果你将“LEAST”更改为“GREATEST”可能会解决这个问题。

关于php - 如何获取用户之间对话的最新消息? (聊天系统),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34539026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com