Destramic Posted April 15, 2007 Share Posted April 15, 2007 im selecting rows from my database but i want to put it into group/category displaying into a drop down list <select></select> so everything begining with the letter C in one category having a label at the top etc.. C - counter-stike : source c&c 3 D- Delta Forece 2 ... Ricky Link to comment https://forums.phpfreaks.com/topic/47074-category/ Share on other sites More sharing options...
Barand Posted April 15, 2007 Share Posted April 15, 2007 try <?php $sql = "SELECT id, name FROM categories ORDER BY name"; $res = mysql_query($sql) or die (mysql_error()."<p>$sql</p>"); echo "<SELECT 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"; ?> Link to comment https://forums.phpfreaks.com/topic/47074-category/#findComment-229597 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.