de rossi
de rossi

Reputation: 13

How to make this code sort by date?

<?php

$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("dbsql", $con);
$offset = 0;
$result = mysql_query ("SELECT * FROM testimonial  where approved='Yes' limit $offset, 10");

echo "<table border='0'>
<tr><font  color='#000099' style='font-size:18px; margin-left:200px;'>Recently Post</font></tr>
<tr>
<th>From</th>
<th width=`400px`>&nbsp;Review</th>
<th>Date</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";

  echo "<td>" . $row['full_name'] . "</td>";
  echo "<td>" . $row['review'] . "</td>";
    echo "<td>" . $row['date'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>

Upvotes: 1

Views: 255

Answers (3)

cbrandolino
cbrandolino

Reputation: 5883

MySQL has an ORDER BY built-in function.

$result = mysql_query ("SELECT * FROM testimonial where approved='Yes' ORDER BY date LIMIT $offset, 10");

Upvotes: 2

oezi
oezi

Reputation: 51797

change your SQL to this:

SELECT * FROM testimonial  where approved='Yes' ORDER BY date LIMIT $offset, 10
                                                ^^^^^^^^^^^^^

Upvotes: 2

Cl&#233;ment
Cl&#233;ment

Reputation: 12917

Just add ORDER BY date to your SQL query =)

Upvotes: 1

Related Questions