我有一个包含四行的表格(时间、姓名、文本和 chat_id)
time | name | text | chat_id
------------------------------+---------------+--------------+---------
2017-03-25 13:15:02.70061+00 | James Douglas | Hello world! | 478
(1 row)
这里是插入数据库的东西(工作正常):
$db->perform("insert into chat(name,text) values($1,$2)",$_SESSION['name'],$_POST['text']);
这是将页面上的数据显示为聊天消息的东西:
$('#chatbox').append('<div><p><b>'+v.name+'</b> - <span>'+v.text+'</span><span style="display: inline-block; float: right;"><small>('+v.timee+')</small></span></div>');
和注销消息:
$db->perform("insert into chat(name, text) values($1,$2)",$_SESSION['name'],' has left the chat session');
所有这些大部分都运行良好,聊天消息如下所示:
詹姆斯·道格拉斯 - 世界,您好! (星期六 13:15)
不幸的是,注销消息如下所示:
James Douglas - 已离开聊天 session (星期六 13:26)
我怎样才能让注销消息看起来像这样呢? James Douglas 已离开聊天 session
(大概编辑这两个)
$('#chatbox').append('<div><p><b>'+v.name+'</b> - <span>'+v.text+'</span><span style="display: inline-block; float: right;"><small>('+v.timee+')</small></span></div>');
$db->perform("insert into chat(name, text) values($1,$2)",$_SESSION['name'],' has left the chat session');
提前致谢!
最佳答案
我认为你的做法是错误的。您应该将文本消息与操作消息分开。操作消息可能是所有消息有人加入/离开/更改昵称等
。您可以在数据库中添加一个字段,例如:
time | name | text | chat_id | type
------------------------------+---------------+--------------+---------+---------
2017-03-25 13:15:02.70061+00 | James Douglas | Hello world! | 478 | text
------------------------------+---------------+--------------+---------+---------
2017-03-25 13:15:12.70061+00 | James Douglas | 1 | 479 | action
然后有另一个表,您将操作 ID 与其各自的文本相关联,例如:
id | text
----------+-------------------------
1 | User left the channel
----------+-------------------------
2 | User joined the channel
所以当您尝试打印行时,您可以为不同类型的文本设置不同的 css 类
$('#chatbox').append('<div class="text-type-'+v.type+'"><p><b>'+v.name+'</b> - <span>'+v.text+'</span><span style="display: inline-block; float: right;"><small>('+v.timee+')</small></span></div>');
关于php - 聊天框注销消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43016977/