2016-07-26 77 views
0

我有一個存儲student_name和CLASS_NAME如何進一步篩選搜索步驟的結果?

student_id | student_name | class_name 
    1  |  John |  1 
    2  |  Herry |  2 
    3  |  Peter |  1 
    4  |  Tom |  2 

我創建了一個表格在數據庫中搜索學生姓名如下表student_tb:

<form style="text-align: center;" method='POST'> 
    <input type="text" name='input_text' id='input_text' placeholder="Input text to search" required> 
    <button type="submit" name='search' value="search" ">Search</button> 
</form> 

<div class="container" style="text-align: center;"> 
    <?php 
    if(isset($_POST['search'])){ 
    $sql = "SELECT * FROM student_tb WHERE student_name LIKE '%".$_POST['input_text']."%' "; 
    $result = mysqli_query($conn, $sql); 
    while($row = mysqli_fetch_assoc($result)){?> 
     <ul> 
      <li><h3><?php echo $row["student_name"] . $row["class_name"]; ?></h3></li>    
     </ul> 
    <?php }}?> 
</div> 

假設,我將增加兩個複選框,其用於進一步過濾搜索步驟的結果。當我點擊複選框時,你能指導我如何實現它嗎?例如,我輸入'T',搜索結果爲Peter 1Tom 2。在那之後,我點擊複選框class 1,結果只剩彼得1.謝謝你這麼多

<div id="filter" style="text-align: center;"> 
<input type="checkbox" id="class1" name="class1" value="class1"> 
    <label for="class1">Class 1</label> 
<input type="checkbox" id="class2" name="class2"> 
    <label for="class2">Class2</label> 
</div> 

回答

1

簡單地定義這些複選框爲陣列(name="class[]"),給他們一個平等的價值觀,以您的CLASS_NAME ID和檢查他們同時建設您的SQL查詢:

<form style="text-align: center;" method='POST'> 
    <input type="text" name='input_text' id='input_text' placeholder="Input text to search" required> 
    <div id="filter" style="text-align: center;"> 
     <input type="checkbox" id="class1" name="class[]" value="1"> 
     <label for="class1">Class 1</label> 
     <input type="checkbox" id="class2" name="class[]" value="2"> 
     <label for="class2">Class2</label> 
    </div> 
    <button type="submit" name="search" value="search">Search</button> 
</form> 

<div class="container" style="text-align: center;"> 
    <?php 
     if (isset($_POST['search'])) { 
      dump($_POST); 
      $sql = "SELECT * FROM student_tb WHERE student_name LIKE '%" . $_POST['input_text'] . "%' "; 
      if(isset($_POST['class'])) { 
       foreach ($_POST['class'] AS $k => $v) { 
        if ($k == 0) { 
         $sql .= " AND (class_name = " . $v; 
        } else { 
         $sql .= " OR class_name = " . $v; 
        } 
       } 
       $sql .= ")"; 
      } 
      $result = mysqli_query($conn, $sql) or die(mysqli_error($conn)); 
      while ($row = mysqli_fetch_assoc($result)) { 
       ?> 
       <ul> 
        <li><h3><?php echo $row["student_name"] . $row["class_name"]; ?></h3></li> 
       </ul> 
      <?php } 
     } ?> 
</div> 

內置SQL查詢:

SELECT 
    * 
FROM 
    student_tb 
WHERE 
    student_name LIKE '%T%' 
    AND (class_name = 1 OR class_name = 2) 

請注意,如果您發送不止一個複選框,你必須將它們與OR子句結合起來,以正確地過濾數據。

UPDATE:這裏是腳本(HTML & PHP)與能力:通過串

  • 搜索;

  • 將自動提交應用複選框過濾器;

  • 保留提交後的所有值;

  • 重置表單;


<form name="searchForm" method="post"> 
    <input type="hidden" name="formSubmit" value="1"> 
    <input 
     type="text" 
     name='input_text' 
     id='input_text' 
     placeholder="Input text to search" 
     required 
     value="<?php if (isset($_POST['input_text'])) echo $_POST['input_text'] ?>"> 
    <div id="filter"> 
     <input 
      type="checkbox" 
      id="class1" 
      name="class[0]" 
      value="1" 
      onclick="this.form.submit()" <?php if (isset($_POST['class'][0])) echo $_POST['class'][0] ? 'checked' : ''; ?>> 
     <label for="class1">Class 1</label> 
     <input 
      type="checkbox" 
      id="class2" 
      name="class[1]" 
      value="2" 
      onclick="this.form.submit()" <?php if (isset($_POST['class'][1])) echo $_POST['class'][1] ? 'checked' : ''; ?>> 
     <label for="class2">Class2</label> 
    </div> 
    <button type="submit" name="search" value="search">Search</button> 
    <button type="reset" name="reset" value="reset" onclick="window.location.href = '<?php echo $_SERVER['PHP_SELF']; ?>'">Reset</button> 
</form> 

<div class="container"> 
    <?php 
     if (isset($_POST['formSubmit'])) { 
      dump($_POST); 
      $sql = "SELECT * FROM student_tb WHERE student_name LIKE '%" . $_POST['input_text'] . "%' "; 
      if (isset($_POST['class'])) { 
       $c = 0; 
       $sql .= " AND ("; 
        foreach ($_POST['class'] AS $k => $v) { 
         if($c == 0) $sql .= "class_name = " . $v; 
         else  $sql .= " OR class_name = " . $v; 
         $c++; 
        } 
       $sql .= ")"; 
      } 
      $result = mysqli_query($conn, $sql) or die(mysqli_error($conn)); 
      while ($row = mysqli_fetch_assoc($result)) { 
       ?> 
       <ul> 
        <li><h3><?php echo $row["student_name"] . $row["class_name"]; ?></h3></li> 
       </ul> 
      <?php } 
     } ?> 
</div> 

的變化是有點靜態的,可以肯定地得到改善,但你會得到的想法。

+0

謝謝。如果我選中一個框並進行搜索,它效果很好。但是,如果我沒有檢查任何框並插入文本進行搜索。它有一個小錯誤,如下所示:(注意:未定義的索引:第26行的index.php中的類012,警告:在第26行的index.php中爲foreach()提供的無效參數 警告:mysqli_fetch_assoc()期望參數1爲mysqli_result,在第35行的index.php中給出的布爾值)....在這種情況下,搜索過程只是做WHERE student_name LIKE'%「。$ _POST ['input_text']。」%'「 – user3051460

+0

是的,我們需要檢查是否有任何複選框作爲POST參數傳遞 - 檢查更新後的腳本 – mitkosoft

+0

謝謝,我明白了,它確實代碼簡單易懂,我只想問你一個問題。現場展示?例如,當我點擊複選框時,過濾結果會顯示1 – user3051460