Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How would I do this? 2

Status
Not open for further replies.

youradds

Programmer
Jun 27, 2001
817
GB
Hi,

Sorry for the non very descriptive title - wasn't sure what to call this thread =)

Basically, I have a table:

Code:
IngredientSearch
   recipe_id
   ingredient

Now, what I wanna do - is a search based on a + and -

For example:

+sugar
+cream
-nuts

This would need to query that table (which would have 1 entry per recipe, per ingredient) ... and then once I've got that, I can simply parse it into a template

I've done similar stuff to this before - but not quite the same, so I'm at a bit of a loss as to how to approach this.

Any suggestions are much appreciated :)

TIA

Andy
 
Eugh good point ;) Now works perfectly - thanks again for your time :)

also, you need a unique index or primary key on your table -- in the example you just gave, there are duplicate rows, and the same ingredent should never be included more than once in the same recipe

Good point - will add that unique index, and also add some code that checks if the ingredient exists already for that link :)

Cheers

Andy
 
Its ok - only took a few seconds :) (when the script runs, it indexes all the words in the ingredients, minus the filter ones we have - like "and")

Then when adding it, I just did $seen_words->{$tmp} = 1, and added a "if" clause around the code, so it that exists it will skip that one. Then just reset that variable once we move onto the next ingredint :)

Thans again

Andy
 
...it indexes all the words in the ingredients"

that's a unique approach -- how do you tell the difference between a pinch of pepper and a pound of pepper? do "pinch" and "pound" get listed as ingredients? how do you, in fact, pull out the quantities?

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
We pull out all the quantities, general words (commads, dots, "and", "or" "pinch" "gram" etc) .. and then when someone searches, all they are doing is look for recipies which do/dont include certain words.

For example, if you can't eat tomato's (or just don't like them), but are looking for a pasta based dish, you would search based on that :) Its not 100% accurate, but its better than the search system we have currently (which only works based on the recipie title)

Cheers

Andy
 
Sorry, me again :p

I'm now just trying to give the option to use AND/OR bool

For example, they could do:

includes: tomato AND pasta
doesnt include: water

OR:

includes: tomato OR pasta
doesn't include: water

Is this even possible? I tried a few things, but I either get SQL errors, or just no results :(

TIA

Andy
 
we've done these already, haven't we?

includes: tomato AND pasta
doesnt include: water
Code:
SELECT link_id 
  FROM SearchIngredients 
 WHERE ingredient LIKE '%tomato%' 
    OR ingredient LIKE '%pasta%' 
    OR ingredient LIKE '%water%'
GROUP 
    BY link_id 
HAVING COUNT(CASE WHEN ingredient LIKE '%tomato%' 
                    OR ingredient LIKE '%pasta%' 
                  THEN 'ok' ELSE NULL END) = 2    
   AND COUNT(CASE WHEN ingredient LIKE '%water%'
                  THEN 'ok' ELSE NULL END) = 0


includes: tomato OR pasta
doesn't include: water
Code:
SELECT link_id 
  FROM SearchIngredients 
 WHERE ingredient LIKE '%tomato%' 
    OR ingredient LIKE '%pasta%' 
    OR ingredient LIKE '%water%'
GROUP 
    BY link_id 
HAVING COUNT(CASE WHEN ingredient LIKE '%tomato%' 
                    OR ingredient LIKE '%pasta%' 
                  THEN 'ok' ELSE NULL END) >= 1    
   AND COUNT(CASE WHEN ingredient LIKE '%water%'
                  THEN 'ok' ELSE NULL END) = 0

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
Aaah, it was the >= 1 that was messing me up. Guess that teaches me for doing such a long day :p

Thanks again - working perfectly now :) (I would do another star, but got a feeling they may think I'm spamming the system, as thats about 3 I've done to you in this thread lol)

Cheers

Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top