Jump to content

category


Destramic

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.