开发者

chat/messaging PHP foreach loop confusion

I'm trying to set up a messaging system where a user's messages are displayed in chronological order. I'd like to have the user's replies to a given message be listed directly below the message they replied to.

Basically I was thinking I need the following 5 varia开发者_运维技巧bles: user_id, recipient_id, message_id, reply_id, and message.

Can someone offer a basic PHP solution for this? I'd assume all it will take is some foreach loops and if, else statements, I just can come up with solution.

Here's what I have so far which only echos the user's replies and not their messages, nor nested in within their messages:

<?php foreach($messages as $message)
{
    $msg_id=$message->message_id;
    $rply_id=$message->reply_id;
    if($msg_id=$rply_id)
    {
        echo $message->message;
    }
}
?>


Here is how I would do this:

Format the data pulled from the database into a basic id-indexed array and a foreign-key-indexed array

  • Pull all messages from the database that are NOT replies, order by date.

  • Index the resulting array by message id.

  • Pull all messages from the database that are replies, order by date.

  • Loop over the replies, putting them into a new, multidimentional array, grouped by reply_id.

Formatting of output

Foreach loop over non-reply messages:

  • Display each message.

  • Pull the id of each message and check whether it is a key in the replies array.

  • If it is present in the replies array...

    • Foreach loop to display each reply message that is present.

End of message foreach loop.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜