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

标签 php mysql

我得到了一项使用 MySQL 代码完成的编码任务。我已经能够执行搜索按钮并且效果很好。但我也被要求使用复选框过滤搜索结果,但我做不到。 以下是 PHP 代码:

搜索到的结果显示在表格中并使用复选框,我必须根据结果的等级过滤结果。复选框是“filterstar5”等。我怎样才能做到这一点?我用谷歌搜索,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   
}

?>

最佳答案

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

(这是糟糕的代码,但可以向您展示一般逻辑。)您可以在上面的 block 之后添加它。

$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 .= ")";

关于php - 使用复选框进一步过滤 SQL 数据库结果 (PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35524504/

相关文章:

php - 在 Windows 上使用 vim-Syntastic 检查 PHP

php - Apache 在 Mavericks 上运行错误版本的 PHP

php - 如何应用 mysql 200 个连接限制?

php - 试图在 sql 查询中获取非对象的属性

javascript - Ajax 中的无限滚动

php - MySQL 夏令时信息似乎不正确。如何确保它在 Windows 中是最新的?

java - 删除 OneToMany 关系中的父级后为 "Cannot delete or update a parent row"

MySQL 在索引期间关闭 - 如何修复索引?

sql - 一对多关系的 MySQL Join 语法

mysql - 如何选择表的校验和加上mysql中的行数