I have a messaging system on my site and I'm trying to group the messages into conversations, similar to how Facebook does. The following is the PHP code I'm using:
<form name="myform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<a href="#new_message" data-toggle="modal" class="btn btn-primary pull-right">New Message </a> <input type="submit" name="deleteBtn" class="btn btn-danger pull-right" id="deleteBtn" value="Delete Selected" />
<br /><br /> <?php
///////////End take away///////////////////////
// SQL to gather their entire PM list
$sql = mysqli_query($db_conx,"SELECT * FROM (SELECT * FROM private_messages WHERE to_id='$my_id' AND recipientDelete='0' ORDER BY time_sent DESC) AS tmp_table GROUP BY LOWER(from_id)");
while($row = mysqli_fetch_array($sql, MYSQLI_ASSOC)){
$item_date = $row["time_sent"];
$convertedTime = ($myObject -> convert_datetime($item_date));
$date = ($myObject -> makeAgo($convertedTime));
//$date = strftime("%b %d, %Y",strtotime($row['time_sent']));
if($row['opened'] == "0"){
$textWeight = 'msgDefault';
} else {
$textWeight = 'msgRead';
}
$fr_id = $row['from_id'];
// SQL - Collect username for sender inside loop
$ret = mysqli_query($db_conx,"SELECT id, username, firstname, lastname FROM bs_mem_base389 WHERE id='$fr_id' LIMIT 1");
while($raw = mysqli_fetch_array($ret, MYSQLI_ASSOC)){ $Sid = $raw['id']; $Sname = $raw['username']; $Sfirst = $raw['firstname']; $Slast = $raw['lastname'];
if ($Sfirst != "") {$Sname = "$Sfirst $Slast";} } //}
?>
<a href="message.php?id=<?php echo $fr_id; ?>" class="<?php echo $textWeight; ?>">
<p class="pull-right"><?php echo $date; ?> <input type="checkbox" name="cb<?php echo $row['id']; ?>" id="cb" value="<?php echo $row['id']; ?>" /></p>
<h4><?php echo $Sname; ?></h4>
<p><?php echo stripslashes(wordwrap(nl2br($row['message']), 54, "
", true)); ?></p></a>
<?php
}// Close Main while loop
?></form>
It's grouped them all together just fine, but they are not in the correct order. They're all jumbled. Any idea on how to order them so that the convo with the most recent message is first?