douyanxing6054 2017-02-16 08:33 采纳率: 0%
浏览 27
已采纳

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

Currently i am getting the value like this...

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

Some times the user will input......

  1. buildingname, areaname, cityname (array size to be 3)

  2. arename, cityname (array size to be 2)

  3. cityname (array size to be 1)

hence there will be 2 commas, 1 comma or no comma.

I want to get the data into array, either dynamically set the size of the array depending on number of inputs or commas (sizes mentioned above)

next if array size is 3, then i want to search in three mysql columns (of choice) with an and operator

if array size is 2 then i want to search in two mysql columns (of choice) with an and operator

if array size is 1 then search in 1 mysql column

i know i am pushing it with such an open question, but i need help... i have been at it since morning can't figure it out....

  • 写回答

2条回答 默认 最新

  • donglu9872 2017-02-16 10:05
    关注

    Finally ended up using the logic below... it is working.

    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];
        }           
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?