简体   繁体   English

使用多个 PHP 下拉列表过滤 MySQL 表

[英]Using Multiple PHP Drop Downs To Filter MySQL Table

Heres a pic of my database table -> MySQL-Table这是我的数据库表的图片-> MySQL-Table

Im trying to create a search page with drop down selectors to filter through each of the last 8 columns.我试图创建一个带有下拉选择器的搜索页面,以过滤最后 8 列中的每一列。 I want the the drop down selectors to be able to select multiple entries (I have this working already) I also want them to preload values from data already entered into my table columns.我希望下拉选择器能够 select 多个条目(我已经有了这个工作)我还希望它们从已经输入到我的表列中的数据中预加载值。 (I also got this working with the help of tutorials... although I admit I don't fully understand how this part works) (我也在教程的帮助下完成了这个工作......虽然我承认我不完全理解这部分是如何工作的)

Using these tutorials i've created a php page that contains 8 drop down selectors that automatically pull values from their respective columns.使用这些教程,我创建了一个 php 页面,其中包含 8 个下拉选择器,它们会自动从各自的列中提取值。 Id like to be able to filter results from my table using either all (or some) of these column filters.我希望能够使用这些列过滤器中的所有(或部分)过滤器从我的表中过滤结果。 For Example... Say I want to show all entries that fall under Genre=Metal, AND KeySig=E minor, AND Tempo=Fast... I might use a mysql command like mysql> SELECT id, NameUrl, Genre, KeySig, TimeSig, Tempo, Tuning, EntType, Recording, RecYear FROM test_table WHERE Genre = 'Metal' AND KeySig = 'E minor' AND Tempo = 'Fast';例如...假设我想显示属于 Genre=Metal, AND KeySig=E minor, AND Tempo=Fast... TimeSig, Tempo, Tuning, EntType, Recording, RecYear FROM test_table WHERE Genre = 'Metal' AND KeySig = 'E minor' AND Tempo = 'Fast';

Essentially i'm trying to do the same thing via a php webpage.本质上,我试图通过 php 网页做同样的事情。 With the code I have right now only my first drop down selector "Genre" actually filters through anything.使用我现在拥有的代码,只有我的第一个下拉选择器“流派”实际上可以过滤任何内容。 The rest of the filters are just there.. they're not set up to do anything yet.过滤器的 rest 就在那儿……它们还没有准备好做任何事情。 I need help pulling $_POST requests from my remaining drop downs and coming up with code that will filter through my columns using multiple variables via the AND operator.我需要帮助从剩余的下拉菜单中提取 $_POST 请求,并提出代码,该代码将通过 AND 运算符使用多个变量过滤我的列。

I hope this makes sense... Im not much of a computer guy.. more of a musician.我希望这是有道理的……我不是一个电脑专家……更像是一个音乐家。 Building this as a tool to help me out with my writing workflow.将其构建为帮助我完成写作工作流程的工具。 Hope someone can help - Thanks希望有人可以提供帮助-谢谢

DBController.php DBController.php

     <?php
 class DBController {
     private $host = "localhost";
     private $user = "root";
     private $password = "password";
     private $database = "test";
     private $conn;

         function __construct() {
         $this->conn = $this->connectDB();
          } 
     function connectDB() {
         $conn = mysqli_connect($this->host,$this->user,$this->password,$this->database);
         return $conn;
     }
         function runQuery($query) {
                 $result = mysqli_query($this->conn,$query);
                 while($row=mysqli_fetch_assoc($result)) {
                 $resultset[] = $row;
                 }      
                 if(!empty($resultset))
                 return $resultset;
     }
 }
 ?>

testsearch.php testsearch.php

        <?php
 include 'DBController.php';
 $db_handle = new DBController();
 $GenreResult = $db_handle->runQuery("SELECT DISTINCT Genre FROM test_table ORDER BY Genre ASC");
 $TempoResult = $db_handle->runQuery("SELECT DISTINCT Tempo FROM test_table ORDER BY Tempo ASC");
 $KeySigResult = $db_handle->runQuery("SELECT DISTINCT KeySig FROM test_table ORDER BY KeySig ASC");
 $TimeSigResult = $db_handle->runQuery("SELECT DISTINCT TimeSig FROM test_table ORDER BY TimeSig ASC");
 $TuningResult = $db_handle->runQuery("SELECT DISTINCT Tuning FROM test_table ORDER BY Tuning ASC");
 $EntTypeResult = $db_handle->runQuery("SELECT DISTINCT EntType FROM test_table ORDER BY EntType ASC");
 $RecordingResult = $db_handle->runQuery("SELECT DISTINCT Recording FROM test_table ORDER BY Recording ASC");
 $RecYearResult = $db_handle->runQuery("SELECT DISTINCT RecYear FROM test_table ORDER BY RecYear ASC");
 ?>
 <html>
 <head>
 <link href="style.css" type="text/css" rel="stylesheet" />
 <title>Riff Bank - Search & Upload</title>
 </head>
 <body>
     <h2>Riff Bank - Search & Upload</h2>
     <form method="POST" name="Genre" action="testsearch.php">
         <div id="demo-grid">
             <div class="search-box">
                 <select id="Place" name="Genre[]" multiple="multiple">
                     <option value="0" selected="selected">Select Genre</option>
                     <form method="POST" name="search" action="testsearch.php">
                         <?php
                         if (! empty($GenreResult)) {
                             foreach ($GenreResult as $key => $value) {
                                 echo '<option value="' . $GenreResult[$key]['Genre'] . '">' . $GenreResult[$key]['Genre'] . '</option>';
                             }
                         }
                         ?>
                 </select><br> <br>
                 <form method="POST" name="search" action="testsearch.php">
         <div id="demo-grid">
             <div class="search-box">
                 <select id="Place" name="KeySig[]" multiple="multiple">
                     <option value="0" selected="selected">Select Key</option>
                     <form method="POST" name="search" action="testsearch.php">
                         <?php
                         if (! empty($KeySigResult)) {
                             foreach ($KeySigResult as $key => $value) {
                                 echo '<option value="' . $KeySigResult[$key]['Tempo'] . '">' . $KeySigResult[$key]['KeySig'] . '</option>';
                             }
                         }
                         ?>
                 </select><br> <br>
                 <form method="POST" name="search" action="testsearch.php">
         <div id="demo-grid">
             <div class="search-box">
                 <select id="Place" name="TimeSig[]" multiple="multiple">
                     <option value="0" selected="selected">Select TIme Signature</option>
                     <form method="POST" name="search" action="testsearch.php">
                         <?php
                         if (! empty($TimeSigResult)) {
                             foreach ($TimeSigResult as $key => $value) {
                                 echo '<option value="' . $TimeSigResult[$key]['TimeSig'] . '">' . $TimeSigResult[$key]['TimeSig'] . '</option>';
                             }
                         }
                         ?>
                 </select><br> <br>
                 <form method="POST" name="search" action="index.php">
         <div id="demo-grid">
             <div class="search-box">
                 <select id="Place" name="Tempo[]" multiple="multiple">
                     <option value="0" selected="selected">Select Tempo</option>
                     <form method="POST" name="search" action="index.php">
                         <?php
                         if (! empty($TempoResult)) {
                             foreach ($TempoResult as $key => $value) {
                                 echo '<option value="' . $TempoResult[$key]['Tempo'] . '">' . $TempoResult[$key]['Tempo'] . '</option>';
                             }
                         }
                         ?>
                 </select><br> <br>
                 <form method="POST" name="search" action="testsearch.php">
         <div id="demo-grid">
             <div class="search-box">
                 <select id="Place" name="Tuning[]" multiple="multiple">
                     <option value="0" selected="selected">Select Tuning</option>
                     <form method="POST" name="search" action="testsearch.php">
                         <?php
                         if (! empty($TuningResult)) {
                             foreach ($TuningResult as $key => $value) {
                                 echo '<option value="' . $TuningResult[$key]['Tuning'] . '">' . $TuningResult[$key]['Tuning'] . '</option>';
                             }
                         }
                         ?>
                 </select><br> <br>
                 <form method="POST" name="search" action="testsearch.php">
         <div id="demo-grid">
             <div class="search-box">
                 <select id="Place" name="EntType[]" multiple="multiple">
                     <option value="0" selected="selected">Select Entry Type</option>
                     <form method="POST" name="search" action="testsearch.php">
                         <?php
                         if (! empty($EntTypeResult)) {
                             foreach ($EntTypeResult as $key => $value) {
                                 echo '<option value="' . $EntTypeResult[$key]['EntType'] . '">' . $EntTypeResult[$key]['EntType'] . '</option>';
                             }
                         }
                         ?>
                 </select><br> <br>
                 <form method="POST" name="search" action="testsearch.php">
         <div id="demo-grid">
             <div class="search-box">
                 <select id="Place" name="Recording[]" multiple="multiple">
                     <option value="0" selected="selected">Select Recording Type</option>
                     <form method="POST" name="search" action="testsearch.php">
                         <?php
                         if (! empty($RecordingResult)) {
                             foreach ($RecordingResult as $key => $value) {
                                 echo '<option value="' . $RecordingResult[$key]['Recording'] . '">' . $RecordingResult[$key]['Recording'] . '</option>';
                             }
                         }
                         ?>
                 </select><br> <br>
                 <form method="POST" name="search" action="index.php">
         <div id="demo-grid">
             <div class="search-box">
                 <select id="Place" name="RecYear[]" multiple="multiple">
                     <option value="0" selected="selected">Select Year</option>
                     <form method="POST" name="search" action="index.php">
                         <?php
                         if (! empty($RecYearResult)) {
                             foreach ($RecYearResult as $key => $value) {
                                 echo '<option value="' . $RecYearResult[$key]['RecYear'] . '">' . $RecYearResult[$key]['RecYear'] . '</option>';
                             }
                         }
                         ?>
                 </select><br> <br>
                 <button id="Filter">Search</button>
             </div>
             <?php
                 if (! empty($_POST['Genre'])) {
                     ?>
                     <table cellpadding="10" cellspacing="1">

                 <thead>
                     <tr>
                         <th><strong>id</strong></th>
                         <th><strong>Name</strong></th>
                         <th><strong>Genre</strong></th>
                         <th><strong>Key</strong></th>
                         <th><strong>Time Sig</strong></th>
                         <th><strong>Tempo</strong></th>
                         <th><strong>Tuning</strong></th>
                         <th><strong>Type</strong></th>
                         <th><strong>Recording</strong></th>
                         <th><strong>Year</strong></th>
                     </tr>
                 </thead>
                 <tbody>
                 <?php
                     $query = "SELECT * from test_table";
                     $i = 0;
                     $selectedOptionCount = count($_POST['Genre']);
                     $selectedOption = "";
                     while ($i < $selectedOptionCount) {
                         $selectedOption = $selectedOption . "'" . $_POST['Genre'][$i] . "'";
                         if ($i < $selectedOptionCount - 1) {
                             $selectedOption = $selectedOption . ", ";
                         }

                         $i ++;
                     }
                     $query = $query . " WHERE Genre in (" . $selectedOption . ")";

                     $result = $db_handle->runQuery($query);
                 }
                 if (! empty($result)) {
                     foreach ($result as $key => $value) {
                         ?>
                 <tr>
                         <td><div class="col" id="user_data_1"><?php echo $result[$key]['id']; ?></div></td>
                         <td><div class="col" id="user_data_2"><?php echo $result[$key]['NameUrl']; ?> </div></td>
                         <td><div class="col" id="user_data_3"><?php echo $result[$key]['Genre']; ?> </div></td>
                         <td><div class="col" id="user_data_4"><?php echo $result[$key]['KeySig']; ?> </div></td>
                         <td><div class="col" id="user_data_5"><?php echo $result[$key]['TimeSig']; ?> </div></td>
                         <td><div class="col" id="user_data_6"><?php echo $result[$key]['Tempo']; ?> </div></td>
                         <td><div class="col" id="user_data_7"><?php echo $result[$key]['Tuning']; ?> </div></td>
                         <td><div class="col" id="user_data_8"><?php echo $result[$key]['EntType']; ?> </div></td>
                         <td><div class="col" id="user_data_9"><?php echo $result[$key]['Recording']; ?> </div></td>
                         <td><div class="col" id="user_data_10"><?php echo $result[$key]['RecYear']; ?> </div></td>
                     </tr>
                 <?php
                     }
                     ?>

                 </tbody>
             </table>
             <?php
                 }
                 ?>  
         </div>
     </form>
 </body>
 </html>
             </div>
     </form>
 </body>
 </html>

ADAM UPDATE: DBController.php... Like This??亚当更新:DBController.php ......像这样??

     <?php
 class DBController {
     public $host = "localhost";
     public $user = "root";
     public $password = "password";
     public $database = "test";
     public $conn;

         function __construct() {
         $this->conn = $this->connectDB();
     }  
     function connectDB() {
         $conn = mysqli_connect($this->host,$this->user,$this- 
>password,$this->database);
         return $conn;
         $stmt = $db_handle->conn->prepare($query);
     }
         function runQuery($query) {
                 $result = mysqli_query($this->conn,$query);
                 while($row=mysqli_fetch_assoc($result)) {
                 $resultset[] = $row;
                 }      
                 if(!empty($resultset))
                 return $resultset;
     }
 }
 ?>

testsearch.php Browser Search - Image testsearch.php 浏览器搜索 - 图片

testsearch.php Browser Results - Image testsearch.php 浏览器结果 - 图像

I would get filters in one multidimensional array我会在一个多维数组中得到过滤器

    include 'DBController.php';
    $db_handle = new DBController();
    $filters = [];

    $filters['Genre'] = array_column($db_handle->runQuery("SELECT DISTINCT Genre FROM test_table ORDER BY Genre ASC"), 'Genre');
    $filters['Tempo'] = array_column($db_handle->runQuery("SELECT DISTINCT Tempo FROM test_table ORDER BY Tempo ASC"), 'Tempo');
    $filters['KeySig'] = array_column($db_handle->runQuery("SELECT DISTINCT KeySig FROM test_table ORDER BY KeySig ASC"), 'KeySig');
    $filters['TimeSig'] = array_column($db_handle->runQuery("SELECT DISTINCT TimeSig FROM test_table ORDER BY TimeSig ASC"), 'TimeSig');
    $filters['Tuning'] = array_column($db_handle->runQuery("SELECT DISTINCT Tuning FROM test_table ORDER BY Tuning ASC"), 'Tuning');
    $filters['EntType'] = array_column($db_handle->runQuery("SELECT DISTINCT EntType FROM test_table ORDER BY EntType ASC"), 'EntType');
    $filters['Recording'] = array_column($db_handle->runQuery("SELECT DISTINCT Recording FROM test_table ORDER BY Recording ASC"), 'Recording');
    $filters['RecYear'] = array_column($db_handle->runQuery("SELECT DISTINCT RecYear FROM test_table ORDER BY RecYear ASC"),'RecYear');

in < html > you should have only one < form > and all < select > in it & the < button > with type="submit" attribute.在 < html > 中,您应该只有一个 < form > 和所有 < select > 以及带有 type="submit" 属性的 < button >。 Close your < div >s properly.正确关闭您的 < div >。 You should use id's value in any tag only once like (id="Place").您应该在任何标签中只使用一次 id 的值,例如 (id="Place")。 You don't need an empty option tag in multiple select unless the value of it is used.您不需要在多个 select 中使用空选项标签,除非使用它的值。 Prepare your query statement with the values sent to server from client to prevent sql injections!!!使用从客户端发送到服务器的值准备查询语句,以防止 sql 注入!!!

Example:例子:

    <html>
    <head>
        <link href="style.css" type="text/css" rel="stylesheet"/>
        <title>Riff Bank - Search & Upload</title>
        <style>
            .search-box select {
                min-width: 200px;
            }
        </style>
    </head>
    <body>
        <h2>Riff Bank - Search & Upload</h2>
        <form method="POST" name="Genre">
            <div id="demo-grid">
                <div class="search-box">
                    Select Genre<br>
                    <select id="Genre" name="Genre[]" multiple="multiple">
                            <?php
                            if (!empty($filters['Genre'])) {
                                foreach ($filters['Genre'] as $key => $value) {
                                    echo '<option value="' . $value . '">' . $value . '</option>';
                                }
                            }
                            ?>
                    </select>
                    <br>
                    <br>
                </div>
                <div class="search-box">
                    Select Key<br>
                    <select id="KeySig" name="KeySig[]" multiple="multiple">
                            <?php
                            if (!empty($filters['KeySig'])) {
                                foreach ($filters['KeySig'] as $key => $value) {
                                    echo '<option value="' . $value . '">' . $value . '</option>';
                                }
                            }
                            ?>
                    </select>
                    <br>
                    <br>
                </div>
                <div class="search-box">
                    Select Time Signature<br>
                    <select id="TimeSig" name="TimeSig[]" multiple="multiple">
                            <?php
                            if (!empty($filters['TimeSig'])) {
                                foreach ($filters['TimeSig'] as $key => $value) {
                                    echo '<option value="' . $value . '">' . $value . '</option>';
                                }
                            }
                            ?>
                    </select>
                    <br>
                    <br>
                </div>
                <div class="search-box">
                    Select Tempo<br>
                    <select id="Tempo" name="Tempo[]" multiple="multiple">
                            <?php
                            if (!empty($filters['Tempo'])) {
                                foreach ($filters['Tempo'] as $key => $value) {
                                    echo '<option value="' . $value . '">' . $value . '</option>';
                                }
                            }
                            ?>
                    </select>
                    <br>
                    <br>
                </div>
                <div class="search-box">
                    Select Tuning<br>
                    <select id="Tuning" name="Tuning[]" multiple="multiple">
                            <?php
                            if (!empty($filters['Tuning'])) {
                                foreach ($filters['Tuning'] as $key => $value) {
                                    echo '<option value="' . $value . '">' . $value . '</option>';
                                }
                            }
                            ?>
                    </select>
                    <br>
                    <br>
                </div>
                <div class="search-box">
                    Select Entry Type<br>
                    <select id="EntType" name="EntType[]" multiple="multiple">
                            <?php
                            if (!empty($filters['EntType'])) {
                                foreach ($filters['EntType'] as $key => $value) {
                                    echo '<option value="' . $value . '">' . $value . '</option>';
                                }
                            }
                            ?>
                    </select>
                    <br>
                    <br>
                </div>
                <div class="search-box">
                    Select Recording Type<br>
                    <select id="Recording" name="Recording[]" multiple="multiple">
                        <?php
                            if (!empty($filters['Recording'])) {
                                foreach ($filters['Recording'] as $key => $value) {
                                    echo '<option value="' . $value . '">' . $value . '</option>';
                                }
                            }
                            ?>
                    </select>
                    <br>
                    <br>
                </div>
                <div class="search-box">
                    Select Year<br>
                    <select id="RecYear" name="RecYear[]" multiple="multiple">
                        <?php
                            if (!empty($filters['RecYear'])) {
                                foreach ($filters['RecYear'] as $key => $value) {
                                    echo '<option value="' . $value . '">' . $value . '</option>';
                                }
                            }
                            ?>
                    </select>
                    <br>
                    <br>
                </div>
                <button id="Filter" type="submit">Search</button>
            </div>
        </form>

        <?php
        if (!empty($_POST) && count($_POST)) {
            //filter the array $_POST with the keys representing your filters & fields in your DB that you trust
            $request_filters = array_intersect_key($_POST, array_flip(['Genre','Tempo','KeySig','TimeSig','Tuning','EntType','Recording']));
            if(count($request_filters)) {
                ?>
                <table cellpadding="10" cellspacing="1">
                <thead>
                <tr>
                    <th>
                        <strong>id</strong>
                    </th>
                    <th>
                        <strong>Name</strong>
                    </th>
                    <th>
                        <strong>Genre</strong>
                    </th>
                    <th>
                        <strong>Key</strong>
                    </th>
                    <th>
                        <strong>Time Sig</strong>
                    </th>
                    <th>
                        <strong>Tempo</strong>
                    </th>
                    <th>
                        <strong>Tuning</strong>
                    </th>
                    <th>
                        <strong>Type</strong>
                    </th>
                    <th>
                        <strong>Recording</strong>
                    </th>
                    <th>
                        <strong>Year</strong>
                    </th>
                </tr>
                </thead>
                <?php
                //then build up your prepare query statement
                $params = [];
                $query = "SELECT * from test_table WHERE ";
                $extra_query = [];
                foreach ($request_filters as $field => $values) {
                    $extra_query[] = "{$field} IN (?" . str_repeat(', ?', count($values) - 1) . ")";
                    $params = array_merge($params, $values);
                }
                $query .= implode(' AND ',$extra_query);
                $stmt = $db_handle->conn->prepare($query);
                $stmt->bind_param(str_repeat('s',count($params)), ...$params);
                if($stmt->execute()){
                    $stmt_result = $stmt->get_result();
                    if($stmt_result && $stmt_result->num_rows){
                        ?>
                        <tbody>
                        <?php
                        while($row = $stmt_result->fetch_assoc()){
                            ?>
                            <tr>
                                <td>
                                    <div class="col"><?php echo $row['id']; ?></div>
                                </td>
                                <td>
                                    <div class="col"><?php echo $row['NameUrl']; ?></div>
                                </td>
                                <td>
                                    <div class="col"><?php echo $row['Genre']; ?></div>
                                </td>
                                <td>
                                    <div class="col"><?php echo $row['KeySig']; ?></div>
                                </td>
                                <td>
                                    <div class="col"><?php echo $row['TimeSig']; ?></div>
                                </td>
                                <td>
                                    <div class="col"><?php echo $row['Tempo']; ?></div>
                                </td>
                                <td>
                                    <div class="col"><?php echo $row['Tuning']; ?></div>
                                </td>
                                <td>
                                    <div class="col"><?php echo $row['EntType']; ?></div>
                                </td>
                                <td>
                                    <div class="col"><?php echo $row['Recording']; ?></div>
                                </td>
                                <td>
                                    <div class="col"><?php echo $row['RecYear']; ?></div>
                                </td>
                            </tr>
                            <?php
                        }
                        ?>
                        </tbody>
                        <?php
                    } else {
                        ?>
                        <tbody>
                            <tr>
                                <td colspan="10">No results!</td>
                            </tr>
                        </tbody>
                        <?php
                    }
                }
                ?>
                </table>
                <?php
            }
        }
    ?>
    </body>
    </html>

The Controller: Controller:

    <?php
    class DBController {
        private $host = "localhost";
        private $user = "root";
        private $password = "password";
        private $database = "test";
        public $conn;
        function __construct() {
            $this->conn = $this->connectDB();
        }
        function connectDB() {
            $conn = mysqli_connect($this->host,$this->user,$this->password,$this->database);
             return $conn;
            }
        function runQuery($query) {
            $result = $this->conn->query($query);
            $resultset = [];
            while($row=$result->fetch_assoc()) {
                $resultset[] = $row;
            }
            return $resultset;
        }
    }
    ?>

I hope it helps.我希望它有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM