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
Select with Optgroup - populated from MySQL Database
Old 09-24-2011, 11:59 AM Select with Optgroup - populated from MySQL Database
pjb007's Avatar
Super Talker

Posts: 104
Location: UK
Trades: 0
I am trying to get a select box with optgroup to populate from a MySQL database. The problem is I can't see how to get the WHERE clause to work.

Here is an example of the code I am using. Currently it displays the optgroups correctly (these are manufacturers) but it lists all cars.

How can I add a WHERE clause to get only cars that match the manufacturer to appear under the optgroup.

Code:
<select>
  <?php
	$manufacturer  = mysql_query("SELECT * FROM cars");
	while($manufacturer_row = mysql_fetch_array($manufacturer )){
    echo "<optgroup label='$manufacturer_row[manufacturer]'>"; 

	$car = mysql_query("SELECT * FROM cars");
	 
	while($car_row = mysql_fetch_array($car)){
    echo "<option>$car_row[car]</option>"; 
	}
	}
  ?>
</optgroup>
</select>
__________________
pjb007
pjb007 is offline
Reply With Quote
View Public Profile Visit pjb007's homepage!
 
 
Register now for full access!
Old 09-24-2011, 12:11 PM Re: Select with Optgroup - populated from MySQL Database
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Use an "ORDER BY" criteria in your query and check when the "manufacturer" column data changes, then insert your optgroup.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 09-24-2011, 12:33 PM Re: Select with Optgroup - populated from MySQL Database
pjb007's Avatar
Super Talker

Posts: 104
Location: UK
Trades: 0
How do you check for changes in a column?
__________________
pjb007
pjb007 is offline
Reply With Quote
View Public Profile Visit pjb007's homepage!
 
Old 09-24-2011, 12:50 PM Re: Select with Optgroup - populated from MySQL Database
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
So, you want to list all cars under their respective manufacturer?
PHP Code:
// run your query, something like
// "select manufacturer, car from cars order by manufacturer"

// manufacturer of the previous car
$previous "";

// just a little help variable
$first_car true;

while(
$row mysql_fetch_array($manufacturer)) {

    
// if this car is of another manufacturer than the last one,
    // add a new optgroup
    
if ($row['manufacturer'] != $previous) {
        if (!
$first_car) {
            echo 
'</optgroup>';
        } else {
            
first_car false;
        }
        echo 
'<optgroup label="' $row['manufacturer'] . '">';
        
$previous $row['manufacturer'];
    }

    
// print the current car
    
echo '<option>' $row['car'] . '</option>';

}
// close the last optgroup tag
echo '</optgroup>'
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.

Last edited by lizciz; 09-24-2011 at 01:01 PM..
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 09-24-2011, 01:53 PM Re: Select with Optgroup - populated from MySQL Database
pjb007's Avatar
Super Talker

Posts: 104
Location: UK
Trades: 0
Thanks that works - but I need to add OrderBy to both elements (the car and the manufacturer) to ensure they stay in order.

When I do this I get duplicate groups.
__________________
pjb007
pjb007 is offline
Reply With Quote
View Public Profile Visit pjb007's homepage!
 
Old 09-24-2011, 02:02 PM Re: Select with Optgroup - populated from MySQL Database
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
You mean like this?
"select manufacturer, car from cars order by manufacturer, cars"
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 09-24-2011, 06:05 PM Re: Select with Optgroup - populated from MySQL Database
pjb007's Avatar
Super Talker

Posts: 104
Location: UK
Trades: 0
thanks - thats done it!
__________________
pjb007
pjb007 is offline
Reply With Quote
View Public Profile Visit pjb007's homepage!
 
Reply     « Reply to Select with Optgroup - populated from MySQL Database
 

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.25046 seconds with 12 queries