So basically I have website that has names of cities that can be checked off. I save them into local storage under one key. And then I send them Via Ajax over to my php script below. Then in php I explode the city variable on “,” and I filter the cities depending on which ones were given. But right now I only know how to do this manually, for example city[0], city[1] etc. Is there a way I can continually put those variables into mysql statement depending on how many there are? I am sorry if I am being confusing, I’m just totally lost. Any help would be appreciated.
Here is my code:
<?php
$con = mysql_connect("localhost", "test", "test") or die('could not connect to mysql');
mysql_select_db("test", $con) or die('could not find database');
$city2 = $_POST['city2'];
$cities = explode(",", $city2);
$rep2 = $_POST['rep2'];
$status2 = $_POST['status2'];
$size2 = $_POST['size2'];
$type2 = $_POST['type2'];
$lat2 = $_POST['lat2'];
$long2 = $_POST['long2'];
$radius2 = $_POST['radius2'];
if ($city2 == '') {
$city_stat = " city RLIKE '.*' ";
} else {
$city_stat = " (city='$cities[0]' OR city='$cities[1]') ";
}
if ($radius2 == '') {
$radius_stat = '10';
} else {
$radius_stat = $_POST['radius2'];
}
if ($size2 == '') {
$size_stat = '';
} else {
$size_stat = " AND size='$size2' ";
}
if ($rep2 == '') {
$rep_stat = '';
} else {
$rep_stat = " AND rep='$rep2' ";
}
if ($status2 == '') {
$status_stat = '';
} else {
$status_stat = " AND status='$status2' ";
}
$result = mysql_query("SELECT lat,lng,id,rep,rep_num,name,city,state,zip,address,status,category,size FROM test WHERE $city_stat $rep_stat $size_stat $status_stat LIMIT 0 , 50 ");
while ($array = mysql_fetch_assoc($result)) {
$array_json[] = $array;
}
echo json_encode($array_json);
?>
From what I think you are asking is that there might be a variable number of cities and you want to set a conditional that the city can be any of them.
So instead of:
You can do something like this: