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

时间:2016-01-29 20:44:38

标签: php loops mysqli

我想显示一个包含这些点的调查表。

y

这些是我的表格:

y = pmin(y1, y2)

# set up your plot as in the question    
plot(x, y1, type="l")
lines(x, y2)

# define a re-usable variable for the vertical line placement
x_vert = 380
abline(v = x[x_vert])

# Now we'll draw 2 polygons, one for the left side, one for the right.
# The first (x,y) pairs of the polygon are just the (x,y) coords of the
# density we're filling to, until the vertical line
# Then we need to connect the "bottom" points, which have coordinates
# (x[x_vert], 0) and (x[1], 0)    
polygon(x = c(x[1:x_vert], x[x_vert], x[1]), 
        y = c(y[1:x_vert], 0, 0),
        col = "blue")
# similar for the right hand polygon, but now going from x_vert to length(x)
polygon(x = c(x[x_vert:length(x)], x[length(x)], x[x_vert]),
        y = c(y[x_vert:length(x)], 0, 0),
        col = "red")

我陷入困境,因为我不断将数据打印出已经显示的内容,并将其作为一个可行的选择。我无法想到如何修复我的逻辑。

这是我到目前为止的代码:

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>

有人可以帮助我在我的逻辑中修复此错误。

1 个答案:

答案 0 :(得分: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>

这是输出:

  

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

     水(选择一个)
  ⦾Squirtle
  ⦾Mudkip

     

火(选择一个)
  ⦾Charmander

     草(选择一个)
  ⦾Bulbasaur
  ⦾Tuncko
  ⦾Turtwig

     

发送

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

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

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

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