我正在尝试根据用户combobox
搜索输入显示记录。这个ajax
编码工作正常。我对这个编码有一个小问题。例如,我从年份开始选择2014年。它将显示2014年发布的所有汽车。然后我从国家选择印度。现在它应该展示2014年发布的所有汽车和印度制造。但它显示了2014年发布的所有汽车以及印度制造的所有汽车。
由于query
( b.officially_released_year = '".$pass_year."' OR b.country_of_origin = '".$pass_country."' OR b.bodystyle = '".$pass_bodystyle."')
上的这一行。我已将OR更改为AND。但如果我从年份组合框输入中选择2014年,则不会显示2014年的汽车。如果我想从数据库获得正确的结果,如何更改此查询?
ajax代码
<script>
$(function(){
$('#search_year, #search_country, #search_bodystyle').on('change', function(){
var year = $('#search_year').val();
var country = $('#search_country').val();
var bodystyle = $('#search_bodystyle').val();
if(year || country || bodystyle){
$('#loader').show();
$.ajax({
type: "POST",
url: "<?=get_url($topic, CAR_FINDER_SEARCH)?>", //or your php page
data: { year: year, country: country, bodystyle: bodystyle }
})
.done(function(response) {
$('#car_finder_results').html(response);
$('#loader').hide();
});
}
});
});
</script>
php页面
<?php
$pass_year = $_POST['year'];
$pass_country = $_POST['country'];
$pass_bodystyle = $_POST['bodystyle'];
$sql_search = "SELECT b.model_name, b.maker_url, b.model_url FROM ".TBL_CAR_ADD_MODELS." a, ".TBL_CAR_SPEC_GENERAL." b WHERE a.model_id = b.model_id AND a.model_status = '1' AND ( b.officially_released_year = '".$pass_year."' OR b.country_of_origin = '".$pass_country."' OR b.bodystyle = '".$pass_bodystyle."')";
$result = mysql_query($sql_search, $CN);
if(mysql_num_rows($result) > 0)
{
while($get_search = mysql_fetch_array($result))
{
?>
<div class="all_list_imgs_anc" style="display:inline-block;">
<a href="<?=asort_get_url(CAR_MAKE, $get_search['maker_url'], $get_search['model_url'])?>" class="img_anc_list">
<span class="model_names"><?=$get_search['model_name'];?></span>
<img src="<?=fa_model_image_path("medium", $get_search['maker_url'],$get_search['model_url'])?>" width="200" height="190" alt="<?=$get_search['model_name'];?>">
</a>
</div>
<?php
}
}
else
{
echo "<div style='margin-top: 25px; text-align: center; width: 900px; display: inline-block;'>No Car Models</div>";
}
?>
答案 0 :(得分:1)
试试这个:
<?php
$pass_year = $_POST['year'];
$pass_country = $_POST['country'];
$pass_bodystyle = $_POST['bodystyle'];
if($pass_country=='')
{
$sql_search = "SELECT b.model_name, b.maker_url, b.model_url FROM ".TBL_CAR_ADD_MODELS." a, ".TBL_CAR_SPEC_GENERAL." b WHERE a.model_id = b.model_id AND a.model_status = '1' AND b.officially_released_year = '".$pass_year."' OR b.country_of_origin = '".$pass_country."' AND b.bodystyle = '".$pass_bodystyle."'";
}
else
$sql_search = "SELECT b.model_name, b.maker_url, b.model_url FROM ".TBL_CAR_ADD_MODELS." a, ".TBL_CAR_SPEC_GENERAL." b WHERE a.model_id = b.model_id AND a.model_status = '1' AND b.officially_released_year = '".$pass_year."' AND b.country_of_origin = '".$pass_country."' AND b.bodystyle = '".$pass_bodystyle."'";
$result = mysql_query($sql_search, $CN);
if(mysql_num_rows($result) > 0)
{
while($get_search = mysql_fetch_array($result))
{
?>
<div class="all_list_imgs_anc" style="display:inline-block;">
<a href="<?=asort_get_url(CAR_MAKE, $get_search['maker_url'], $get_search['model_url'])?>" class="img_anc_list">
<span class="model_names"><?=$get_search['model_name'];?></span>
<img src="<?=fa_model_image_path("medium", $get_search['maker_url'],$get_search['model_url'])?>" width="200" height="190" alt="<?=$get_search['model_name'];?>">
</a>
</div>
<?php
}
}
else
{
echo "<div style='margin-top: 25px; text-align: center; width: 900px; display: inline-block;'>No Car Models</div>";
}
?>