Jump to content

sort by date


bigrossco

Recommended Posts

I have the code below but would like to modify it so it will sort the output by the date

 

Thanks

 

r

 

<?php
    $host = "$lang_dbhost";
    $user = "$lang_dbuser";
    $pass = "$lang_dbpass";
    $db = "$lang_dbase";

    // open connection
    $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

    // select database
    mysql_select_db($db) or die ("Unable to select database!");



    $query = "SELECT id, DATE_FORMAT(date,'%d/%m/%Y'), time, tech, description
FROM calander";

    // execute query
    $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

?>




<form method="post">
  <?php
if (mysql_num_rows($result) > 0) {
echo"<table border = 1>";

while($row = mysql_fetch_row($result)){
echo"<tr>";
echo"<td><b>Date: </b>".$row[1]." <b>Time: </b>".$row[2]." <b>Staff: </b>".$row[3]." <a href=\"./update-appoint.php?id=".$row[0]."\">$lang_update</a>  <a href=\"./delete-appoint.php?id=".$row[0]."\">$lang_delete</a> </b></td>";
echo"</tr>";
        echo "<td>" . $row[4]."</td>";
//echo"</table>";
  }
  }
else {

echo"<b>Their is currently no appointments</b>";
}

?>

Link to comment
https://forums.phpfreaks.com/topic/37619-sort-by-date/
Share on other sites

Basically, since you're formatting the date, you're better off selecting it twice (once with formatting and once without). That way, you can ORDER BY the unformatted date:

<?php
$sql = mysql_query("SELECT date, DATE_FORMAT(date, '%d/%m/%Y') AS formatted, time, tech, description 
FROM calendar 
WHERE date = DATE() ORDER BY date");
?>

 

Notice also that if your datatype of the column is a DATE field, it's already in the %Y-%m-%d format, so you can lose the second DATE_FORMAT() call for a simple DATE() call instead.

 

Good luck

Link to comment
https://forums.phpfreaks.com/topic/37619-sort-by-date/#findComment-179923
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.