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."'");
mysql_family of functions. Now would be a great time to switch to PDO or mysqli.