Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.