我在 ColdFusion 中制作了一个使用不同 MySQL 表的聊天脚本。一个是存储对话的地方,另一个是定义谁开始对话和谁接收对话的地方。
现在,我制作了一个脚本,让您可以与某人一起聊天。发送消息时,它会在 MySQL 表中添加一行,其中包含时间戳、消息 ID、发送消息的用户 ID 和消息本身。整个对话显示在这段代码中:
// Get all conversations started by the user that is signed in
<cfquery name = "Friendship" datasource = "#DSN#">
SELECT *
FROM `conversations`
WHERE `user_one` LIKE '#user.id#'
ORDER BY `conversations`.`lastmessage` DESC
</cfquery>
// Get all conversations started by others but receiver is the user that is signed in
<cfquery name = "FriendshipBack" datasource = "#DSN#">
SELECT *
FROM `conversations`
WHERE `user_two` LIKE <cfqueryparam value="#user.id#" cfsqltype="cf_sql_integer">
ORDER BY `conversations`.`lastmessage` DESC
</cfquery>
// OUTPUT - conversations that I began
<cfoutput query="Friendship">
// This code translates a user id into a name
<cfquery name = "FriendshipToUsername" datasource = "#DSN#">
SELECT *
FROM users
WHERE id = '#user_two#'
</cfquery>
<cfif IsDefined("FriendshipToUsername.username")>
<cfif IsDefined('URL.chat') and URL.chat neq "">
</cfif>
// Display username
<h3>#FriendshipToUsername.username#</h3>
</cfif>
</cfoutput>
/// OUTPUT - conversations that I received
<cfoutput query="FriendshipBack">
// This query translates a user id into a name
<cfquery name = "FriendshipToUsernameBack" datasource = "#DSN#">
SELECT *
FROM users
WHERE id = <cfqueryparam value="#user_one#" cfsqltype="cf_sql_integer">
</cfquery>
<cfif IsDefined('URL.chat') and URL.chat neq "">
</cfif>
// Display username
<h3>#FriendshipToUsernameBack.username</h3>
</cfoutput>
此时聊天列表分为两类:一类是登录用户开始对话的列表,另一类是登录用户收到其他人的对话的列表。问题是,我不希望它以这种方式显示,所以我想将两个列表混合在一起并根据时间戳(从最新到最旧)列出它们
此时聊天列表分为两类:一类是登录用户开始对话的列表,另一类是登录用户收到其他人的对话的列表。问题是,我不希望它以这种方式显示,所以我想将两个列表混合在一起并根据时间戳(从最新到最旧)列出它们
有什么办法可以做到这一点?
我知道我的代码是可利用的,但这仍然是 WIP