php动态双下拉菜单

时间:2015-04-06 04:58:59

标签: php html forms dynamic drop-down-menu

目前我尝试从同一个表中构建两个链接下拉菜单.. 这是表: -

表名:items_sold

 no      |    category   |   item          |  model
-------------------------------------------------------------
1        |   stationery  |  pen            |  pilot
2        |   stationery  |  pencil         | stabilo
3        |   stationery  |  pen            |  stabilo
4        |   it stuff    |  pc             |  lenovo thinkcentre
5        |   it stuff    |  pc             |  asus
6        |   it stuff    |  notebook       |  acer aspire zero

我已经创建了第一个下拉菜单: -

<select name=item>
<?php
$sql = "SELECT DISTINCT item FROM items_sold WHERE category='$category'";
$result = mysql_query($sql);
while ($row=mysql_fetch_array($result)) { ?>
<option value"<?php echo $row['item']; ?>"><?php echo $row['item']; ?></option>
<?php } ?>
</select>

对于第二个下拉菜单,我尝试使用以下代码: -

<select name="model">
<?php
$sql1 = "SELECT * FROM items_sold WHERE category='$category' AND item=$row['item']";
$result1 = mysql_query($sql1);
while ($row1=mysql_fetch_array($result1)) { ?>
<option value"<?php echo $row1['model']; ?>"><?php echo $row1['model']; ?></option>
<?php } ?>
</select>

我尝试根据用户从第一个菜单中选择的项目来显示项目的模型。但是第二个下拉菜单中没有任何内容。看起来有点不对劲......有没有办法可以在不使用javascript的情况下做到这一点?

TQ

1 个答案:

答案 0 :(得分:0)

您的语法错误是错误的。请将其作为

<select name="model">
  <?php
$sql1 = "SELECT * FROM items_sold WHERE category='$category' AND item=$row[item]";
$result1 = mysql_query($sql1);
while ($row1=mysql_fetch_array($result1)) { ?>
  <option value"<?=$row1['model']?>">
  <?=$row1['model'];?>
  </option>
  <?php } ?>
</select>