简体   繁体   中英

echo mysql data only once in while loop

I have managed to make a php chat where each message gets a row in my database table, the user_from column will of course be the same multiple times. Now I want to echo the latest message sent to or from the logged in user. I only manage to echo every message by using this code.

 <?php

  $select = mysql_query("SELECT * FROM privat_mess WHERE userid_to='$idn' OR      
  userid='$idn' ORDER BY identitet DESC");

 while($row = mysql_fetch_assoc($select)){
     $id = $row['identitet'];
     $userid = $row['userid'];
     $userid_to = $row['userid_to'];
     $user_from = $row['user_from'];
     $user_to = $row['user_to'];
     $msg_body = $row['msg_body'];
     $opened = $row['opened'];


    if($userid == $idn) {
    echo '<div class>
        <p><strong>'.$user_to.'</strong></p>
            '.$msg_body.'
    </div>';
    }
else{
        echo '<div>
        <p><strong>'.$user_from.'</strong></p>
          '.$msg_body.'

    </div>';
}

}

?>

The $userid is the $id from the users table for the user that sent the message, $idn is the id for the user that is logged in.

I'm sure it's possible to use unique_array() somehow but I would be really thankful if someone could show me how or if there is a better way. I also tried to use DISTINCT but I couldn't get it to work properly.

I wan't it to be like the messages on facebook where the one top is the latest active chat. If you press it, you will come to that chat

EDIT:

     $id = A_I, primary Key, the message id 
     $userid = the id for the user that sent the message 
     $userid_to = the id for the user that got the message 
     $user_from = name for the user who sent the message
     $user_to = name for the user who got the message
     $msg_body = The message text

I want to display the latest message from each chat.

By one chat I mean where the $userid and the $user_from are unique. If the same user sent more messages I don't want them to be displayed aswell.

From your comment but I want one message from each user not one message in total

Try this :

SELECT 
     * 
FROM 
     privat_mess 
WHERE 
     userid_to='$idn' 
OR      
     userid='$idn' 
GROUP BY 
     userid
ORDER BY 
     identitet DESC
$select = mysql_query("SELECT * FROM privat_mess WHERE userid_to='$idn' OR      
                       userid='$idn' ORDER BY identitet DESC limit 0,1");

Will only get you 1 record, the first. I'd sugest you read up on limit for a minute or two since it can speed up your query a lot!

EDIT:

Well, if you only want a solution, and not a fancy one (feels like I left my brain when I got home from work) just take out the identitet you want and then requery each of those:

SELECT 
      max(identitet), userid_from
FROM 
     privat_mess 
WHERE
     userid_to='$idn' OR userid='$idn'
GROUP BY userid_from

You will get the freshest identitet for each chat and can loop over each to get more info about each.

If you want to solve it through sql from the begining I got another way, but it's not pretty(don't use this on big databases!):

 SELECT * 
 FROM privat_mess 
 WHERE (userid_to='$idn' OR userid='$idn') AND 
 identitet IN (SELECT max(identitet) 
               FROM privat_mess
               WHERE userid_to='$idn' OR userid='$idn' 
               GROUP BY userid_from)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM