0

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);
$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 Answers 2

2

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>
Sign up to request clarification or add additional context in comments.

Comments

0

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>

1 Comment

Thank you!I can use this in my other page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.