2013-08-28 74 views
1

我想使用ComboBox從mysql中搜索問題。如果我在ComboBox中選擇第一章,我想要顯示那個只有第一章有問題的問題。通過複選框從mysql中檢索數據

在這假設我的第1章包含2個問題,第2章包含一些問題等等。當我選擇第1章時,它不會顯示第1章所具有的問題。它只會打印上一章的最後一個問題。我怎麼解決這個問題?

<?php  

     $sql= "select distinct chapter from math";   
     $q= mysql_query($sql);   
     echo "<select name='fname'>";  
     while($info=mysql_fetch_array($q)){ 
     $d1 = $info['chapter'];  
     echo "<option> ".$info['chapter']."</option>";   
     } 
     echo "</select>";   
     $sql1 = "select question from math where chapter=$d1";   
     $sql1_res = mysql_query($sql1) or die(mysql_error());  
     while($row = mysql_fetch_array($sql1_res)){ 
       $question=htmlspecialchars_decode($row['question'], ENT_QUOTES); // It gives only last question. 
     echo $question;   
     } 
    ?> 

回答

0

你的錯誤在於你選擇的是上一次查詢迭代的問題,你選擇了章節。

... 
while($info=mysql_fetch_array($q)){ 
    $d1 = $info['chapter'];  
    echo "<option> ".$info['chapter']."</option>";   
} 
echo "</select>";   
$sql1 = "select question from math where chapter=$d1"; 
... 

將始終選擇最後一章。但你有另一個問題。假設你想在用戶從下拉列表中選擇一些值時顯示問題,你必須使用POST/GET/AJAX將選擇發送回PHP並根據該選擇生成結果。事情是這樣的:

if(!isset($_POST['fname'])) 
    { 
     $sql= "select distinct chapter from math";   
     $q= mysql_query($sql);   
     echo "<select name='fname'>"; 

     while($info=mysql_fetch_array($q)){  
     echo "<option> ".$info['chapter']."</option>";   
     } 
     echo "</select>";  
    } 
    else 
    { 
     $sql1 = "select question from math where chapter = " . $_POST['fname'];   
     $sql1_res = mysql_query($sql1) or die(mysql_error());  
     while($row = mysql_fetch_array($sql1_res)){ 
     $question=htmlspecialchars_decode($row['question'], ENT_QUOTES); // It gives only last question. 
     echo $question;   
     } 
    } 
0

在您的問題查詢根據下面的代碼改變而循環:

<?php  

     $sql= "select distinct chapter from math";   
     $q= mysql_query($sql);   
     echo "<select name='fname'>";  
     $d1 = array(); 
     while($info=mysql_fetch_array($q)){ 
     $d1[] = $info['chapter'];  
     echo "<option> ".$info['chapter']."</option>";   
     } 
     echo "</select>";   
     $sql1 = "select question from math where chapter IN ('".implode("','",$d1)."')";   
     $sql1_res = mysql_query($sql1) or die(mysql_error());  
     while($row = mysql_fetch_array($sql1_res)){ 
     $question=htmlspecialchars_decode($row['question'], ENT_QUOTES); // It gives only last question. 
     echo $question;   
     } 
    ?> 

使$d1變量作爲數組和改變問題的選擇查詢。

+0

他有括號,所以他不迴應它的每一個迭代。 – Dexa

+0

@Dexa我的錯誤。我已經改變了我的答案。 –

0
<?php  

     $sql= "select distinct chapter from math";   
     $q= mysql_query($sql);   
     echo "<select name='fname'>";  
     while($info=mysql_fetch_array($q)) 
    { 
     $d1 = $info['chapter'];  
     echo "<option> ".$info['chapter']."</option>";   

     echo "</select>";   
     $sql1 = "select question from math where chapter=$d1";   
     $sql1_res = mysql_query($sql1) or die(mysql_error());  
     while($row = mysql_fetch_array($sql1_res)) 
      { 
       $question=htmlspecialchars_decode($row['question'], ENT_QUOTES); // It gives only last question. 
     echo $question;   
     } 
    } 
    ?> 
0
$sql1 = "select question from math where chapter=".$d1;