I know I run the risk of getting flamed like TV Evangelist on Judgment Day, but my Ox is in a Ditch and I need help ASAP. I have a real estate property table…has about 120 fields. I have a search form written. It searches keywords, Property Type or Sub Type, minimum price, maximum price, square ft, etc…
What I need is to have the search find anything that meets criteria OR return all if they just select any on the drop down. I’m pushing the ‘%’ character if they select any from the list.
Here’s my 1st stab which results in 0 results
select * from properties
where AdvSubDiv LIKE '%'
AND PropType = '%'
AND Sub_Type = '%'
AND ListPrice BETWEEN '100000' AND '150000'
AND NumBeds = '%'
AND NumFBath = ''
AND SqrFeet BETWEEN '%' AND '%'
ORDER by SRENumber
variables are being passed by php as $_POST. The example above is an “Any” search…basically leaving the form blank and hitting the submit button…
when the form is submitted, you must inspect each form field to see if nothing is in it, or if the “any” option has been chosen (both of which actions amount to the same thing, right?)
okay, for each form field where the user wants all rows for that field, here’s what you do – omit the corresponding column condition from the WHERE clause
don’t try to use a wildcard or anything – just omit that condition from the WHERE clause
Thanks for that! So cool that you guys are willing to help.
I know beggars can’t be choosers…but I’ll beg any way. is there any way you could give an example of how those conditional statements would look synactically?
You have already helped tremendously. thanks again
Just to update everybody, this worked perfectly once it was tailored to suit it’s purpose. Kudos to Dave! I wouldn’t worry about your php skills…that’s what httpd error_log is for, eh?
BTW, this was ported into a customized Drupal mod with just a few hacks to get the pager working on the results list…who says you can’t learn something on a Friday?
It’s very slick little idea there that I had never thought of…
I do have this one weird tick with it now tho…if someone searches for a keyword that starts with the letter n (e.g. the word new), the SQL statement looks like this:
SELECT * FROM properties WHERE AdvSubDiv LIKE '%0ew%' ORDER BY SRENumber LIMIT 0, 5
0.91
I’ve made sure that it’s escaping characters…but that’s the only thing that’s still stumpin’ me. and this may very well be a PHP issue now and not relevant to these forums.