Page 1 of 1

drop-down menus and mysql

Posted: Mon Jan 20, 2003 2:40 pm
by Lisa.W
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.

Posted: Mon Jan 20, 2003 4:05 pm
by daven
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

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

&lt;HTML&gt;
&lt;select name="category"&gt;
    &lt;?for($i=0;$i&lt;count($category_array);$i++)&#123;
        print "&lt;option&gt;".$category_array&#1111;$i];
    &#125;?&gt;
&lt;/select&gt;
&lt;/HTML&gt;
2. without arrays

Code: Select all

<?php
$query="SELECT * FROM categories";
$result=mysql_query($query,$db_conn);
?>

Code: Select all

&lt;HTML&gt;
&lt;select name="category"&gt;
    &lt;?while($row=mysql_fetch_assoc($result))&#123;
        print "&lt;option&gt;".$row&#1111;category_name];
    &#125;?&gt;
&lt;/select&gt;
&lt;/HTML&gt;
Then do whatever form/javascript processing you have in mind.

Posted: Mon Jan 20, 2003 4:33 pm
by Lisa.W
Thank you very much for the prompt answer!

I will try your code straight away.

Best wishes,
Lisa.W

Posted: Tue Jan 21, 2003 3:27 am
by Skywalker
<?while($row=mysql_fetch_assoc($result)){
print "<option>".$row[category_name];
}?>

you write <? to open php try [<?php] insted of [<?]

?>