我正在开发一个以列出的格式显示数据库产品的网站,到目前为止一直很好。但是,我想在上面添加一个简单的复选框,勾选后,按价格,最低到最高(以及从最高到最低的另一个)对列表进行排序。我试过这样做,但我遇到了问题。
首先,我添加了一个标有&#34的复选框(请参见下面的HTML);按最低价格排序"。然后我在主数据库查询下面的PHP代码中添加了一行(我的PHP代码大约10行),它试图在数据库查询的末尾附加一段查询语言,就在上面,但它没有&似乎工作正常。
我只是想这样做,以便当用户选中复选框时,它按最便宜的价格排序。我做错了什么?
注意:' buynow'是每行存储价格数据的地方。
HTML
<form action="spain-holidays.php" method="post">
<p>Order by lowest price:</p><input name="orderbyprice" type="checkbox" value="orderbyprice" />
</form>
PHP
<?php
//connect
include ("db.connect.php");
//pagination
$per_page = 20;
$pages_query = mysql_query("SELECT COUNT('id') FROM `holidaytable` WHERE brandname = 'Spain'");
$count = mysql_result($pages_query, 0);
$pages = ceil(mysql_result($pages_query, 0) / $per_page);
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;
echo '<p>There are '.$count.' holidays<hr></p><br /><br />';
//construct query and insert values
$query = mysql_query("SELECT * FROM holidaytable WHERE brandname = 'Spain' LIMIT $start, $per_page") or die(mysql_error());
//THIS IS THE LINE I ADDED HERE:
if (isset($_POST['orderbyprice'])) $query .= " ORDER BY buynow ASC";
while($rows = mysql_fetch_assoc($query)) {
$name = $rows['name'];
$id2 = $rows['id2'];
$hotelname = $rows['hotelname'];
$desc = $rows['desc'];
$spec = $rows['spec'];
$resort = $rows['resort'];
$awtrack = $rows['awtrack'];
$awthumb = $rows['awthumb'];
$awimage = $rows['awimage'];
$mthumb = $rows['mThumb'];
$mlink = $rows['mlink'];
$mimage = $rows['mimage'];
$buynow = $rows['buynow'];
$awcatid = $rows['awcatid'];
$mcat = $rows['mcat'];
$brandname = $rows['brandname'];
echo "<div class='resultbox'>
<div class='resultboxtopbar'>
<div class='hotelname'><h3>$hotelname</h3></div>
<div class='price'><h3>From <strong>£$buynow</strong> $spec</h3></div>
</div>
<div class='resultboxmain'>
<div class='resultboximage'><a href='$awtrack' target='_blank'><img src='$mimage' /></a></div>
<h4>$resort, $brandname</h4>
<p>$name</p>
<p>$desc...</p>
<div class='moreinfobutton'><input name='moreinfo' type='button' value='moreinfo' /></div>
</div>";
//more pagination, etc
答案 0 :(得分:1)
mysql_query()
不返回字符串,因此无法追加。你应该做这样的事情:
$sql = "SELECT * FROM holidaytable WHERE brandname = 'Spain' LIMIT $start, $per_page";
if (isset($_POST['orderbyprice'])) $sql .= " ORDER BY buynow ASC";
$query = mysql_query($sql) or die(mysql_error());