所以基本上我的网站上有可以检查的城市名称。我将它们保存在一个密钥下的本地存储中。然后我通过Ajax将它们发送到我下面的PHP脚本。然后在php中我在“,”上爆炸城市变量,并根据给出的那些过滤城市。但是现在我只知道如何手动执行此操作,例如city [0],city [1]等。有没有办法可以将这些变量连续放入mysql语句中,具体取决于有多少?如果我感到困惑,我很抱歉,我只是完全迷失了。任何帮助,将不胜感激。
这是我的代码:
<?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);
?>
答案 0 :(得分:2)
我认为你要问的是,可能存在可变数量的城市,并且你想设定一个条件,即城市可以是其中任何城市。
所以而不是:
$city_stat = " (city='$cities[0]' OR city='$cities[1]') ";
您可以这样做:
$city_stat = '(city IN ('.implode(',',$cities).'))';