使用 jQuery 基于多搜索栏过滤结果



我希望用户能够根据键入多个搜索栏组件来搜索作业,但在我的代码中,它可以基于一个搜索栏进行搜索。为此,我使用了两个变量 search 和 search2,它只能对搜索变量起作用。


目录

<form action="search.php" method="GET">
<input type="text" id="" class="form-control searchBar" placeholder="Designation">
<input type="text" id="" class="form-control searchBar" placeholder="City" />
<button id="searchBtn" type="button" class="btn btn-info btn-flat">Go!</button>
</form>

JavaScript

<script type="text/javascript">
$("#searchBtn").on("click", function(e) {
e.preventDefault();
var searchResult = $(".searchBar ").val();
var filter = "searchBar";
if(searchResult != "" ) {
$("#pagination").twbsPagination('destroy');
Search(searchResult, filter);
} else {
$("#pagination").twbsPagination('destroy');
Pagination();
}
});
</script>
<script type="text/javascript">
function Search(val, filter) {
$("#pagination").twbsPagination({
totalPages: <?php echo $total_pages; ?>,
visible: 5,
onPageClick: function (e, page) {
e.preventDefault();
val = encodeURIComponent(val);
$("#target-content").html("loading....");
//$("#target-content").load("search.php?page="+page+"&search="+val+"&filter="+filter); 
$("#target-content").load("search.php?page="+page+"&search="+val+"&search2="+val+"&filter="+filter);    

}
});
}
</script>

我的搜索.php页面:

<?php
session_start();
require_once("db.php");
$limit = 4;
if(isset($_GET["page"])) 
{
$page = $_GET['page'];
} 
else 
{
$page = 1;
}
$start_from = ($page-1) * $limit;
if(isset($_GET['filter']) && $_GET['filter']=='searchBar')
{
$search = $_GET['search'];
$search2 = $_GET['search2'];
$sql = "SELECT * FROM job_post INNER JOIN company ON job_post.id_company=company.id_company WHERE jobtitle LIKE '%$search%' OR city LIKE '%$search2%' LIMIT $start_from, $limit";
}
?>

我有两个输入字段,一个用于 jobtitle,一个用于城市,基于此我想要相关的输出,例如:jobtile='Software Devloper' 和 city= 'delhi',我的数据库只显示德里城市软件开发者的结果。

搜索.php:

if(isset($_GET['filter']) && $_GET['filter']=='searchBar')
{
$search = $_GET['search'];
$search2 = $_GET['search2'];
$sql = "SELECT * FROM job_post INNER JOIN company ON job_post.id_company=company.id_company WHERE jobtitle LIKE '%$search%' OR city LIKE '%$search2%' LIMIT $start_from, $limit";
}
else if(isset($_GET['filter']) && $_GET['filter']=='experience')
{
$sql = "SELECT * FROM job_post WHERE experience >='$_GET[search]' LIMIT $start_from, $limit";
}
$result = $conn->query($sql);
if($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
$sql1 = "SELECT * FROM company WHERE id_company='$row[id_company]'";
$result1 = $conn->query($sql1);
if($result1->num_rows > 0)
{
while($row1 = $result1->fetch_assoc()) 
{
?>
<div class="attachment-block clearfix">
<img class="attachment-img" src="uploads/logo/<?php echo $row1['logo']; ?>" alt="Attachment Image">
<div class="attachment-pushed">
<h4 class="attachment-heading"><a href="view_job_post.php?id=<?php echo $row['id_jobpost']; ?>"><?php echo $row['jobtitle']; ?></a> <span class="attachment-heading pull-right">$<?php echo $row['maximumsalary']; ?>/Month</span></h4>
<div class="attachment-text">
<div><strong><?php echo $row1['companyname']; ?> | <?php echo $row1['city']; ?> | Experience <?php echo $row['experience']; ?> Years</strong></div>
</div>
</div>
</div>
<?php
}
}
}
} 
}
$conn->close();
?>

最新更新