dsadsa123111 2016-01-29 20:44
浏览 22

如何使用单选按钮从数据库中的数据打印列表?

I want to display a survey with these points that are shown in the table.

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

these are the tables that I have:

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

I am stuck in this because I keep looping the data printing out what was already displayed and leaving it as a posible choice. I can't think of how to fix my logic.

This is the code that I have so far:

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

What I get is:

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)

The output that I want to have is:

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]

Can someone help me fix this error in my logic.

  • 写回答

1条回答 默认 最新

  • dsogx84602 2016-02-02 00:23
    关注

    This is my code:

    $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/>
    ";
            $intro = $row['question'];
        endif;
    
        if( $classType != $row['classType'] ):
            echo "<br/>
    {$row[classType]} (Choose one)<br/>
    ";
            $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>
    

    and this is the output:

    Choose one pokémon from each type

    Water (Choose one)
    ⦾ Squirtle
    ⦾ Mudkip

    Fire (Choose one)
    ⦾ Charmander

    Grass (Choose one)
    ⦾ Bulbasaur
    ⦾ Treecko
    ⦾ Turtwig

    <kbd>SEND</kbd>

    <kbd>phpfiddle demo</kbd> (click “Run” to see result)

    The mySQL query is performed once, then in the while loop the classes are grouped using a variable ($classType) as flag.

    In each radio button I have set the name as classType and the value as polemon, but if you prefer set it with corresponding id, you can easy change their contents.

    Please note that the names in our table schema are different from that in your code. I have used the names provided in table schema, otherwise you have to change survey_classType_pokemon_table in question_classType_pokemon_table. Check also the others table and fields name.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度