I am sending multiple array to a php file through ajax. I think its working properly. Here is code.
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);
}
});
}
On the php file im receiving them into seprate arrays to making dynamic query where clause Here is the php file.
<?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";
}
?>
Problem is the IF statements are not working. Only first three and second last IF statements of Query construction area are executed on every values of $cat,$loc and $exp. Please help me to resolve it. Thanks in advance.
This will give you the same result with less code
<?php
include "function.php";
$clauses=array();
function safe(&$variable) {
$variable=mysql_real_escape_string($variable);
return $variable;
}
if(isset($_POST['cat']))
{
$data=array_map('safe', $_POST['cat']);
$cat=" category_filter='".implode(' OR category_filter=', $data)."'";
$clauses[]='('.$cat.')';
}
if(isset($_POST['loc']))
{
$data=array_map('safe', $_POST['loc']);
$loc=" location_filter='".implode("' OR location_filter='", $data)."'";
$clauses[]='('.$loc.')';
}
if(isset($_POST['exp']))
{
$data=array_map('safe', $_POST['exp']);
$exp=" experience_filter='".implode("' OR experience_filter='", $data)."'";
$clauses[]='('.$exp.')';
}
$sql="select * from jobs_table";
if(sizeof($clauses)>0)
{
$sql.=" WHERE ".implode(" AND ", $clauses);
}
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.