Treehouse: Grow your CSS skills. Land your dream job.

Only show one conversation for each user

  • # July 23, 2012 at 7:38 am

    OK, so it’s difficult to explain but I’m gonna try. Basically, I’m making a messages or conversations page, where you can see your conversations. I get the conversation from the messages, so basically I show every message. What I want to do however, is to only show the latest message of each conversation. Here is how I currently have it:

    	< ?php
    $getmsg = mysql_query("SELECT * FROM messages WHERE to_user = '$username' ORDER BY id DESC");
    if (!$getmsg){
    while ($row = mysql_fetch_assoc($getmsg)){
    $from = $row;
    $content = $row;
    $msgid = $row;
    $seen = $row;
    $shortcontent = substr($content, 0, 60)."...";
    notseen< ?php } ?>" href="/convo/< ?php echo($from); ?>">
    < ?php echo($from); ?>

    < ?php
    if (strlen($content) < 65){

    < ?php

    I sent two messages to myself, and they both show up on the conversation page, I want it so that only the latest of them show up there, and if someone else gives me a message, their latest show there as well. Understand?

    Thank you.
    – Schart

    # July 23, 2012 at 12:55 pm

    Try changing your query to this:

    $getmsg = mysql_query("SELECT * FROM messages WHERE to_user = '$username' ORDER BY id DESC LIMIT 1");

    Limit 1 tells the query to only grab one result.

    # July 23, 2012 at 12:59 pm

    Sure but I need one for each conversation.

    # July 23, 2012 at 1:05 pm

    Ah alright, my bad. The easiest way I can think of would be to keep track of the conversation with an id, and then that way you can limit the number of results for each conversation to its ID with the newest result.

    # July 23, 2012 at 1:35 pm

    @ChrisxClash No I just explained it badly :) – Yeah I’ll try that.

    # July 23, 2012 at 11:46 pm

    I agree, you need a way to identify which conversation the message is a part of and include a date stamp column so that you can get the latest message.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.