2013-11-09 29 views
1

我試圖建立一個塊沒有任何運氣的功能,我的SQL技能不如我希望他們會在這種情況下。PHP自定義聊天,編碼塊功能

我有一個名爲「messages」的表和一個名爲「blocks」的表 現在,有一個文件將所有內容同步到Chat,我想要做的是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 

in「blocks」user_id是塊行的所有者id。 和block_id是所有者想要阻止的ID。 IF "messages.source_id = blocks.block_id OR messages.block_id = blocks.user_id" 但不要讓消息得到低谷。我明白,要求某人爲我編碼非常粗魯,但我問,任何人都可以給這個鏡頭?

這裏的sync.php文件: http://pastebin.com/8iiSCXGS

非常感謝!

+0

所以messages.source_id是消息創建者的ID嗎?這是一個私人消息傳遞操作嗎?從創建者的角度來看,如果他們阻止了某人,那麼您希望以創建該消息的形式限制它們的可用性。從被阻止的用戶的角度來看,你想讓他們知道他們是否被阻止? – sark9012

+0

你好,是的,這是正確的!我希望sync.php不要拍攝來自阻止我並且被阻止的用戶的消息。如上所示,所有塊都存儲在塊表中。在私人聊天中我修復了Block功能!但是在開放的房間裏,每個人都可以互相講話,sync.php文件會在用戶之間發送所有消息,這就是爲什麼sync.php必須檢查blocks表以查看它是否應該接收消息,這是非常複雜的(至少我...) – user2965339

+0

好吧,它仍然會將消息存儲在數據庫中,但不會顯示它們。我相信這可以通過將表格連接在一起來實現。 – sark9012

回答

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表的左加入 - 即,我們要考慮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) 

正如你所看到的,我們想要的行是其中最後三米欄是空的那些,這意味着沒有影響向特定用戶顯示此特定消息的阻止規則。因此,要提取這些信息,我們只需添加WHERE block_id IS NULL到查詢的末尾:

SELECT * FROM `messages` 
    LEFT JOIN `blocks` 
    ON (`author_id`=`block_id` AND `user_id`=1) 
    OR (`author_id`=`user_id` AND `block_id`=1) 
    WHERE `block_id` IS NULL; 
+----+-----------+-----------------------------------------------------------------------+------+---------+----------+ 
| 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 | 
| 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 | 
+----+-----------+-----------------------------------------------------------------------+------+---------+----------+ 
4 rows in set (0.01 sec) 

如果替換不同的用戶ID添加到這個查詢,你應該得到你之後的結果。

+0

我明天醒來時會檢查這個! – user2965339

+0

謝謝squeamish ossifrage這幫助我解決了這個問題:D – user2965339

+0

不客氣。我可以補充一點,如果你想在公共網站上實現這種「忽略」系統,請確保主持人仍然可以看到忽略他們的人的消息,否則垃圾郵件發送者可能會忽略所有人,然後填寫沒有被檢測到的帶有鏈接垃圾和其他垃圾的網站。 –