我已经在一个站点中实现了一个搜索栏,该站点将转到数据库并从中获取结果,但现在我想为该搜索添加一个过滤器,以便您可以按一组类别进行过滤(也在这个数据库中定义)。 如何获取Filter Dropdown中的参数并将它们传递给PHP搜索?
到目前为止,我的HTML看起来像这样:
<div class="form-group">
<div class="input-group-btn search-panel">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
<span id="search_concept">Filtrar</span> <span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">
<?php print("$salida");?>
<li><a href="#" data-value="action">Todo</a></li>
</ul>
<input type="hidden" name="search_param" value="all" id="search_param">
<input type="text" class="form-control" name="searchinv" placeholder="Buscar" onkeydown="searchq();">
</div>
</div>
javascript是:
$(document).ready(function(e){
$('.search-panel .dropdown-menu').find('a').click(function(e) {
e.preventDefault();
var param = $(this).attr("href").replace("#","");
var concept = $(this).text();
$('.search-panel span#search_concept').text(concept);
$('.input-group #search_param').val(param);
});
});
function searchq(){
var searchTxt = $("input[name='searchinv']").val();
$.post("search.php",{searchVal: searchTxt}, function(output){
$("#output").html(output);
});
}
PHP:
if(isset($_POST['searchVal'])){
$searchq = $_POST['searchVal'];
$query = $mysqli->query("SELECT * FROM product WHERE name_product LIKE '%".$searchq."%'");
if($query->num_rows){
$rows = $query->fetch_all(MYSQLI_ASSOC);
forEach($rows as $row){
$nprod = $row['name_product'];
$id = $row['id_product'];
$output .= '<div> '.$nprod.' '.$id.'</div>';
}
}
答案 0 :(得分:1)
从数据库中选择所有类别,然后在视图中循环浏览它们作为选项中的选项。像这样:
<select id='search-filter' name='filter'>
<!--$categories is an array containing all your available filters-->
<?php foreach($categories as $category):?>
<option value='<?php echo $category;?>'><?php echo $category;?></option>
<?php endforeach;?>
</select>
这将创建一个下拉列表,其中包含过滤器作为选项。
然后在您的javascript中编辑searchq()
函数,以便它也在select字段中发布值:
function searchq(){
var searchTxt = $("input[name='searchinv']").val();
var filter = $('search-filter').val();
$.post("search.php",{searchVal: searchTxt,filter:filter}, function(output){
$("#output").html(output);
});
}
在你的php中为你的查询添加过滤器。如果所有过滤器值都适用于同一列,则很容易:
if(isset($_POST['searchVal'])){
$searchq = $_POST['searchVal'];
if(isset($_POST['filter'])) {
$filter = $_POST['filter'];
$query = $mysqli->query("SELECT * FROM product WHERE name_product LIKE '%{$searchq}%' AND filter_col = '{$filter}'");
} else {
$query = $mysqli->query("SELECT * FROM product WHERE name_product LIKE '%{$searchq}%'");
}
}
...
如果每个过滤器都适用于不同的列,那么我会将它们作为一组键值对发送出去。循环遍历该数组会将查询附加到where where子句。像这样:
$filters = json_decode($_POST['filters']);//send the variable as a json encoded array
$sql_query = "SELECT * FROM product WHERE name_product LIKE '%{$searchq}%' ";
foreach($filters as $col=>$val) {
$sql_query .= " AND {$col} = {$val} ";
}
$query = $mysqli->query($sql_query);
...
当然,如果是这种情况,您需要保存每个过滤器在数据库中应用的列,然后在每个选择选项上将其作为数据属性打印,在选择输入中选择多个选项,并在javascript build和json编码选项以发布到后端。
另外注意 - 不要忘记在后端转义数据以防止sql注入:https://www.owasp.org/index.php/SQL_Injection_Prevention_Cheat_Sheet
希望有所帮助,祝你好运!