2016-01-29 70 views
0

我想用這些表格中顯示的點來顯示調查。如何使用單選按鈕打印數據庫中的數據列表?

survey_classType_pokemon_table 
idquestion idclassType idPokemon 
8   | 13   | 14 
8   | 13   | 15 
8   | 14   | 16 
8   | 15   | 17 
8   | 15   | 18 
8   | 15   | 19 

這些都是我的表:

surveyTable 
idsurvey question 
8  Choose one pokémon from each type 

classTypeTable 
idclassType classType 
13   | Water 
13   | Water 
14   | Fire 
15   | Grass 
15   | Grass 
15   | Grass 

pokemonTable 
idPokemon pokemon 
14  | Squirtle 
15  | Mudkip 
16  | Charmander 
17  | Bulbasaur 
18  | Treecko 
19  | Turtwig 

我被困在這,因爲我一直循環的數據打印出什麼已經顯示,並且把它當作一個更多鈔票的選擇。我想不出如何解決我的邏輯問題。

這是代碼,我到目前爲止有:

$sql2 = mysqli_query(" SELECT * FROM question_classType_pokemon_table where idsurvey='8' ") or die(mysqli_error()); 

     ?> 

     <div class="entry"> 
     <form action="#" method="post"> 
     <?php 


     $cont = 1; 
     $num = 1; 
     while($row = mysqli_fetch_assoc($sql2)) { 

      $idclasstype = $row['idclasstype']; 
      $idPokemon = $row['idPokemon']; 

      $result1 = mysqli_query("SELECT classType FROM ClassTypeTable where idclasstype='$idclasstype' "); 

      while($row = mysqli_fetch_array($result1)){ 
        $classType = $row['classType']; 
        echo $classType."<br/>"; 
      } 


      $sql3 = mysqli_query("SELECT pokemon FROM pokemonTable where idPokemon='$idPokemon'"); 
      while($row1 = mysqli_fetch_assoc($sql3)){ 
       ${'opc'.$num} = $row1['pokemon']; 
       echo "<br/>"; 
       echo "NUM: ".$num++."<br/>"; 
      } 
      for($i = 1; $i < $num; $i++){ 
       echo "<input type=\"radio\" name=\"".${'opc'.$cont}."\" value=\"".${'opc'.$i}."\">".${'opc'.$i}."<br/>"; 
      } 
      $cont++; 

     } 

    ?> 
     <input type="submit" value="SEND"> 
     </form> 
     </div> 

我得到的是:

Water 
NUM: 1 
Squirtle (radio button) 

Water 
NUM: 2 
Squirtle (radio button) 
Mudkip (radio button) 

Fire 
NUM: 3 
Squirtle (radio button) 
Mudkip (radio button) 
Charmander (radio button) 

Grass 
NUM: 4 
Squirtle (radio button) 
Mudkip (radio button) 
Charmander (radio button) 
Bulbasaur (radio button) 

Grass 
NUM: 5 
Squirtle (radio button) 
Mudkip (radio button) 
Charmander (radio button) 
Bulbasaur (radio button) 
Treecko (radio button) 

Grass 
NUM: 6 
Squirtle (radio button) 
Mudkip (radio button) 
Charmander (radio button) 
Bulbasaur (radio button) 
Treecko (radio button) 
Turtwig (radio button) 

,我想有輸出是:

Survey 
Choose one pokémon from each type 

Water (Choose one) 
1. Squirtle (radio button) 
2. Mudkip (radio button) 

Fire (Choose one) 
1. Charmander (radio button) 

Grass (Choose one) 
1. Bulbasaur (radio button) 
2. Treecko (radio button) 
3. Turtwig (radio button) 

[SEND] 

燦有人幫我在我的邏輯中解決這個錯誤。

+0

我會從一本關於PHP和MySQL的好書或教程開始 – Strawberry

+0

stackoverflow搜索應該也有很大的幫助。 –

+0

@HilmiErdemKEREN我看了幾乎無處不在。但我找不到任何好的例子。 – learningbyexample

回答

1

這是我的代碼:

$result = mysqli_query 
(
    $link, 
    "SELECT DISTINCT pokemonTable.pokemon, 
      surveyTable.*, 
      survey_classType_pokemon_table.*, 
      classTypeTable.classType 
    FROM surveyTable 
    JOIN survey_classType_pokemon_table 
     ON survey_classType_pokemon_table.idquestion = surveyTable.idsurvey 
    JOIN classTypeTable 
     ON classTypeTable.idclassType = survey_classType_pokemon_table.idclassType 
    JOIN pokemonTable 
     ON pokemonTable.idPokemon = survey_classType_pokemon_table.idPokemon 
    WHERE survey_classType_pokemon_table.idquestion='8' 
    ORDER BY classTypeTable.idclassType ASC, pokemonTable.idPokemon ASC 
    " 
) or die(mysqli_error($link)); 
?> 
<div class="entry"> 
<form action="" method="post"> 
<?php 
$classType = $intro = ''; 
while($row = mysqli_fetch_assoc($result)): 

    if(!$intro): 
     echo $row['question']."<br/>\n"; 
     $intro = $row['question']; 
    endif; 

    if($classType != $row['classType']): 
     echo "<br/>\n{$row[classType]} (Choose one)<br/>\n"; 
     $classType = $row['classType']; 
    endif; 
?> 
<input type="radio" name="<?php echo $row['classType']; ?>" value="<?php echo $row['pokemon']; ?>"><?php echo $row['pokemon']; ?><br/> 
<?php endwhile; ?> 
<input type="submit" value="SEND"> 
</form> 
</div> 

,這是輸出:

選擇從每種類型的一個神奇寶貝

水(任選其一)
⦾小水龜
⦾ Mudkip

F IRE(任選其一)
⦾火龍

草(任選其一)
⦾妙蛙種子
⦾木守宮
⦾Turtwig

SEND

phpfiddle demo(點擊「跑向查看結果)

mySQL查詢執行一次,然後在while循環中,類使用變量($classType)作爲標誌進行分組。

在每個單選按鈕中,我已將名稱設置爲classType,值爲polemon,但是如果您希望將其設置爲相應的ID,則可以輕鬆更改其內容。

請注意,我們的表架構中的名稱與代碼中的名稱不同。我已經使用了表模式中提供的名稱,否則您必須更改survey_classType_pokemon_tablequestion_classType_pokemon_table。檢查其他表和字段名稱。

相關問題