Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Help with modifying php code
Old 02-17-2009, 08:45 AM Help with modifying php code
Novice Talker

Posts: 6
Trades: 0
Code:
<?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 some 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

Code:
-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
Darkwoods is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-17-2009, 03:20 PM Re: Help with modifying php code
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
It's a bit unclear what you want but I'll try. Im not really *that* good with SQL yet, though, but how about this?

PHP Code:
// The SQL query
// SELECT name, items.id as itemID, itemname FROM cat,items WHERE cat.id=items.idcat GROUP BY name

// run query etc.

$prev '';
$newGroup false;
while(
$row mysql_fetch_assoc($result)) { // or use fetch_row if you prefer that
   
$newGroup = ($prev == $row['name']) ? false true;
   
$prev $row['name'];
   if (
$newGroup) {
      
$group ucfirst($row['name']); // first capital letter
      
echo "<optgroup label=\"$group\">";
   }
   echo 
"<select value=\"" $row['itemID'] . "\">" $row['itemname'] . "</select>";
   if (
$newGroup) {
      echo 
"</optgroup>";
   }


Last edited by lizciz; 02-17-2009 at 03:22 PM..
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Reply     « Reply to Help with modifying php code
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.11228 seconds with 12 queries