Hi!
I’d be grateful, if you could help me solving a problem with php and mysql.
I have got a mysql database. There is a table in this database, called “categories”. I want to create a html (or php)-Page with a drop-down menu. And in this menu, I want to insert the values in the table categories. The user can choose a category, and is directed to another html-Page, depending on his choice.
Could you help me getting the categories from the mysql-table into the drop-down menu on my php-Page?
Thanks for your help.
Best regards,
Lisa W.
drop-down menus and mysql
Moderator: General Moderators
- daven
- Forum Contributor
- Posts: 332
- Joined: Tue Dec 17, 2002 1:29 pm
- Location: Gaithersburg, MD
- Contact:
two options come to mind. One involves creating an array, and the other does not. Technically, it would be better to use the array, but either method will work. The code surmizes that you have table "categories" with column "category_name".
1. with array
2. without arrays
Then do whatever form/javascript processing you have in mind.
1. with array
Code: Select all
<?php
$query="SELECT * FROM categories";
$result=mysql_query($query,$db_conn);
for($i=0;$i<mysql_num_rows($result);$i++){
$category_array[$i]=$row[category_name];
}
?>Code: Select all
<HTML>
<select name="category">
<?for($i=0;$i<count($category_array);$i++){
print "<option>".$category_arrayї$i];
}?>
</select>
</HTML>Code: Select all
<?php
$query="SELECT * FROM categories";
$result=mysql_query($query,$db_conn);
?>Code: Select all
<HTML>
<select name="category">
<?while($row=mysql_fetch_assoc($result)){
print "<option>".$rowїcategory_name];
}?>
</select>
</HTML>