Click to See Complete Forum and Search --> : Searching in a table for some items, but not all


jpony
07-13-2007, 11:47 PM
Hey there!

I'm trying to learn PHP so i've set up a little challenge for myself.

I want to create a table of items. Let's say food products. Each food product will have some attributes... how about the "meal" (4 options: breakfast, lunch, snack, dinner), ingredients (comma separated values), origin (western, Chinese, etc.), and the name of the item (cookie, etc.).

I've figured out how to create items in a table... now I need to figure out how to search.

Is it possible to search for some attributes, but not all?

Lets say I wanted a desert with chocolate in it. Can that be done?

Thank you so much for your help!

Cheers,

knowj
07-14-2007, 04:46 AM
do you mean MySQL/Postgre or a HTML table?

kvirri
07-14-2007, 05:21 AM
If you go with mysql or some other sql compatible database, try something like

SELECT * FROM table WHERE name=value AND name=value

or considering something like your table

SELECT recipe FROM food_products WHERE `origin`='Chinese' AND`meal`='desert' AND `ingredients`='%chocolate%'

(though I'm not really aware of any chinese deserts with chocolate :P)

ok, for the explaining: the reverse apostrophes like `col`, define a column or table, I'm used to writing all columns but it's gennerally used only on columns/table with spaces in their name

the '' in 'value' define a value (again, a string with spaces in it). Careful, it's a single apostrope, not inverted commas.

the % in the chocolate is a wildcard (like * is for windows file systems) which says "ingredients contains 'chocolate'". We're using it, since there could be several ingredients.

Hope this helps.

jpony
07-14-2007, 10:33 AM
Oh that's brilliant! Thank you! I'll give it a shot later today.

Quest question though: what happens if there is more than one dessert with chocolate in it? Can it return more than one item? If a "searcher" wanted to find all of the western (he he) desserts with chocolate in it, can this be done?

Again, I really appreciate you help!