mysql消息已读未读,显示消息状态:PHP / MySQL消息系统中的已读和未读(与其他用户不同)...

I am displaying the message status that differs from each user. Let's say user1 sends a message to user2, user1's message status then sets to read, while user2's message is set to unread by default. It will be updated after user2 clicks the message.

So in these scenario, the message of user1 (from the inbox) will have a gray-colored font which indicates that the message is set to read (since user1 is the one who is sending). On the other side, user2 have a bold font that indicates that the message is unread.

Here is the first structure of the table:

message(messageid, fromid, toid, message, timestamp, status)

The problem here is that if I update the message status to read, it affects the other side (user2). So I add another column that will set the status differently from user1 and user2:

message(messageid, fromid, toid, message, timestamp, from_status, to_status)

Here, from_status is for the fromid and to_status is for toid. But I'm having a problem on how to use these values to display the status.

The PHP code of that I use during my first attempt is these:

$id = $_SESSION['id'];

$query = mysql_query("SELECT m.* FROM message m

LEFT JOIN message m2 ON (

(m.fromid=m2.fromid AND m.toid=m2.toid) OR

(m.fromid=m2.toid AND m.toid=m2.fromid)

) AND m.timestamp

WHERE (m.fromid='$id' OR m.toid='$id') AND m2.toid IS NULL ORDER BY timestamp DESC");

while ($message = mysql_fetch_array($query)) {

if ($message['status'] === 'unread') {

// bold font style will be applied

}

else {

// gray-colored font will be applied

}

}

?>

(The query fetches each conversation from every user with the latest conversation.)

These code works fine for the main user, which is user1, but affects the other side, which views that the message received from user2 is set to read instead or unread.

So, I'm having some trouble on what to do on the modified table, having 2 separate status each for each user. How can I get these done?

解决方案

@andrewsi comment is quite nice, when you'll have for example many receivers. In your case it's only one additional field, so in my opinion it's not an overflow to use just one table.

Regarding your case you can do this in one simple sql:

SELECT m.*,

CASE

WHEN m.fromid = $id THEN m.from_status

WHEN m.toid = $id THEN m.to_status

END as read_status

FROM message m

WHERE

m.fromid = $id OR m.toid = $id

ORDER BY timestamp DESC;

And in your view you are only checking the read_status field

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值