Php 在表单提交时将值放入数组
目前我得到的值是这样的Php 在表单提交时将值放入数组,php,arrays,get,Php,Arrays,Get,目前我得到的值是这样的 $property_buildingorlocation = ($_GET['buildingorlocation']); 有时用户会输入 buildingname、areaname、cityname(数组大小为3) arename、cityname(数组大小为2) cityname(数组大小为1) 因此将有2个逗号,1个逗号或没有逗号 我想将数据放入数组,根据输入的数量或逗号(上面提到的大小)动态设置数组的大小 接下来,如果数组大小是3,那么我想用and运算符搜索三个
$property_buildingorlocation = ($_GET['buildingorlocation']);
有时用户会输入
$searchparams=explode(',',$property_buildingorlocation);
$searchparams=('trim',$searchparams);
计算元素的数量
$searchparamscount=count($searchparams);
你的逻辑使用
最后使用了下面的逻辑。。。它正在工作
if (!empty($property_buildingorlocation)) {
$searchparams = array_map('trim', explode(',', $property_buildingorlocation));
$searchparamscount=count($searchparams);
// If Property Buildingname, Areaname and City are given
if ($searchparamscount == 3) {
$wheres[] = 'property_buildingname LIKE :property_buildingname AND property_areaname LIKE :property_areaname AND property_city LIKE :property_city';
$params[':property_buildingname'] = $searchparams[0];
$params[':property_areaname'] = $searchparams[1];
$select7 = $con->prepare("SELECT city_id, city_name from tbl_city WHERE city_name LIKE (:property_city)");
$select7->setFetchMode(PDO::FETCH_ASSOC);
$select7->bindParam(':property_city', $searchparams[2], PDO::PARAM_STR);
$select7->execute();
while($data7=$select7->fetch()){
$searchparams[2] = $data7['city_id'];
}
$params[':property_city'] = $searchparams[2];
// If Property Areaname and City are given
} else if ($searchparamscount == 2) {
$wheres[] = 'property_areaname LIKE :property_areaname AND property_city LIKE :property_city';
$params[':property_areaname'] = $searchparams[0];
$select7 = $con->prepare("SELECT city_id, city_name from tbl_city WHERE city_name LIKE (:property_city)");
$select7->setFetchMode(PDO::FETCH_ASSOC);
$select7->bindParam(':property_city', $searchparams[1], PDO::PARAM_STR);
$select7->execute();
while($data7=$select7->fetch()){
$searchparams[1] = $data7['city_id'];
}
$params[':property_city'] = $searchparams[1];
}
// If Property City is given
else if ($searchparamscount == 1) {
$wheres[] = 'property_city LIKE :property_city';
$select7 = $con->prepare("SELECT city_id, city_name from tbl_city WHERE city_name LIKE (:property_city)");
$select7->setFetchMode(PDO::FETCH_ASSOC);
$select7->bindParam(':property_city', $searchparams[0], PDO::PARAM_STR);
$select7->execute();
while($data7=$select7->fetch()){
$searchparams[0] = $data7['city_id'];
}
$params[':property_city'] = $searchparams[0];
}
}
谢谢@Alexey。。。最终使用了非常有用的逻辑您总是使用$searchparams[0],您确定这样做有效吗?可能是$searchparams[0]、$searchparams[1]和$searchparams[2]?是的,我修复了它,谢谢你指出。。它出现在testingcode中,现在变得越来越复杂,但我已经发布了正确的答案
if (!empty($property_buildingorlocation)) {
$searchparams = array_map('trim', explode(',', $property_buildingorlocation));
$searchparamscount=count($searchparams);
// If Property Buildingname, Areaname and City are given
if ($searchparamscount == 3) {
$wheres[] = 'property_buildingname LIKE :property_buildingname AND property_areaname LIKE :property_areaname AND property_city LIKE :property_city';
$params[':property_buildingname'] = $searchparams[0];
$params[':property_areaname'] = $searchparams[1];
$select7 = $con->prepare("SELECT city_id, city_name from tbl_city WHERE city_name LIKE (:property_city)");
$select7->setFetchMode(PDO::FETCH_ASSOC);
$select7->bindParam(':property_city', $searchparams[2], PDO::PARAM_STR);
$select7->execute();
while($data7=$select7->fetch()){
$searchparams[2] = $data7['city_id'];
}
$params[':property_city'] = $searchparams[2];
// If Property Areaname and City are given
} else if ($searchparamscount == 2) {
$wheres[] = 'property_areaname LIKE :property_areaname AND property_city LIKE :property_city';
$params[':property_areaname'] = $searchparams[0];
$select7 = $con->prepare("SELECT city_id, city_name from tbl_city WHERE city_name LIKE (:property_city)");
$select7->setFetchMode(PDO::FETCH_ASSOC);
$select7->bindParam(':property_city', $searchparams[1], PDO::PARAM_STR);
$select7->execute();
while($data7=$select7->fetch()){
$searchparams[1] = $data7['city_id'];
}
$params[':property_city'] = $searchparams[1];
}
// If Property City is given
else if ($searchparamscount == 1) {
$wheres[] = 'property_city LIKE :property_city';
$select7 = $con->prepare("SELECT city_id, city_name from tbl_city WHERE city_name LIKE (:property_city)");
$select7->setFetchMode(PDO::FETCH_ASSOC);
$select7->bindParam(':property_city', $searchparams[0], PDO::PARAM_STR);
$select7->execute();
while($data7=$select7->fetch()){
$searchparams[0] = $data7['city_id'];
}
$params[':property_city'] = $searchparams[0];
}
}