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 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题