有一个php下拉菜单的问题

时间:2014-06-01 13:16:32

标签: php mysql

所以我正在开展一个项目,要求用户从下拉菜单中进行选择。使用以下代码我只能设法获得1号和1号插槽。 2显示一个值。它当前正在插入其他插槽的值为0,因此它们正在工作,只是没有显示任何值。如果这还不够清楚,请告诉我,我可以尝试提供更多信息。代码如下;

 echo' <br/>
 Slot 1 
<select name="slot1">';

while($row = mysqli_fetch_array($result) and $i<10)
{
echo"<option value='";
echo$row['playerID'];    // sorry about this inverted commas are trickys
echo"'>";
echo $row['name'];
echo "</option>";

$i=$i+1;
}

echo '</select>';

echo' <br/>
Slot 2 
<select name="slot2">';
$i=1;
while($row = mysqli_fetch_array($result) and $i<10)
{
echo"<option value='";
echo$row['playerID'];    // sorry about this inverted commas are trickys
echo"'>";
echo $row['name'];
echo "</option>";

 $i=$i+1;
 }

echo '</select>';

echo' <br/>
Slot 3 
<select name="slot3">';

 while($row = mysqli_fetch_array($result) and $i<10)
 {
  echo"<option value='";
 echo$row['playerID'];    // sorry about this inverted commas are trickys
 echo"'>";
 echo $row['name'];
 echo "</option>";

 $i=$i+1;
 }

 echo '</select>';

 echo' <br/>
 Slot 4 
 <select name="slot4">';

 while($row = mysqli_fetch_array($result) and $i<10)
 {
 echo"<option value='";
 echo$row['playerID'];    // sorry about this inverted commas are trickys
 echo"'>";
 echo $row['name'];
 echo "</option>";

$i=$i+1;
}

echo '</select>';

echo' <br/>
Slot 5 
<select name="slot5">';

while($row = mysqli_fetch_array($result) and $i<10)
{
echo"<option value='";
echo$row['playerID'];    // sorry about this inverted commas are trickys
echo"'>";
echo $row['name'];
echo "</option>";

$i=$i+1;
}

echo '</select>';

2 个答案:

答案 0 :(得分:0)

您必须在其他人插槽之前将$ i值重置为1:

$i=1;

或者您必须再次考虑如何填充插槽,是否是相同的查询?每个插槽都有相同的值吗?在这种情况下,您需要: mysqldataseek

答案 1 :(得分:0)

更简单的方法是将html代码收集到变量中,然后在html部分中使用它们。

<?php 

    $slot1 = '';                                                //initializing the slot variables
    $slot2 = '';
    $slot3 = '';

    while($row = mysqli_fetch_array($result)){
        $playerId = $row['playerId'];
        $name     = $row['name'];
        if($i < 10){
            $slot1 .="<option value='$playerId'>$name</option>";        //appending the options using .=
            $i++;
        }
        if($i < 20){
            $slot2 .="<option value='$playerId'>$name</option>";
            $i++;
        }
        if($i < 30){
            $slot3 .="<option value='$playerId'>$name</option>";
            $i++;
        }
    }

?>

<html>
    <body>
        <select name="slot1" >
            <?php echo $slot1 ?>
        </select>

        <select name="slot2" >
            <?php echo $slot2 ?>
        </select>

        <select name="slot3" >
            <?php echo $slot3 ?>
        </select>
    </body>
</html>