select from loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

select from loop

Post by aceconcepts »

Hi,

I have a mysql database and one table is a 'product' table.

This table consists of product names, prices and types.

What i would like to be able to do is display a list of each product type that is different.

For example:

product table consists of

Name Price Type
------------------------------------------------------
giraffe £1.00 mask
lion £1.00 mask
whistle £2.00 toy
ball £1.50 toy
t-shirt £5.50 clothing
------------------------------------------------------

I want to be able to display a list of each different product type.

e.g. from the table above the list would look like:

List
--------------
mask
toy
clothing
--------------

How can i do this?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

select * from table where type = 'toy'
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

That won't work because i want to extract all different product 'type' without specifying one.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

ooooh ok

Code: Select all

$types = array('toy','clothing','mask');
foreach($types AS $type){
   SELECT * FROM `table` WHERE `type` = $type;
}

// or
// kidna guessing on this one, sorry 
SELECT * FROM `table` GROUP BY `type`
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

SELECT DISTINCT `type` FROM `table`
Post Reply