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

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....

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

Put data into array

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

Count number of elements

$searchparamscount=count($searchparams);

Do your logic using switch

switch ($searchparamscount) {
case 1:
    ...
    break;
case 2:
    ...
    break;
case 3:
    ...
   break;
}