使用复选框进一步过滤SQL数据库结果(PHP)

时间:2016-02-20 14:19:20

标签: php mysql

我获得了使用MySQL代码的编码分配。我已经能够做搜索按钮,它完美地工作。但我也被要求使用复选框过滤搜索结果,但我不能这样做。 以下是PHP代码:

搜索结果显示在表格中并使用复选框,我必须根据结果的RATING过滤结果。复选框是“filterstar5”,依此类推。我怎样才能实现这一目标?我google了,Ajax似乎是解决方案,但我还没有学过Ajax。

非常感谢。 (我知道这些是旧的SQL代码,这是我学习它们的任务的一部分)。 这是我想要的一个例子:http://hibbard.eu/blog/pages/ajax-filter/

<?php
error_reporting(0);
include("config.php");
?>

<section id="reservation-form">

<form id="form1" name="form1" method="post" action="">

<label>Search City</label>
<input name="citysearch" type="text"/>

<label for="from">Check in</label>
<input name="from" type="text" id="from" size="10"/>

<label for="to">Check out</label>
<input name="to" type="text" id="to" size="10"/>

<label >Price($)</label>
<select name="pricefrom">
<option value="">--</option>
<option value="100">100</option>
<option value="150">150</option>
<option value="200">200</option>
<option value="500">500</option>
</select>

<label >To($)</label>
<select name="priceto">
<option value="">--</option>
<option value="150">150</option>
<option value="200">200</option>
<option value="250">250</option>
<option value="900">900</option>
</select>

<button type="submit">Search</button>
</form>
</section>




<h4>STARS</a></h4>
<input type="checkbox" name="filterstar5" value="5">5
<input type="checkbox" name="filterstar4" value="4">4
<input type="checkbox" name="filterstar3" value="3">3
<input type="checkbox" name="filterstar2" value="2">2
<input type="checkbox" name="filterstar1" value="1">1



<?php
if ($_REQUEST["citysearch"]<>'') {
$search_citysearch = " AND city='".mysql_real_escape_string($_REQUEST["citysearch"])."' OR country='".mysql_real_escape_string($_REQUEST["citysearch"])."'";    
}
if($_REQUEST["pricefrom"]<>'' and $_REQUEST["priceto"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE price >= '".mysql_real_escape_string($_REQUEST["pricefrom"])."' AND price <= '".mysql_real_escape_string($_REQUEST["priceto"])."'".$search_citysearch;
} else if ($_REQUEST["pricefrom"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE price >= '".mysql_real_escape_string($_REQUEST["pricefrom"])."'".$search_citysearch;
} else if ($_REQUEST["priceto"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE price <= '".mysql_real_escape_string($_REQUEST["priceto"])."'".$search_citysearch;
}
else if($_REQUEST["from"]<>'' and $_REQUEST["to"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE from_date >= '".mysql_real_escape_string($_REQUEST["from"])."' AND to_date <= '".mysql_real_escape_string($_REQUEST["to"])."'".$search_citysearch;
} else if ($_REQUEST["from"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE from_date >= '".mysql_real_escape_string($_REQUEST["from"])."'".$search_citysearch;
} else if ($_REQUEST["to"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE to_date <= '".mysql_real_escape_string($_REQUEST["to"])."'".$search_citysearch;
} else {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE id>0".$search_citysearch;
}

$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
if (mysql_num_rows($sql_result)>0) {
while ($row = mysql_fetch_assoc($sql_result)) {

?>

<table width="750" border="0" style=";border:1px solid lightblue;">
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>

<tr>
<td colspan=6 height="10"></td>
</tr>

<tr>
<td rowspan=3 align="center"><?php echo $row["images"]; ?></td>
<td colspan=1 class="mycss">
<div id="containerhotel">
<div class="tabletext"><?php echo $row["hotel"]; ?></div>
</div>
</td>
<td colspan=1></td>
<td colspan=1></td>
<td colspan=1></td>
<td colspan=1 align="center" class="tabletextstar"><?php echo $row["starimages"]; ?></td>
</tr>

<tr>
<td colspan=1 class="myButton"><?php echo $row["links"]; ?></td>
<td colspan=1></td>
<td colspan=1></td>
<td colspan=1></td>
<td colspan=1 align="center"><?php echo $row["priceimages"]; ?></td>
</tr>

<tr>
<td colspan=3 class="tabletextcity" ><?php echo $row["city"]; ?></td>
<td colspan=1></td>
<td colspan=1></td>
</tr>

<tr>
<td colspan=6 height="10"></td>
</tr>

</table>
<?php
}
} else {
?>
<p>No Hotels found, Please try another search.</p>
<?php   
}

?>

1 个答案:

答案 0 :(得分:1)

通常,您可以继续做您正在做的事情。我不是100%确定你希望它如何工作,但我会假设对于每个选中的按钮,你想要找到所有项目的评级介于下一个数字和下一个数字之间。

(这是错误的代码,但是为了向您展示一般逻辑。)您可以在上面的块之后添加它。

$sql .= " AND (rating = 0 ";

if ($_REQUEST["filterstar5"] == 5) {
    $sql .= " or (rating <= 5 and rating >= 4)";
}
if ($_REQUEST["filterstar4"] -= 4) {
    $sql .= " or (rating <= 4 and rating >= 3)";
}
if ($_REQUEST["filterstar3"] == 3) {
    $sql .= " or (rating <= 3 and rating >= 2)";
}
if ($_REQUEST["filterstar2"] == 2) {
    $sql .= " or (rating <= 2 and rating >= 2)";
}
if ($_REQUEST["filterstar1"] == 1) {
    $sql .= " or (rating <= 1 and rating >= 0)";
}
$sql .= ")";