1

我已经创建了一个从查询中填写的HTML 表,并将查询设置为按降序排序,但是查询运行并且结果显示在表中我无法获取运行降序的日期。

数据库已为名为 walk_date 的日期字段正确设置。我也习惯strtotime将日期排序为人类友好。

05.10.2012
07.10.2012
18.08.2012
21.10.2012
22.09.2012

这是他们出来的顺序,以下是我的代码:

<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()?>
4

2 回答 2

2

如果您将日期存储为标准 MySQL 日期格式 (YYYY-MM-DD),那么使用 SQL 就可以轻松完成。只需将子句中的DESC关键字与字段一起使用:ORDER BYwalk_date

   SELECT *
     FROM table_name
 ORDER BY walk_date DESC
于 2013-01-12T00:10:45.820 回答
1

更改使用DESC查询中的关键字。

例如,如果这是您的查询

Select someDate from someTable ORDER BY someDate

将其更改为

Select someDate from someTable ORDER BY someDate DESC

编辑:好的,现在您已经显示了您的代码。以下是将上述内容应用于您的确切查询的方法

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

这里的所有都是它的。

于 2013-01-12T00:10:34.513 回答