我创建了一个从查询中填写的 HTML表格,并将查询设置为按降序排序然而查询运行并且结果显示在table我无法获取运行降序的日期。
为日期字段正确设置数据库,该字段名为walk_date。我还使用strtotime
将日期排序为人类友好。
2012年5月10日
二○一二年十月七日
2012年8月18日
2012年10月21日
2012年9月22日
这是他们出来的顺序,以下是我的代码:
<h1>A Photographic Diary</h1>
<p> </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()?>
答案 0 :(得分:2)
如果您将日期存储为标准的MySQL日期格式(YYYY-MM-DD),这对SQL来说很简单。只需使用DESC
字段中的ORDER BY
关键字和walk_date
字段:
SELECT *
FROM table_name
ORDER BY walk_date DESC
答案 1 :(得分: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
这就是它的全部内容。