2013-01-24 96 views
0

我有一個基本的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 
?> 
+0

可能重複的解決方案](http://stackoverflow.com/questions/7137357/cascade-dropdown-list-using-jquery-php) –

+0

那麼你有什麼問題?你從來沒有真正提出過一個問題。 – Pitchinnate

+0

我遇到的問題是已經填充了下拉菜單,我怎麼去限制已經返回的數據,進一步通過用戶做出的選擇? – cstones88

回答

0

您可以使用JavaScript來做到這一點。只要做到:

1)添加<form action="" method="post" id="drop_submit"><select>標籤

2)收件<select id="select_tag" name="YOUR_SELECT_NAME"> 「選項PHP DB代碼」 </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(); 
+0

是否可以不使用JS? – cstones88

+0

PHP完全在服務器端運行!所以如果你想控制或管理用戶交互,你需要客戶端編程!使用js你可以建立高度動態的網頁內容。你可以不用JS。爲此,您必須在SELECT標籤之後創建一個提交按鈕,這樣用戶才能夠使用選定的輸入提交表單。 ! –

+0

我創建了另一個提交按鈕,現在應該根據選擇框中的值過濾結果 - 但是,當我提交/發佈時,沒有任何值從下拉列表中傳遞 - 我在原始查詢中發佈了更新後的代碼。謝謝 – cstones88

0

當你更新你的問題,它是使用jQuery/PHP的[級聯下拉列表

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