搜索结果按选择/下拉框筛选

时间:2013-01-24 19:36:45

标签: php sql pdo

我有一个基本的SQL查询,它根据输入到搜索字段的内容来获取一些数据 - 我已经放置了一个从数据库中的列填充的选择框 - 我想要做的是当这是改变/发布,它更新已经找到的结果,即通过游戏的“流派”进一步钻进它们。

这是我已有的代码

  echo "<select id='dropdown' name='dropdown' class='dropdown'>";//creates select HTML element
    $stmt = $dbh->prepare('SELECT genre_name FROM genre'); //prepares sql query
    $stmt->execute(); //executes query
       while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { //fetches data in associative array
    echo "<option>{$row['genre_name']}</option>"; //inputs data found into select as an option
       }       
echo "</select>";




echo '<input type="submit" value="Filter" id="filter" name="filter" class="filter">';

if(!isset($_POST['filter']))//check if filter genre has been pressed
{
    echo ("<h4>Please select a genre</h4>"); //if not show this
}
    else { //otherwise if it has...do this
    echo ("<h4>Your results have been filtered</h4>");

    $dropvalue = $_POST['dropdown']; //sets variable of value of dropdown box
    $dbh = config();
            //sorting function, want to further drill search results by genre
            $stmt = $dbh->prepare("SELECT * FROM consoles, games, genre WHERE genre.genre_name = :dropvalue");
            $stmt->bindValue(':dropvalue','%'.$dropvalue.'%');
            $stmt->execute();           
            while ($row = $stmt->fetch())
{
    echo "<ul>";
    echo "<a href='details.php?game_id=".$row['game_id']."'>".$row['game_name']."</a>";
    echo "</ul>";
}
}
      $dbh = NULL; //terminates connection to database
?>

2 个答案:

答案 0 :(得分:0)

您可以使用Javascript来执行此操作。只是做:

1)在<form action="" method="post" id="drop_submit">代码

之前添加<select>

2)用PHP DB CODE编写<select id="select_tag" name="YOUR_SELECT_NAME">“选项”</select>

3)添加以下javascript以在HTML页面的任何位置提交表单。

<script type="text/javascript">
  $('#status').change(function ()
  {
    $('#drop_submit').submit();
  });
</script>

雅,别忘了添加,

if(isset($_POST['dropdown']))
{
  $dropdown = $_POST['dropdown'];
}
else
{
  $dropdown = "YOUR_DEFAULT / 1ST VALUE";
}
  $stmt = $dbh->prepare("SELECT game_name FROM games WHERE games.game_id=('$dropdown')"); 
  //prepares sql statement, this prevents SQL Injection, as the query is sent seperate to the string entered.
  $stmt->bindValue(':dropdown','%'.$dropdown.'%');
  $stmt->execute();

答案 1 :(得分:0)

当您更新问题时,它就是

的解决方案
echo '<form action="" method="post">';

     echo "<select id='dropdown' name='dropdown' class='dropdown'>";//creates select HTML element
        $stmt = $dbh->prepare('SELECT genre_name FROM genre'); //prepares sql query
        $stmt->execute(); //executes query
           while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { //fetches data in associative array
        echo "<option>{$row['genre_name']}</option>"; //inputs data found into select as an option
           }       
    echo "</select>";        
    echo '<input type="submit" value="Filter" id="filter" name="filter" class="filter">';

    echo '</form>';

    if(!isset($_POST['filter']))//check if filter genre has been pressed
    {
        echo ("<h4>Please select a genre</h4>"); //if not show this
    }
        else { //otherwise if it has...do this
        echo ("<h4>Your results have been filtered</h4>");

        $dropvalue = $_POST['dropdown']; //sets variable of value of dropdown box
        $dbh = config();
                //sorting function, want to further drill search results by genre
                $stmt = $dbh->prepare("SELECT * FROM consoles, games, genre WHERE genre.genre_name = :dropvalue");
                $stmt->bindValue(':dropvalue','%'.$dropvalue.'%');
                $stmt->execute();           
                while ($row = $stmt->fetch())
    {
        echo "<ul>";
        echo "<a href='details.php?game_id=".$row['game_id']."'>".$row['game_name']."</a>";
        echo "</ul>";
    }
    }
          $dbh = NULL; //terminates connection to database
    ?>