Jump to content

[SOLVED] Help with modifying php code please


Darkwoods

Recommended Posts

<?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

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>";
?>

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.