dqt20140129 2019-04-08 10:53
浏览 48
已采纳

每个循环PHP的多输入和多列

Currently taking all get request through foreach function. Looking if column called season contains them. Next step is adding 1 more column to check if any get request is LIKE any of the next column values. The second column has to be an AND and not and OR, which means if the first column season contains any of the get requests AND the second column contains any of the GET requests.

Currently:

$array_name = array();
foreach ($_GET as $key => $value) {
    $array_name[] = "'%" . escape_string($value) . "%'";
};

$string = implode(' OR season LIKE ', $array_name);

$tank = "SELECT * FROM shrubs2 WHERE season LIKE {$string}";
echo $tank;          

First edit:

      function searchByColumn($values, $columnName) {
      $string = implode(" OR $columnName LIKE ", $values);
      return "SELECT * FROM shrubs2 WHERE $columnName LIKE $string";
  }

  $array_name = array();
  foreach ($_GET as $key => $value) {
      $array_name[] = "'%".escape_string($value)."%'";
  }

  $colNames = array("season", "日照"); // can add here more column names
  foreach($colNames as $colName) {
      $str = searchByColumn($array_name, $colName);
  }

  echo $str;


 ///// creating the query with the variable $str
  • 写回答

1条回答 默认 最新

  • dtypj3308 2019-04-08 11:10
    关注

    You can define function to search for each column:

    function searchByColumn($values, $columnName) {
        $string = implode(" OR $columnName LIKE ", $values);
        return "SELECT * FROM shrubs2 WHERE $columnName LIKE $string";
    } 
    

    Then use it as:

    $array_name = array();
    foreach ($_GET as $key => $value) {
        $array_name[] = "'%".escape_string($value)."%'";
    }
    
    $colNames = array("season"); // can add here more column names
    foreach($colNames as $colName) {
        $str = searchByColumn($array_name, $colName);
        echo $str; // or run it
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?