gpt4 book ai didi

php - 获取聊天数据并按接收者和发送者 ID 排序

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

我有一个问题。

我有一个这样的数据库:

messages_id | int | Auto-increment
from | int
to | int
message | text

现在我在按发件人 ID 对它们进行分组时遇到问题。我只想检索登录用户发送或接收的消息。这并不太难。

SELECT * FROM messages WHERE from = 1 OR to = 1 ORDER BY messages_id ASC

但现在,他们没有分组。因为不同的人可以向该用户发送消息。我真的不知道从哪里开始。

我想要这样的东西:

array(
[5] => array(
[0] => "message One",
[1] => "Message two"
),
[32] => array(
[0] => "message One",
[1] => "Message two"
)
);

5 和 32 是正在聊天的人的 ID。

希望大家能帮忙:)

最佳答案

感谢大家的回复。真的很感激,但我自己已经弄清楚了;)

现在我得到以下信息:

$currentUserID = get_current_user_id();
$rows = $wpdb->get_results("SELECT * FROM `messages` WHERE `from` = '" . $currentUserID . "' OR `to` = '" . currentUserID . "' ORDER BY `messages_id` ASC");

$messages = [];

foreach($rows as $row) {
if($row->from == $currentUserID) {
$messages[$row->to][] .= $row->message;
}
else {
$messages[$row->from][] .= $row->message;
}
}
print_r($messages);

关于php - 获取聊天数据并按接收者和发送者 ID 排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39891062/

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