I have created a table in MySQL with following columns:
id - from_id - to_id - datetime - message
My PHP code is :
$user = $_SESSION['user'];
$sql= "SELECT * FROM messages where to_id=".$user;
$result = $conn->query($sql);
echo "<table><tr><td>FROM</td><td>MESSAGE</td></tr>";
while($row=mysqli_fetch_assoc($result)){
$from = $row['from_id'];
$message = $row['message'];
if(count($from) >1){
echo "More than 1 message from ".$from;
}
echo "<tr><td>".$from."</td><td>".$message."</td></tr>";
}
echo "</table>";
I can get all the messages sent to the user. But Im also getting messages sent FROM the same user in different lines. So for example, if user2 has sent user1 4 messages, all the 4 messages are listed in this list. I want only one row for the user2 and when i click on the row, i will see all the messages from that user. But on this page i want to see only 1 row with the latest message.
How can I achieve this?