2

I have a table messages, and I would like to display the most recent message. If user 1 is logged in, I would like to do this: For instance if user 1 sent an message to user 2, then user 2 to user 1, I would like to show to user 1, the newest message. So it would be the one that was sent by user 2. My table has:

id |  id_from|id_to|text 
1     1       2     abc     
2     2       1     dif      
3     3       1     jsd      

So, I have to display to user 1, the ids 2 and 3.

I try this: thanks a lot!

   $consulta=mysql_query("select * from messages where id_from='".$id."' or id_to='".$id."'  ");
while($filas=mysql_fetch_array($consulta)){
    $id=$filas['id'];
    $id_to=$filas['id_to'];
    $status=$filas['status'];
    $text=$filas['text'];
    $time=$filas['time'];
    echo "lo primero: ";

$consulta2=mysql_query("select * from messages where id_from='".$id."' or id_to='".$id."' ");
    while($filas2=mysql_fetch_array($consulta2)){
        echo "es ";
        $id2=$filas2['id'];
        $id_to2=$filas2['id_to'];
        $status2=$filas2['status'];
        $text2=$filas2['text'];
        $time2=$filas2['time'];
        echo "es ";
        echo $id2;
            if($id_from==$id_from2 and $id_to==$id_to2 and $id2>$id){ 
                $id=$id2;           
                echo "el mas grande ";
                echo $id;
            }
    }
    echo $id;
    echo " ";
    //Almaceno en un vector los mensajes a mostrar
    $result  = array ( "'".$id."'");
6
  • can you post some sample table data? Commented Jan 2, 2013 at 6:41
  • Any error you are getting? Commented Jan 2, 2013 at 6:41
  • The code never enters to the second while ,thats my Question : ) Commented Jan 2, 2013 at 6:44
  • And how can I make it? I would like to display the most recent message Commented Jan 2, 2013 at 6:46
  • Heads up! The next major release of PHP is deprecating the mysql_ family of functions. Now would be a great time to switch to PDO or mysqli. Commented Jan 2, 2013 at 8:44

2 Answers 2

0

Use a single simple query

SELECT * FROM mytable WHERE id_from != 1 AND id_to = 1
Sign up to request clarification or add additional context in comments.

1 Comment

What I put on that table is an example the table perhaps has lots of rows, so I have to show for the user that is logged in, all the inboxes that he sent, or he received
0

I assume first column id is set to auto increment. To show the most recent activity of user with id=$userid use

SELECT * 
FROM  `messages` 
WHERE  `id_from` =  '$userid' ||  `id_to` =  '$userid'
ORDER BY  `id` DESC 
LIMIT 1

From this query we will get the single last activity of the user.From that resultant row u can check whether the activity is a sent message or recieved one and show the text accordingly.

If ur simply tring to show inbox messages with most recent one on top use

    SELECT * 
    FROM  `messages` 
    WHERE  `id_to` =  '$userid'
    ORDER BY  `id` DESC 

2 Comments

I want to do what facebook does. for instance you send an inbox to user 2, then to user 3, then user 2 answers you, so you have to display the message that user 2 sent to you and the one that you sent to user 3
Ok. So u r trying to display messages like chat .Clicking on a user will show all conversations between u and him in order.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.