我在我的網站上有一個消息傳遞系統,我試圖將消息分組到對話中,類似於Facebook的做法。以下是我正在使用的PHP代碼:MySQL數據與PHP的順序不正確
<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, "\n", true)); ?></p></a>
<?php
}// Close Main while loop
?></form>
它將它們組合在一起就好,但它們的順序不正確。他們都混亂。有關如何訂購它們的想法,以便帶有最新消息的convo是第一個?
你在select中沒有ORDER BY,在db中沒有固有的順序,你必須明確地設置它 – 2013-07-22 22:51:46