Jump to content

Private Message Group By Conversation


ShoeLace1291

Recommended Posts

Hello, all!  I am trying to write my own private messaging chat script for my website.  I would like the "Inbox" to display the "conversations" with other users, but only by the last message of the conversation.  I found a thread on stackoverflow and used a query suggested on that page.  The query doesn't find any rows.  http://stackoverflow.com/questions/10520246/mysql-query-to-group-messages-into-a-conversation-and-show-the-first-in-the-inbo

 

4gfTb.gif

$query_convos = "
            SELECT m1.message_id, m1.author_id, m1.recipient_id, m1.content, UNIX_TIMESTAMP(m1.date_sent)
            FROM private_messages AS m1
            WHERE m1.date_sent = (
                SELECT max(m1.date_sent)
                FROM private_messages AS m2
                WHERE m1.author_id = m2.author_id
                    AND m1.recipient_id = m2.recipient_id
                    AND (author_id = ".$this->user['id']." OR recipient_id = ".$this->user['id'].")        
            )
            GROUP BY m1.author_id, m1.recipient_id
            ORDER BY m1.message_id DESC
            LIMIT 10
            ";
Edited by ShoeLace1291
Link to comment
Share on other sites

It would be so much easier if you just attached a "conversation ID" to each message. Which you know when the messages are created: creating a new message is a new conversation, while hitting Reply would be continuing an existing conversation.

That ID can very easily be just the ID of the original message; when you make a new message in the conversation you copy the ID from the message being replied to, if present, or use that message's own ID, if not.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.