开发者

Group result by field

Am building a messaging system for a site, but i want it to have an instant messaging app feel. I have table with a structure like this:

<!-- language: lang-none -->
id
R_id     = Reciever's id
S_id     = Sender's Id
message
read     = 0 if unread, 1 if read
post_time
conv_id  = conversation id

I am trying to build a query that retrieves all messages pertaining to a receiving user and display it in a group format i.e all messages from a user grouped with the user's name, sort of like facebook messages.

This is the method in my model, am working with codeigniter

function get_user_conversations($user_id) {
       //Load Models
       $this->load->model('conversation_model');
       //Load helper
       $this->load->helper('date');
       //database query
       $q = $this->db->select('*')
                     ->from('conversations_inbox')
                     ->where('R_id',$user_id)
                     ->group_by('S_id')
                     -&开发者_如何学编程gt;order_by('post_time','desc')
                     ->get();
        $conversations = $q->result();
        return $conversations;
   }


Maybe something like this?

// Presuming you want the most recent ones first
$q = mysql_query("SELECT message FROM `TABLE` WHERE `R_id`='USERNAME_HERE' ORDER BY `post_time` DESC");
while ($row = mysql_fetch_array($q)) {
  echo "<li>Message: " . $row['message'];
}

I'm honestly not really sure what you're asking, so that's the best I can do.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜