-2

我需要帮助来解决我的问题。我不能让我的选择器显示 92 次,我为此编写了 FOR 指令,但它只显示一个选择器,其值形式为 Database ,我该怎么做......我的代码:

<?php
  $con = mysql_connect("localhost","root","sergios.com");
    if (!$con)
       {
       die('Could not connect: ' . mysql_error());
  }

mysql_select_db("phptests", $con);

$result = mysql_query("SELECT * FROM Category");

 for($i=1;$i<92;++$i){  
  echo "<select>";
        while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
    {
    echo "<option>" .  $line['name'] . "</option>";
     }
      echo "</select>"; 
      }

  ?>
4

3 回答 3

0

您需要在循环之外获取打开和关闭选择。由于您已经在遍历结果,因此不需要额外的 for 循环:

$con = mysql_connect("localhost","root","sergios.com");
if (!$con)  {
   die('Could not connect: ' . mysql_error());
}

mysql_select_db("phptests", $con);

$result = mysql_query("SELECT * FROM Category");

echo "<select>";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
  echo "<option>" .  $line['name'] . "</option>";
}
echo "</select>"; 
于 2012-10-24T10:08:15.773 回答
0

您必须在 for 循环之外构建选项

$result = mysql_query("SELECT * FROM Category"); 
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) 
{ 
    $myOptions .= "<option>" .  $line['name'] . "</option>"; 
} 
for($i=1;$i<92;++$i){   
  echo "<select>"; 
     echo $myOptions;    
  echo "</select>";  
}
于 2012-10-24T10:11:19.087 回答
0

我认为您需要显示 92 个选择框。试试下面的代码......

$result = mysql_query("SELECT * FROM Category"); 
$options = "";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
  $options .= "<option>" .  $line['name'] . "</option>";
}
for($i=1;$i<92;++$i){ 
    echo "<select>";
    echo $options;
    echo "</select>"; 
}
于 2012-10-24T10:12:00.863 回答