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

问题描述 投票:-1回答:1

这里是我的数据库表的图片-> MySQL-Table

我正在尝试创建一个带有下拉选择器的搜索页面,以对最后8列中的每一个进行过滤。我希望下拉选择器能够选择多个条目(我已经可以使用了)我还希望他们从已经输入到表列中的数据中预加载值。 (尽管我承认我不完全了解这部分的工作原理,但我也在教程的帮助下完成了这项工作。)

使用这些教程,我创建了一个php页面,其中包含8个下拉选择器,这些选择器会自动从相应列中提取值。我希望能够使用所有(或某些)这些列过滤器从我的表格中过滤结果。例如...说我想显示所有属于Genre = Metal,AND KeySig = E minor和AND Tempo = Fast的条目...我可能会使用mysql命令,例如mysql> SELECT id,NameUrl,Genre,KeySig,TimeSig,Tempo,Tuning,EntType,Recording,REYEAR from test_table WHERE Genre ='Metal'AND KeySig ='E minor'AND Tempo ='Fast';

基本上,我正在尝试通过php网页执行相同的操作。现在有了代码,只有我的第一个下拉选择器“流派”实际上过滤了所有内容。其余的过滤器就在那儿..它们还没有做任何事情。我需要帮助,从剩余的下拉列表中拉出$ _POST请求,并提出可以通过AND运算符使用多个变量对我的列进行过滤的代码。

我希望这是有道理的。。。我不是计算机专家,而是更多的音乐家。将其构建为可帮助我完成写作工作流程的工具。希望有人可以帮助-谢谢

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

        <?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更新: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 Browser Results - Image

php html mysql search
1个回答
0
投票

我将在一个多维数组中获得过滤器

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

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

在中,您应该只有一个,并且所有

© www.soinside.com 2019 - 2024. All rights reserved.