按照Facebook的用户名对所有消息进行分组

时间:2014-03-11 09:22:23

标签: php mysql sql facebook

好的,经过几个小时的编码后,我最终得到了这段代码,我被困在这里。我想要做的是,如果你转到你的Facebook消息,你会看到Facebook将来自用户名的所有消息分组并显示在页面上,这就是我在这里尝试做的事情。

以下是我的代码。仍在努力让一些东西为我工作。还有当前代码示例的屏幕截图。

SQL

<?php
    $session_id = GET_SESSION_ID_VALUE(ENCRYPTION_KEY);
    $sql = "SELECT messages.*, profile.profile_id, profile.profile_name, profile.profile_photo, profile.profile_username FROM messages INNER JOIN profile ON profile.profile_id = messages.message_from WHERE messages.message_from = ' " . $session_id . "' OR messages.message_to = ' " . $session_id . "' ORDER BY messages.message_datetime DESC";
    $query = $db->SELECT($sql);
    if($db->NUM_ROWS() > 0){
        $rows = $db->FETCH_OBJECT();
        foreach($rows as $row){
            $message_id = $row->message_id;
            $message_from = $row->message_from;
            $message_content = $row->message_content;
            $message_content = (strlen($message_content) > 90) ? substr($message_content, 0, 100) . '...' : $message_content;
            $message_username = $row->profile_username;
            $message_name = $row->profile_name;                     
            $message_photo = $row->profile_photo;                       
        /* HTML GOES HERE */
        }
    }
    ?>

数据库表

enter image description here

上述代码的屏幕截图。 enter image description here

1 个答案:

答案 0 :(得分:1)

<?php
    $session_id = GET_SESSION_ID_VALUE(ENCRYPTION_KEY);
    $sql = "SELECT messages.*, profile.profile_id, profile.profile_name, profile.profile_photo, profile.profile_username FROM messages INNER JOIN profile ON profile.profile_id = messages.message_from WHERE messages.message_from = ' " . $session_id . "' OR messages.message_to = ' " . $session_id . "' ORDER BY messages.message_datetime DESC";
    $query = $db->SELECT($sql);
    if($db->NUM_ROWS() > 0){
            $prevMessage_from=""; --Get name of previous message
            $rows = $db->FETCH_OBJECT();
            foreach($rows as $row){
            while ($prevMessage_from=$row->message_from)
             {--Here must be code that add messages to existing--}
            if ($prevMessage_from<>$row->message_from)
            {
            $message_id = $row->message_id;
            $message_from = $row->message_from;
            $message_content = $row->message_content;
            $message_content = (strlen($message_content) > 90) ? substr($message_content, 0, 100) . '...' : $message_content;
            $message_username = $row->profile_username;
            $message_name = $row->profile_name;                     
            $message_photo = $row->profile_photo;                       
            $prevMessage_from=$message_from;
            /* HTML GOES HERE */
            }

        }
    }
    ?>