Am building a message system with php and mysql and am using this tutorial as my guide http://aaronsaray.com/blog/2010/07/27/facebook-message-system-in-php
Am working on the inbox page and am having issues with the inbox as it wont select messages that have already been read, so when a user opens their inbox to find prior message, they are presented with a blank page because the query wont select their messages which they have read.
this is the query
$sql = "select m.mid, m.seq, m.created_on, m.created_by, m.body, r.status from message_recips r
inner join ".$this->msg_table." m on m.mid=r.mid and m.seq=r.seq
where r.uid=".$this->user_id." and r.status in ('A', 'N')
and r.seq=(select max(rr.seq) from message_recips rr where rr.mid=m.mid and rr.status in ('A', 'N'))
and if (m.seq=1 and m.created_by=".$this->user_id.", 1=0, 1=1)
order by m.created_on desc";
This is explanation of the query from the tutorial
First thing is to get both of the identifiers for the message (MID/SEQ), when it was created (so we can show the date), who created it (so we can show the originator or who it is ‘from’), and the status. The status will just be used to show if that message is new.
The sql gets the data from the recips table first. This is the pointer to all of the ‘copies’ of the initial message that should be available. Note that the message table itself is joined on so we can get the actual content of the message. Next, the recipient UID is verified to be the current user and the message must be either New or Active. Next, the sequence number must be a specific one. In this case a subselect is done. The maximum sequence number (so that would make it the newest) from the recips table where that message is the current message and the status is not deleted. In this case we don’t verify that the UID of that subselect is any user because we want to show any originator whether it be ourself or someone else. The last part of the where clause verifies that the sequence number is not 1 and that its not created by our current user. If it is 1, that means its the first message of the thread, created by us, and that we shouldn’t select it. Your inbox never shows items that you have originally sent but received no responses.
Then, the rest is pretty simple. All of the items are retrieved. A loop is generated and each ‘newest’ message is shown with a link to view it. Notice how the view link only has the MID, however. We don’t need to know the sequence number as we’ll be showing the entire thread.
How should this query be modified?