user1971485
user1971485

Reputation: 11

Trying to resolve an issue regarding ORDER BY in a query

I have created an HTML table filled in from a query and have set the query to order by in descending order however the query runs and the results show up in the table I cannot get the dates to run descending.

The database is correctly set up for a date field which is named as walk_date. I have also used strtotime to sort the date to human friendly.

05.10.2012
07.10.2012
18.08.2012
21.10.2012
22.09.2012

This is the order they come out in, and the following is my code:

<h1>A Photographic Diary</h1>
    <p>&nbsp;</p>

<div id="about-text">

<p>All walks are listed in order with the latest at the top. Walks can be filtered by area using the drop-down box. The abbreviated Area ID's are as follows:<br/>
YD  -   Yorkshire Dales<br/>
LD  -   Lake District<br/>
FB  -   Forest of Bowland<br/>
WPM -   West Pennine Moors<br/>
PD  -   Peak District<br/>
LOC -   Local<br/>
O   -   Other<br/></p>
</div>
 <!--Selects walk area -->
<br /> <form id="form1" name="form1" method="post" action="walk_results.php">
<label>Area</label>
<select name="area_id">
<option value="">All</option>


<?php
$sql_result = mysql_query("SELECT * FROM walk GROUP BY area_id ORDER BY walk_date DESC");
while ($row = mysql_fetch_assoc($sql_result)) {
        echo "<option value='".$row["area_id"]."'".($row["area_id"]==$_REQUEST["area_id"]? " selected" : "").">".$row["area_id"]."</option>";
};

?>
<!-- Submits walk area -->
</select>
  </label>

  <input type="submit" name="button" id="button" class="styled-button" value="Select Area" /></form> 
   <style type="text/css">
   .styled-button {
    -webkit-box-shadow:rgba(0,0,0,0.2) 0 1px 0 0;
    -moz-box-shadow:rgba(0,0,0,0.2) 0 1px 0 0;
    box-shadow:rgba(0,0,0,0.2) 0 1px 0 0;
    border-bottom-color:#333;
    border:1px solid #6470ED;
    background-color:#6470ED;
    border-radius:3px;
    -moz-border-radius:5px;
    -webkit-border-radius:5px;
    color:#fff;
    font-family:Verdana, Arial, Helvetica, sans-serif;
    font-size:12px;
    text-shadow:#b2e2f5 0 1px 0;
    padding:2px
}


  </style>
</select>
  </label>  

<!-- Walk area results table -->
<div id= "result">
<br /><br />
<table width="900" border="1" cellspacing="1" cellpadding="4">
  <tr>
    <td width="84" bgcolor="#6470ED" align="center"><strong>Select Walk</strong></td>
    <td width="75" bgcolor="#6470ED" align="center"><strong>Walk Date</strong></td>
    <td width="360" bgcolor="#6470ED" align="center"><strong>Title</strong></td>
    <td width="64" bgcolor="#6470ED" align="center"><strong>Distance (Miles)</strong></td>
    <td width="85" bgcolor="#6470ED" align="center"><strong>Ascent</strong></td>
    <td width="78" bgcolor="#6470ED" align="center"><strong>Time</strong></td>
    <td width="67" bgcolor="#6470ED" align="center"><strong>Area Id</strong></td>
  </tr>


<?php

if ($_REQUEST["area_id"]<>'') {
    $search_area_id = "AND area_id='".mysql_real_escape_string($_REQUEST["area_id"])."'";
}

if ($_REQUEST["from"]<>'' and $_REQUEST["to"]<>''){

}

else {
    $sql = "SELECT * FROM walk  WHERE walk_id>\"\"".$search_string.$search_area_id ;

}

$sql_result= mysql_query ($sql) or die ('request"Error"'.$sql);
#echo "SQL $sql";  --- to view info ---

$clicked =$_REQUEST["area_id"];
$sql= "SELECT * FROM walk  WHERE area_id = '$clicked'";

if (mysql_num_rows($sql_result)>0) {

    while ($row = mysql_fetch_assoc($sql_result)) {
    $id = $row['walk_id'];
    $date_of_walk = $row['walk_date']; 
    $date_of_walk = date("d.m.Y", strtotime($date_of_walk));
#echo "id $id<br>";  --- to view info ---


?>  
<!-- Table Results -->
  <tr>
    <td><?php echo "<a href='walk_info.php?walk_id=$id'>Select Walk"?></a></td>
    <td><?php echo $row['walk_date']=$date_of_walk;?></td>
    <td><?php echo $row["title"]; ?></td>
    <td><?php echo $row["distance"]; ?></td>
    <td><?php echo $row["ascent"]; ?></td>
    <td><?php echo $row["time"]; ?></td>
    <td><?php echo $row["area_id"]; ?></td>
  </tr>

<?php
    }
} else {  

$row= mysql_fetch_assoc($sql_result);$sql_result= mysql_query ($sql);
?>
<tr><td colspan="5">No results found.</td>
<?php   
 }
mysql_close()?>

Upvotes: 1

Views: 63

Answers (2)

JohnFx
JohnFx

Reputation: 34909

Change use the DESC keyword in your query.

For example, if this was your query

Select someDate from someTable ORDER BY someDate

change it to

Select someDate from someTable ORDER BY someDate DESC

Edit: okay now you have shown your code. Here's how to apply the above to your exact query

SELECT * FROM walk WHERE walk_id ORDER BY walk_date={...} DESC

That's all there is to it.

Upvotes: 1

John Conde
John Conde

Reputation: 219804

If you stored your dates as the standard MySQL date format (YYYY-MM-DD) this would be simple to do with SQL. Just use the DESC keyword in your ORDER BY clause with the walk_date field:

   SELECT *
     FROM table_name
 ORDER BY walk_date DESC

Upvotes: 2

Related Questions