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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?