dynamic optgroup drop down menu using mysql & php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dm404
Forum Newbie
Posts: 2
Joined: Sun Apr 13, 2008 8:12 am

dynamic optgroup drop down menu using mysql & php

Post by dm404 »

Hi

I'm a newbie and have only been learning for four weeks or so. I am practicing what i've read and so am creating my first web application

I'm designing a system where venues need to register, I have mostly completed the registration page but am struggling with one particular aspect.

The database consists of the tables:

venue(Where foreign key=AreaID)
area(AreaID, AreaName, AreaRegion)

When venues register they must select an AreaID. At the moment I have dynamically created a drop down box that is populated by querying the area table in mysql. It sorts the option by AreaName and the values correspond to the AreaID.

Whilst I was initially happy, I've realised that the sheer number of areas mean it will have to be sorted by AreaRegion. I think optgroup can be used to achieve this. The AreaRegion column is type enum('Scotland', 'Wales',....) with more regions to be added.

I've done a little research, but have ended up more confused than before! Any suggestions would be much appreciated. My code at the moment:

<p><b>Select Area:</b><select name="areaid">

Code: Select all

 
<?php
 
$query1 = "SELECT AreaID, AreaName from area ORDER BY AreaName ASC";
$result1=@mysql_query($query1);
 
while ($row = @mysql_fetch_array($result1)) {
print '<option value="' . $row['AreaID'] . '">' . $row['AreaName'] . '</option>';
}
?>
 
 
</p>
</select>


Thanks in advance,

Daniel
Post Reply