Forums
This topic is locked
opt group in dynamic select menu
Posted 07 Jul 2005 17:41:30
1
has voted
07 Jul 2005 17:41:30 Geofrey Biggins posted:
Hi, i want to be able to put sub headings in my select menu that are populated from my mysql database.i have managed to get it to work with optgroup but it requires a recordset for every subheading i have. Is this too many recordsets on one page if i have for example 5 subcatagories. The code below is how far i have got, it would be greate if you can suggest how to improve it or suggest another way of doing it.
<pre id=code><font face=courier size=2 id=code>
<?php require_once('../Connect/reg.php'); ?>
<?php
mysql_select_db($database_registration, $registration);
$query_Recordset1 = "SELECT title FROM table WHERE menu = 'first'";
$Recordset1 = mysql_query($query_Recordset1, $registration) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
mysql_select_db($database_registration, $registration);
$query_Recordset2 = "SELECT title FROM table WHERE menu = 'second'";
$Recordset2 = mysql_query($query_Recordset2, $registration) or die(mysql_error());
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
$totalRows_Recordset2 = mysql_num_rows($Recordset2);
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form name="form1" method="get" action="">
<select name="title" id="title">
<option value="">please select</option>
<OPTGROUP LABEL="first options">
<?php
do {
?>
<option value="<?php echo $row_Recordset1['title']?>"><?php echo $row_Recordset1['title']?></option>
<?php
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
$rows = mysql_num_rows($Recordset1);
if($rows > 0) {
mysql_data_seek($Recordset1, 0);
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
}
?>
</OPTGROUP>
<OPTGROUP LABEL="second options">
<?php
do {
?>
<option value="<?php echo $row_Recordset2['title']?>"><?php echo $row_Recordset2['title']?></option>
<?php
} while ($row_Recordset2 = mysql_fetch_assoc($Recordset2));
$rows = mysql_num_rows($Recordset2);
if($rows > 0) {
mysql_data_seek($Recordset2, 0);
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
}
?>
</OPTGROUP>
</select>
</form>
</p>
</body>
</html>
<?php
mysql_free_result($Recordset1);
mysql_free_result($Recordset2);
?>
</font id=code></pre id=code> <b></b>
to ask is but a moments shame, not to ask and remain ignorant is a lifelong shame.