Forums

PHP

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>
&lt;?php require_once('../Connect/reg.php'); ?&gt;
&lt;?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);
?&gt;

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;form name="form1" method="get" action=""&gt;
&lt;select name="title" id="title"&gt;
&lt;option value=""&gt;please select&lt;/option&gt;
&lt;OPTGROUP LABEL="first options"&gt;
&lt;?php
do {
?&gt;
&lt;option value="&lt;?php echo $row_Recordset1['title']?&gt;"&gt;&lt;?php echo $row_Recordset1['title']?&gt;&lt;/option&gt;
&lt;?php
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
$rows = mysql_num_rows($Recordset1);
if($rows &gt; 0) {
mysql_data_seek($Recordset1, 0);
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
}
?&gt;
&lt;/OPTGROUP&gt;
&lt;OPTGROUP LABEL="second options"&gt;
&lt;?php
do {
?&gt;
&lt;option value="&lt;?php echo $row_Recordset2['title']?&gt;"&gt;&lt;?php echo $row_Recordset2['title']?&gt;&lt;/option&gt;
&lt;?php
} while ($row_Recordset2 = mysql_fetch_assoc($Recordset2));
$rows = mysql_num_rows($Recordset2);
if($rows &gt; 0) {
mysql_data_seek($Recordset2, 0);
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
}
?&gt;
&lt;/OPTGROUP&gt;
&lt;/select&gt;
&lt;/form&gt;
&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;

&lt;?php
mysql_free_result($Recordset1);

mysql_free_result($Recordset2);
?&gt;


</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.

Reply to this topic