0
我通過ajax發送多個數組到php文件。我認爲它的工作正常。這是代碼。通過jQuery傳遞多個數組到php文件ajax
function call_ajax(){
var category=new Array();
$('.a:checked').each(function(i){
category[i] = $(this).val();
});
var location=new Array();
$('.b:checked').each(function(j){
location[j] = $(this).val();
});
var experience=new Array();
$('.c:checked').each(function(k){
experience[k] = $(this).val();
});
$.ajax({
type: 'post',
url: 'check3.php',
data: {cat:category, loc:location, exp:experience},
cache: false,
beforeSend: function() {
$('.a').hide();
$('.b').hide();
$('.c').hide();
},
success: function(data) {
console.log('ok');
alert(data);
}
});
}
在PHP文件I M接收它們變成seprate陣列以使動態查詢where子句 這裏是PHP文件。
<?php
include "function.php";
$cat=0;$loc=0;$exp=0;
if(isset($_POST['cat']))
{
$category=$_POST['cat'];
$length=count($category);
$cat=" catecory_filter=";
for($i=0;$i<$length;$i++)
{
if($i==$length-1){ $cat=$cat." '$category[$i]' "; }
else{ $cat=$cat." '$category[$i]' or category_filter="; }
}
}
if(isset($_POST['loc']))
{
$location=$_POST['loc'];
$length=count($location);
$loc=" location_filter=";
for($i=0;$i<$length;$i++)
{
if($i==$length-1){ $loc=$loc." '$location[$i]' "; }
else{ $loc=$loc." '$location[$i]' or location_filter="; }
}
}
if(isset($_POST['exp']))
{
$experience=$_POST['exp'];
$length=count($experience);
$exp=" experience_filter=";
for($i=0;$i<$length;$i++)
{
if($i==$length-1){ $exp=$exp." '$experience[$i]' "; }
else{ $exp=$exp." '$experience[$i]' or experience_filter="; }
}
}
//Query Construction Portion
if($cat && $loc==0 && $exp==0)
{
$result="select * from jobs_table where ($cat)";
echo "$result";
}
else if($cat==0 && $loc && $exp==0)
{
$result="select * from jobs_table where ($loc)";
echo "$result";
}
else if($cat==0 && $loc==0 && $exp)
{
$result="select * from jobs_table where ($exp)";
echo "$result";
}
else if($cat==0 && $loc && $exp)
{
$result="select * from jobs_table where ($loc) AND ($exp)";
echo "$result";
}
else if($cat && $loc && $exp==0)
{
$result="select * from jobs_table where ($cat) AND ($loc)";
echo "$result";
}
else if($cat && $loc==0 && $exp)
{
$result="select * from jobs_table where ($cat) AND ($exp)";
echo "$result";
}
else if($cat && $loc && $exp)
{
$result="select * from jobs_table where ($cat) AND ($loc) AND ($exp)";
echo "$result";
}
else if($cat==0 && $loc==0 && $exp==0)
{
$result="select * from jobs_table";
echo "$result";
}
else
{
echo "No match found";
}
?>
問題是IF語句不起作用。只有$ cat,$ loc和$ exp的每個值纔會執行Query構造區域的前三個和後一個IF語句。 請幫我解決它。 在此先感謝。
您的混合整數和文本比較。使用$ cat ='';作爲一個字符串,然後檢查使用if(strlen($ cat)== 0)http://php.net/manual/en/language.operators.comparison.php – Waygood 2013-05-01 08:00:30
非常感謝.....它工作 – user1986484 2013-05-01 08:25:52