准备使用我的SQL查询结果填充下拉菜单。脚本函数,但我不确定将结果添加到html下拉菜单的语法。目前,它会使用结果填充表格。这是我的代码:
<?php require_once('Connections/database.php'); ?>
<?php
$q=$_GET["q"];
mysql_select_db($database_db, $database);
$sql="SELECT cat_id, catname FROM categories WHERE cat_id = '".$q."'";
$result = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>Category Name</th>
<th>Category ID</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['catname'] . "</td>";
echo "<td>" . $row['cat_id'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($database);
?>
答案 0 :(得分:2)
这应该这样做:
<select name="input_name">
<?php
while($row = mysql_fetch_array($result))
echo "<option value='".$row['cat_id']."'>" . $row['catname'] . "</option>";
?>
</select>
答案 1 :(得分:0)
对于菜单,请使用
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Products</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
<ul>
<?php
for(){
?>
<li><a href="#">$result[datafield]</a></li>
}
?>
</ul>