doujiekeyan0622 2017-03-17 09:49
浏览 46
已采纳

如何通过使用多选值HTML来忽略MySQL-Statement中未设置的数组

How do I ignore empty values in MySQL-Statement?

Like if e.g. the Manfacturer Name A and Name B are selected, Color Red is selected and the Price is ignored(value is empty).

HTML-Part

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <div class="container">
    <div class="row">
      <div class="col-xs-8">
        <select multiple class="form-control" name="multiMan[]">
                <option value="" selected> Manufacturer </option> 
                <option value="A"> Name A </option> 
                <option value="B"> Name B </option> 
                <option value="C"> Name C </option> 
              </select>
        <br><br>
        <select multiple class="form-control" name="multiCol[]">
                <option value="" selected> Select color... </option>
                <option value="red">    Red </option>
                <option value="blue">   Blue </option>
                <option value="green">  Green </option>
              </select>
        <br><br>
        <select multiple class="form-control" name="multiPri[]">
                <option value="" selected> Price... </option>
                <option value="100"> 100 </option>
                <option value="200"> 200 </option>
                <option value="300"> 300 </option>
              </select>
        <br><br>
        <button type="submit" name="submit_filter" class="btn"> GO </button>
      </div>
    </div>
  </div>
</form>
<center>

PHP-Part

<center>
   <?php
     if(isset($_POST["submit_filter"])){ 
      function splitArray($element){
        return "'$element'";
      }

      $array1         = $_POST['multiMan'];
      $searchMultiMan = implode(',', array_map('splitArray', $array1));

      $array2         = $_POST['multiCol'];
      $searchMultiCol = implode(',', array_map('splitArray', $array2));

      $array3         = $_POST['multiPri'];
      $searchMultiPri = implode(',', array_map('splitArray', $array3));

      echo $searchMultiMan . "<br>";
      echo $searchMultiCol . "<br>";
      echo $searchMultiPri . "<br>";

      $multiFilter =  "SELECT     data.manufacturer, data.color, data.price
                      FROM        data
                      WHERE       data.manufacturer IN ($searchMultiMan) 
                      AND         data.color        IN ($searchMultiCol)
                      AND         data.price        IN ($searchMultiPri);";

      echo $multiFilter;

      $multiFilterErg = mysqli_query($db, $multiFilter);    
     }
    ?>
  </center>

The problem here is, that the query is searching for a price with an empty entry. What I want to do is that if the price is not setted to ignore this and show all entries with Name A and B and color red and all prices.

</div>
  • 写回答

1条回答 默认 最新

  • duanjiwang2927 2017-03-17 09:57
    关注

    So, you need to only return non-empty elements from $_POST inside of splitArray(), check for string length to build the $where array, and only include the WHERE clause if there is something to filter with.

    Complete Code :

    function splitArray($element){
        if($element){
            return "'$element'";
        }
    }
    
    $searchMultiMan=implode(',',array_map('splitArray',$_POST['multiMan']));
    $searchMultiCol=implode(',',array_map('splitArray',$_POST['multiCol']));
    $searchMultiPri=implode(',',array_map('splitArray',$_POST['multiPri']));
    
    $where=[];
    if(strlen($searchMultiMan)>0){$where[]="data.manufacturer IN ($searchMultiMan)";}
    if(strlen($searchMultiCol)>0){$where[]="data.color IN ($searchMultiCol)";}
    if(strlen($searchMultiPri)>0){$where[]="data.price IN ($searchMultiPri)";}
    
    $multiFilter="SELECT data.manufacturer, data.color, data.price FROM data";
    if(sizeof($where)>0){
        $multiFilter.=" WHERE ".implode(' AND ',$where);
    }
    echo $multiFilter;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算