在表单提交中获取值到数组

时间:2017-02-16 08:33:09

标签: php arrays get

目前我正在获得这样的价值......

$property_buildingorlocation = ($_GET['buildingorlocation']);

有时用户会输入......

  1. buildingname,areaname,cityname(数组大小为3)

  2. arename,cityname(数组大小为2)

  3. cityname(数组大小为1)

  4. 因此会有2个逗号,1个逗号或无逗号。

    我想将数据转换为数组,或者根据输入或逗号的数量动态设置数组的大小(上面提到的大小)

    接下来如果数组大小为3,那么我想用一个和运算符搜索三个mysql列(选择)

    如果数组大小为2,那么我想用一个和运算符搜索两个mysql列(选择)

    如果数组大小为1则在1 mysql列中搜索

    我知道我正在用这样一个悬而未决的问题推动它,但我需要帮助......我一直在努力,因为早上无法弄明白......

2 个答案:

答案 0 :(得分:1)

将数据放入数组

$searchparams=explode(',',$property_buildingorlocation);
$searchparams=('trim',$searchparams);

计算元素数量

$searchparamscount=count($searchparams);

使用switch

做你的逻辑
switch ($searchparamscount) {
case 1:
    ...
    break;
case 2:
    ...
    break;
case 3:
    ...
   break;
}

答案 1 :(得分:0)

最后最终使用了下面的逻辑......它正在运行。

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];
    }           
}