如何进一步过滤搜索步骤的结果?

时间:2016-07-26 09:10:34

标签: javascript php ajax mysqli phpmyadmin

我有一个表student_tb,用于存储student_name和class_name

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

我创建了一个表单来搜索数据库中的学生姓名,如下所示:

<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 个答案:

答案 0 :(得分: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子句组合才能正确过滤数据。

更新:以下是能够执行以下操作的脚本(HTML&amp; 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>

这种变化有点静,可以肯定地改进,但你会明白这一点。