更改日期的顺序以显示上面的最新和最早的下面

时间:2014-01-18 13:54:15

标签: php mysql date

我正在通过交易制作购物车。 我想根据日期显示我的项目, 最新的日期最早会出现在我的最上面。

这个项目现在实际上是从最早到最晚。 我可以知道如何最早将其更改为最新版本?

<?php
do{
$des = $row_supermarketcart['productdes'];
$pack = $row_supermarketcart['package'];
$price = $row_supermarketcart['itemprice'];
$qty = $row_supermarketcart['qty'];
$ddate = $row_supermarketcart['ddate'];


if ($row_supermarketcart['username'] == $_SESSION['MM_Username'] )
{

            echo "<tr>";
            echo "<td>". $des ."</td>";
            echo "<td>". $pack. "</td>";
            echo "<td>$" .$price. "</td>";
            echo "<td>". $qty. "</td>";
            echo "<td>".$ddate."</td>";


}

else
{
     echo "<br>";
      echo "<center><b>YOU HAVE NOT MAKE ANY TRANSACTION YET</b></center>";
      echo "<br>";
      echo "<br>";

}

} while ($row_supermarketcart = mysql_fetch_assoc($supermarketcart));
?>

1 个答案:

答案 0 :(得分:4)

在SQL查询中使用ORDER BY子句按日期排序:

 ORDER BY `ddate` DESC

不仅在SQL查询中更快,而且您不必更改PHP代码。