0

検索フィールドに入力された内容に基づいてデータを取得する基本的な SQL クエリがあります - データベースの列から入力される選択ボックスを配置しました - 私がしたいのは、これが変更されたときです/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">';

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
?>
4

2 に答える 2

0

Javascript を使用してそれを行うことができます。ただ行う:

1)タグの<form action="" method="post" id="drop_submit">前に追加<select>

<select id="select_tag" name="YOUR_SELECT_NAME">2) 「OPTIONS BY PHP DB CODE」と書く</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();
于 2013-01-24T20:35:01.910 に答える
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
    ?>
于 2013-01-25T20:50:17.773 に答える