几个月来,我一直在为一位家庭成员开发一个网站,在过去的一个月里,我一直专注于过滤SQL结果的网站功能。
这是我正在处理的页面:http://www.drivencarsales.co.uk/used-cars.php
我只是想让我的用户用页面左侧的表单过滤页面右侧列出的PHP+MySQL结果。
这是我目前的设置:
我使用以下PHP:连接到包含网站上所有车辆数据的数据库和表
<?php
try {
$db = new PDO("mysql:host=localhost;dbname=","","");
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$db->exec("SET NAMES 'utf8'");
} catch (Exception $e) {
echo "Could not connect to the database.";
exit;
}
?>
I then have another file that includes all of my SQL queries:
<?php
include('database.php');
try {
$results = $db->query("SELECT Make, Model, Colour, FuelType, Year, Mileage, Bodytype, Doors, Variant, EngineSize, Price, Transmission, PictureRefs, ServiceHistory, PreviousOwners, Options, FourWheelDrive FROM import ORDER BY Make ASC");
} catch (Exception $e) {
echo "Error.";
exit;
}
try {
$filterres = $db->query("SELECT DISTINCT Make FROM import ORDER BY Make ASC");
} catch (Exception $e) {
echo "Error.";
exit;
}
?>
当所有行都显示在表中时,第一个查询用于列出结果。
第二个查询用于表单中的"Make"select元素,它只显示表中显示的所有"Make’s",不显示重复的。
然后,我得到了HTML和PHP的块,以响应结果:
<?php include('db-affinity/filter.php'); ?>
<div class="col-md-8 col-sm-8 col-lg-8">
<?php while($row = $results->fetch(PDO::FETCH_ASSOC))
{
echo '
<div class="listing-container ' . $row["Make"] . '">
<a href="carpage.php"><h3 class="model-listing-title clearfix">'.$row["Make"].' '.$row["Model"].' '.$row["Variant"].'</h3></a>
<h3 class="price-listing">£'.number_format($row['Price']).'</h3>
</div>
<div class="listing-container-spec">
<img src="'.(explode(',', $row["PictureRefs"])[0]).'" class="stock-img-finder"/>
<div class="ul-listing-container">
<ul class="overwrite-btstrp-ul">
<li class="diesel-svg list-svg">'.$row["FuelType"].'</li>
<li class="saloon-svg list-svg">'.$row["Bodytype"].'</li>
<li class="gear-svg list-svg">'.$row["Transmission"].'</li>
<li class="color-svg list-svg">'.$row["Colour"].'</li>
</ul>
</div>
<ul class="overwrite-btstrp-ul other-specs-ul h4-style">
<li>Mileage: '.number_format($row["Mileage"]).'</li>
<li>Engine size: '.$row["EngineSize"].'cc</li>
</ul>
<button href="#" class="btn h4-style checked-btn hover-listing-btn"><span class="glyphicon glyphicon-ok"></span> History checked
</button>
<button href="#" class="btn h4-style more-details-btn hover-listing-btn tst-mre-btn"><span class="glyphicon glyphicon-list"></span> More details
</button>
<button href="#" class="btn h4-style test-drive-btn hover-listing-btn tst-mre-btn"><span class="test-drive-glyph"></span> Test drive
</button>
<h4 class="h4-style listing-photos-count"><span class="glyphicon glyphicon-camera"></span> 5 More photos</h4>
</div>
';
} ?>
正如您所看到的,它使用模板中的while循环来回显所有行。
最后但同样重要的是,我有我的表格:
<div class="container con-col-listing">
<div class="row">
<div class="col-md-4 col-sm-4">
<form class="car-finder-container dflt-container">
<h2 class="h2-finder">Car finder</h2>
<ul class="toggle-view">
<li class="li-toggle">
<h4 class="h4-finder-toggle">Make<span class="glyphicon glyphicon-plus glyph-plus-toggle"></span></h4>
<div class="panel">
<select class="form-control select-box">
<option value="make-any">Make (Any)</option>
<?php while($make = $filterres->fetch(PDO::FETCH_ASSOC))
{
echo '
<option value="'. $make["Make"].'">'.$make["Make"].'</option>
';
} ?>
</select>
<select class="form-control last-select select-box">
<option value="model-any">Model (Any)</option>
<option value="two">Two</option>
<option value="three">Three</option>
<option value="four">Four</option>
<option value="five">Five</option>
</select>
</div>
</li>
<li class="li-toggle">
<h4 class="h4-finder-toggle">Body type<span class="glyphicon glyphicon-plus glyph-plus-toggle"></span></h4>
<div class="panel">
<input id="four-by-four-checkbox" class="float-checkbox" type="checkbox"/>
<label for="four-by-four-checkbox" class="label-checkbox">4x4</label>
<input id="convertible-checkbox" class="float-checkbox" type="checkbox"/>
<label for="convertible-checkbox" class="label-checkbox">Convertible</label>
<input id="coupe-checkbox" class="float-checkbox" type="checkbox"/>
<label for="coupe-checkbox" class="label-checkbox">Coupe</label>
</div>
</li>
<li class="li-toggle">
<h4 class="h4-finder-toggle">Transmission<span class="glyphicon glyphicon-plus glyph-plus-toggle"></span></h4>
<div class="panel">
<input id="automatic-checkbox" class="float-checkbox" type="checkbox"/>
<label for="automatic-checkbox" class="label-checkbox">Automatic</label>
<input id="manual-checkbox" class="float-checkbox" type="checkbox"/>
<label for="manual-checkbox" class="label-checkbox">Manual</label>
<input id="semi-auto-checkbox" class="float-checkbox" type="checkbox"/>
<label for="semi-auto-checkbox" class="label-checkbox">Semi automatic</label>
</div>
</li>
</ul>
<button href="#" class="btn btn-block car-search-button btn-lg btn-success"><span class="glyphicon car-search-g glyphicon-search"></span> Search cars
</button>
<h4 class="h4-finder"><a href="#">Try our Smart Search </a><span class="glyphicon info-car-search-g glyphicon-info-sign"></span></h4>
</form>
</div>
您只需要查看表单的顶部,因为其余部分都不相关,它基本上是使用代码块2中的查询将所有make显示到select元素中,并再次使用while循环将每个make放入vehicle SQL表中。
所以我的问题是。。。如何使用AJAX只显示SQL表中包含在表单中选择的"Make"的行?
如果有人能花点时间向我展示一个适用于我的设置的示例,那将是非常棒的,我只熟悉PHP,并且一直在努力理解如何在我的情况下使用AJAX,我只需要一个简单的方法来更新列表。
$make = $_POST['make']; //or $_GET if that's how you roll
$query = "
SELECT make, model, etc.
FROM myTable
WHERE make = '$make'
";
这将只显示选定品牌的结果。您可以使用POST
ed HTML表单中的值重复选择过滤器的其余部分。AJAX调用可能看起来像
$.ajax({
type: "POST",
url: "path/to/php/script",
data: $('#myForm').serialize(),
dataType: "JSON",
success: function(resp) {
var response = JSON.parse(resp);
//code to output to table goes here
}
});
看起来您开始显示所有内容。因此,与其对只包含所选make的结果集发出AJAX调用,您可以考虑只使用已经显示的内容,并隐藏与make不匹配的项。这只需要在所有div上设置visible=false
,类以"listing container"开头,但与make的确切值不匹配,可能是"listing container AUDI"。
类似这样的东西:
// display only Audi makes
var myContainers = $("div[class^='listing-container ']").not(".listing-container.AUDI");
myContainers.hide();
myContainers.next(".listing-container-spec").hide();
这样做的好处是您不会再处理另一个服务器请求。然而,如果您的一整套可用工具变得如此之大,以至于不想让用户在一个页面上列出所有工具(因此您需要分页),那么您将希望按照最初的考虑进行ajax查询。
我认为这与您想要实现的目标非常相似。在此处选中将foreach值添加到Ajax