0

I have a table populated from a mysql database. One of the fields is "status". I would like this cell to be a drop down box inside the table, so I can then update the particular field.

This code, correctly displays the table and currently it displays the "status" filed inside a text box that I can edit successfully. I would like this to be a drop down though.

<?php

require_once('db_connect.php');

$result = mysql_query("SELECT *
FROM queries
WHERE SR = '$_GET[SR]'
")
or die(mysql_error());


echo '<form name="Form" action="update.php" method="post">';
echo 
"<table id='box-table-b'>
 <thead>
 <tr>
 <th>SR</th>
 <th>Product</th>
 <th>Status</th>
 </tr>

  </thead>";

while($row = mysql_fetch_array($result))
   {
   echo "<tbody>";
   echo "<tr>";
   echo "<td>" . $row['SR'] . "</td>";
   echo "<td>" . $row['product'] . "</td>";
   echo "<td>" . '<input type="text" name="status" value="'.$row['status'].'" />' . "</td>";
   echo "</tr>";
   echo "</tbody>";
   }
 echo "</table>";
 echo '<input type="submit" name="Save" value="Save" />';
 echo '</form>';


?>

Can someone please show me how to do this ?

4

2 回答 2

0

要回答这个问题,您应该使用<select></select>标签。例子:

<select>
<option>Item 1</option>
<option>Item 2</option>
<option> ... </option>
<option selected="selected">Item N</option>
</select>

在此特定示例中,默认情况下将显示下拉菜单并选择“项目 N”。

附带说明一下,一般来说,使用 mysql_* 函数不是不好的做法吗?

于 2013-05-16T14:06:28.930 回答
0

通过下拉菜单我猜你的意思是一个select标签,任何更复杂的东西,这需要一个自定义实现。

这需要两个步骤:首先您需要创建选择标签并使用选项标签填充它,然后您需要将所需的值设置为选中。

创建选择标签:

$myselect="<select id='status' name='status'>";
foreach($status_values as $e){
     $myselect.="<option value='$e'>$e</option>";
}
$myselect.="</select>";

$status_value是一个数组,您可以在代码中包含或从查询中获取它。

要选择正确的,您可以在上面的代码中添加以下 if:

$myselect="<select id='status' name='status'>";
foreach($status_values as $e){
     if($e == $row['status']){
          $myselect.="<option value='$e' SELECTED>$e</option>";
     }else
          $myselect.="<option value='$e'>$e</option>";
     }
}
$myselect.="</select>";
于 2013-05-16T14:11:38.660 回答