Adding Values to dropdown Menu box from MySql_Fetch_Array
Am attemtping to populate a dropdown menu with the results of my SQL query. The script functions, however I just am unsure of the syntax to add the results into the html dropdown menu. Currently it populates a table with the results. Here is my code:
<?php require_once('Connections/database.php'); ?>
<?php
$q=$_GET["q"];
mysql_select_db($database_db, $database);
开发者_如何学Python$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);
?>
This should do it:
<select name="input_name">
<?php
while($row = mysql_fetch_array($result))
echo "<option value='".$row['cat_id']."'>" . $row['catname'] . "</option>";
?>
</select>
for menu , use
<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>
精彩评论