Justtechjobs.com Find a programming school near you






Online Campus Both


php-general | 2001062

Re: [PHP] [OT-ish] Optional Extras. From: Richard Lynch (ceo <email protected>)
Date: 06/27/01

How about solving both problems at once? :-)

Yes, go with the N:N (the technical term for that car_option table)
relation, *AND* give yourself a weighted search engine to boot!

create table car (car_id auto_increment...);
create table option (option_id auto_increment...);
create table car_option(car_id int4, option_id int4, key(option_id));

insert into car(name) values('Chevy Nova');
insert into car(name) values('Lexus');
insert into option(name) values('a/c');
insert into option(name) values('power window');
insert into option(name) values('power locks');
insert into car_option(car_id, option_id) values(1, 1);
insert into car_option(car_id, option_id) values(2, 1);
insert into car_option(car_id, option_id) values(2, 2);
insert into car_option(car_id, option_id) values(2, 3);

NOTE: The Lexus has all those options (at that price it had better!) and
the Nova, well... It has a/c! :-)

Now, assume the user wants everything, so you have:
$options[1]
$options[2]
$options[3]
all "set" to some value by your checkbox.

#Untested code.
$query = "select count(car_id), car.name from car, car_option ";
$query .= " where car.car_id = car_option.car_id "
$query .= " and (1 = 0";
# 1 = 0 is a "starter yeast" for the following:
while (list($option_id) = each($options)){
    $query .= " or option_id = $option_id ";
}
# finish off our options OR list...
$query .= ")";
$query .= " group by car_id ";

--
WARNING richard <email protected> address is an endangered species -- Use
ceo <email protected>
Wanna help me out?  Like Music?  Buy a CD: http://l-i-e.com/artists.htm
Volunteer a little time: http://chatmusic.com/volunteer.htm

-- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-general-unsubscribe <email protected> For additional commands, e-mail: php-general-help <email protected> To contact the list administrators, e-mail: php-list-admin <email protected>