PHP自定义聊天,编码块功能

时间:2013-11-09 18:57:13

标签: php mysql

我试图建立一个没有任何运气的Block功能,我的SQL技能并不像我希望的那样好。

我有一个名为“messages”的表和一个名为“blocks”的表 现在,有1个文件将所有内容同步到聊天,我正在尝试做的是IF用户1阻止用户2而不是用户1的消息永远不会到达用户2而用户2的消息不应该到达用户1.短期内,如果你阻止一个你不能和他/她说话的人和他/她不能跟你说话!

"blocks" table:
id bigint(20)
user_id tinyint(20)
block_id tinyint(20)

"messages" table:
id bigint(20)
timestamp datetime
dest_type varchar(255)
dest_id bigint(20)
source_type varchar(255)
source_id bigint(20)
message_type varchar(255)
message text

在“blocks”中,user_id是块行的所有者ID。 和block_id是所有者想要阻止的id。 如果"messages.source_id = blocks.block_id OR messages.block_id = blocks.user_id" 不要让信息低谷。我明白要求别人为我编码是非常粗鲁但是我问,有人可以试一试吗?

这是sync.php文件: http://pastebin.com/8iiSCXGS

非常感谢!

1 个答案:

答案 0 :(得分:1)

我没有深入研究你的代码,但也许这会有所帮助。

让我们从简化的数据库结构开始,如下所示:

CREATE TABLE `blocks` (
  `id`            BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `user_id`       INT UNSIGNED NOT NULL,
  `block_id`      INT UNSIGNED NOT NULL );

INSERT INTO `blocks` (`user_id`,`block_id`) VALUES
  (1,2),(3,4),(2,1);

CREATE TABLE `messages` (
  `id`            BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `author_id`     BIGINT NOT NULL,
  `message`       TEXT NOT NULL );

INSERT INTO `messages` (`author_id`,`message`) VALUES
  (1,"Message from user #1, who has a mutual block in place with user #2"),
  (2,"Message from user #2, who has a mutual block in place with user #1"),
  (3,"Message from user #3, who has blocked user #4"),
  (4,"Message from user #4, who has been blocked by user #3"),
  (5,"Message from user #5, who takes no part in all this blocking business");

现在让我们假设用户$n访问网站(其中1≤$n≤5)。为了确定哪些消息可以显示,我们需要执行messagesblocks表的LEFT JOIN - 即,我们要考虑messages的每一行以及任何一行blocks包含相关信息(具体而言,邮件作者阻止用户$n,或已被用户$n阻止)。如果$n = 1,我们有以下内容:

SELECT * FROM `messages`
  LEFT JOIN `blocks`
    ON (`author_id`=`block_id` AND `user_id`=1)
    OR (`author_id`=`user_id` AND `block_id`=1);

以下是该查询的结果:

+----+-----------+-----------------------------------------------------------------------+------+---------+----------+
| id | author_id | message                                                               | id   | user_id | block_id |
+----+-----------+-----------------------------------------------------------------------+------+---------+----------+
|  1 |         1 | Message from user #1, who has a mutual block in place with user #2    | NULL |    NULL |     NULL |
|  2 |         2 | Message from user #2, who has a mutual block in place with user #1    |    1 |       1 |        2 |
|  2 |         2 | Message from user #2, who has a mutual block in place with user #1    |    3 |       2 |        1 |
|  3 |         3 | Message from user #3, who has blocked user #4                         | NULL |    NULL |     NULL |
|  4 |         4 | Message from user #4, who has been blocked by user #3                 | NULL |    NULL |     NULL |
|  5 |         5 | Message from user #5, who takes no part in all this blocking business | NULL |    NULL |     NULL |
+----+-----------+-----------------------------------------------------------------------+------+---------+----------+
6 rows in set (0.00 sec)

如您所见,我们想要的行是最后三列为NULL的行,这意味着没有阻止规则影响此特定用户显示此特定消息。因此,要提取这些消息,我们只需将WHERE block_id IS NULL添加到查询的末尾:

block_id

如果您将不同的用户ID替换为此查询,则应获得您所追求的结果。