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条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵