【问题标题】:How do I print a list from data in my database with radio buttons?如何使用单选按钮从数据库中的数据打印列表?
【发布时间】:2016-01-29 20:44:37
【问题描述】:

我想用表格中显示的这些点显示调查。

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]

谁能帮我解决我的逻辑中的这个错误。

【问题讨论】:

  • 我会从一本关于 PHP 和 MySQL 的好书或教程开始
  • stackoverflow 搜索也应该有很大帮助。
  • @HilmiErdemKEREN 我几乎到处都看过。但我找不到任何好的例子。

标签: php loops mysqli


【解决方案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>

这是输出:

从每种类型中选择一只神奇宝贝

水(选择一项)
⦾ 松鼠
⦾泥鳅

火(选择一个)
⦾ Charmander

草(选择一个)
⦾ 大头龙
⦾ Treecko
⦾龟甲

发送

phpfiddle demo(点击“运行”查看结果)

执行一次 mySQL 查询,然后在 while 循环中使用变量 ($classType) 作为标志对类进行分组。

在每个单选按钮中,我将名称设置为classType,将值设置为polemon,但如果您更喜欢设置相应的id,您可以轻松更改其内容。

请注意,我们的表架构中的名称与您的代码中的名称不同。我已经使用了表架构中提供的名称,否则您必须在question_classType_pokemon_table 中更改survey_classType_pokemon_table。还要检查其他表和字段名称。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多