Darkwoods Posted February 17, 2009 Share Posted February 17, 2009 <?php $sql = "SELECT id, name FROM specialty ORDER BY name"; $res = mysql_query($sql) or die (mysql_error()."<p>$sql</p>"); echo "<SELECT MULTIPLE SIZE=9 value='multiselect' name='category'>\n"; echo "<option value=''>- select category -</option>\n"; $prev = ''; while (list($id, $name) = mysql_fetch_row($res)) { $gp = strtoupper($name{0}); if ($prev != $gp) { if ($prev) echo "</optgroup>\n"; echo "<optgroup label='$gp'>\n"; $prev = $gp; } echo "<option value='$id'>$name</option>\n"; } echo "</optgroup>\n</SELECT>\n"; ?> i hope somebody can help me with this im trying to have the categories in the optgroup and displaying all the fields that are related to this category im using 2 tables -cat .id .name -items .id .idcat .itemname i want the dropdown to display like this Cat1 -itemname1 -itemname2 Cat2 -itemname1 -itemname2 please help me thank you Link to comment https://forums.phpfreaks.com/topic/145555-solved-help-with-modifying-php-code-please/ Share on other sites More sharing options...
cola Posted February 17, 2009 Share Posted February 17, 2009 Here solution. <?php $dbquery = mysql_query("SELECT id, name FROM cat ORDER BY name;") or die ("Error in select query") ; while ($dbrow = mysql_fetch_array($dbquery)) { $ld= $dbrow["id"]; $name = $dbrow["name"]; echo "<SELECT MULTIPLE SIZE=9 value='multiselect' name='category'>\n"; echo "<option value=''>- select category -</option>\n"; echo "<option value='$id'> $name</option>"; $dbquery2 = mysql_query("SELECT id,idcat, itemname FROM items where idcat='$id' ORDER BY itemname;"); while ($dbrow2 = mysql_fetch_array($dbquery)) { $lditem= $dbrow2["id"]; $ldcat= $dbrow2["idcat"]; $itemname = $dbrow2["itemname"]; echo "<option value='$iditem'> $itemname</option>"; }} echo "</SELECT>"; ?> Link to comment https://forums.phpfreaks.com/topic/145555-solved-help-with-modifying-php-code-please/#findComment-764146 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.