如何创建搜索过滤器,例如 Facebook 上的查找好友页面。我在表帐户中有3列体型、种族类型、肤色。我已经为我的页面创建了 View ,其中的复选框现在不会过滤值。它显示我在没有过滤的情况下检查的所有值。我只需要,如果 bodybuild=1 ANDethictype=2ANDskincolor=1,我想显示在其行中具有此值的用户的名称。
这是我现在使用的代码,
这是新的更新代码,
if(isset($_REQUEST['search'])){
$pro_price = $_REQUEST['pro_price'];
$pro_code = $_REQUEST['pro_code'];
$hsn_code = $_REQUEST['hsn_code'];
foreach ($_REQUEST['pro_price'] as $pro_price) {
$statearray[] = mysql_real_escape_string($pro_price);
}
foreach ($_REQUEST['pro_code'] as $pro_code) {
$codesarray[] = mysql_real_escape_string($pro_code);
}
foreach ($_REQUEST['hsn_code'] as $hsn_code) {
$hsnarray[] = mysql_real_escape_string($hsn_code);
}
$states = implode ("','", $statearray);
$codes = implode ("','", $codesarray);
$hsn = implode ("','", $hsnarray);
$sql = "SELECT * FROM addproduct WHERE pro_price IN ('$states') OR pro_code IN ('$codes') OR hsn_code IN ('$hsn')";
$result = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($result) == 0)
{
echo "Sorry, but we can not find an entry to match your query...<br><br>";
}
else
{
echo "<table border='1' width='900' class='srchrslt'>
<tr class='head'>
<td>pro_name</td>
<td>pro_brand</td>
<td>hsn_code</td>
<td>pro_price</td>
<td>pro_code</td>
</tr>";
while($row = mysql_fetch_assoc( $result ))
{
echo "<tr>";
echo "<td>" . $row['pro_name'] . " </td>";
echo "<td>" . $row['pro_brand'] . " </td>";
echo "<td>" . $row['hsn_code'] . " </td>";
echo "<td>" . $row['pro_price'] . " </td>";
echo "<td>" . $row['pro_code'] . " </td>";
echo "</tr>";
}
echo "</table>";
}}
例如,如果我选择 pro_code 为 101,hsn_code 为 101101,pro_price 为 250,我想在此处查看 View Sonic LCD 作为我的数据库,
我需要过滤我的数据值,就像我上面所说的那样,如果 bodybuild=1 ANDethictype=4ANDskincolor=6,我想显示该表中用户的日期。列的值可以根据我们对复选框的选择进行更改
最佳答案
最后在AJAX的帮助下我得到了问题的解决方案。感谢所有支持我的人。我正在为更多候选人发布代码。
index.php
<body>
<h1>Demo</h1>
<table id="employees"><thead>
<tr><th>Name</th>
<th>BodyBuild</th>
<th>EthnicType</th>
<th>Skincolor</th></tr>
</thead><tbody></tbody>
</table>
<div id="filter">
<h2>Filter options</h2>
<div>
<h4>Body Build</h4>
<input type="checkbox" id="car" name="sixpack">
<label for="car">Six Pack</label>
</div>
<div>
<input type="checkbox" id="car" name="fat">
<label for="car">Fat</label>
</div>
<div>
<input type="checkbox" id="car" name="thin">
<label for="car">Thin</label>
</div>
<div>
<h4>Ethnic Type</h4>
<input type="checkbox" id="language" name="arab">
<label for="language">Arab</label>
</div>
<div>
<input type="checkbox" id="language" name="indian">
<label for="language">Indian</label>
</div>
<div>
<h4>Skin Color</h4>
<input type="checkbox" id="nights" name="black">
<label for="nights">Black</label>
</div>
<div>
<input type="checkbox" id="nights" name="white">
<label for="nights">White</label>
</div>
</div>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
function makeTable(data){
var tbl_body = "";
$.each(data, function() {
var tbl_row = "";
$.each(this, function(k , v) {
tbl_row += "<td>"+v+"</td>";
})
tbl_body += "<tr>"+tbl_row+"</tr>";
})
return tbl_body;
}
function getEmployeeFilterOptions(){
var opts = [];
$checkboxes.each(function(){
if(this.checked){
opts.push(this.name);
}
});
return opts;
}
function updateEmployees(opts){
$.ajax({
type: "POST",
url: "search",
dataType : 'json',
cache: false,
data: {filterOpts: opts},
success: function(records){
$('#employees tbody').html(makeTable(records));
}
});
}
var $checkboxes = $("input:checkbox");
$checkboxes.on("change", function(){
var opts = getEmployeeFilterOptions();
updateEmployees(opts);
});
updateEmployees();
</script>
</body>
search.php
<?php
$pdo = new PDO('mysql:host=localhost;dbname=castingkall', 'root', '');
$select = 'SELECT name,bodybuild,ethnictype,skincolor';
$from = ' FROM accounts';
$where = ' WHERE TRUE';
$opts = isset($_POST['filterOpts'])? $_POST['filterOpts'] : array('');
if (in_array("sixpack", $opts)){
$where .= " AND bodybuild = 1";
}
if (in_array("fat", $opts)){
$where .= " AND bodybuild = 2";
}
if (in_array("thin", $opts)){
$where .= " AND bodybuild = 3";
}
if (in_array("arab", $opts)){
$where .= " AND ethnictype = 4";
}
if (in_array("indian", $opts)){
$where .= " AND ethnictype = 5";
}
if (in_array("black", $opts)){
$where .= " AND skincolor = 6";
}
if (in_array("white", $opts)){
$where .= " AND skincolor = 7";
}
$sql = $select . $from . $where;
$statement = $pdo->prepare($sql);
$statement->execute();
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($results);
echo($json);
?>
只需根据您的数据更改数据库名称、表名称和列名称即可。
希望您觉得这很有用...
关于php - 从数据库 Likr facebook **查找 friend ** 页面搜索过滤,PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45856319/