PHP中的下拉列表

时间:2012-10-24 10:06:57

标签: php mysql html5

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

<?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>"; 
      }

  ?>

3 个答案:

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

答案 1 :(得分: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>";  
}

答案 2 :(得分: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>"; 
}