Solarpitch Posted November 7, 2007 Share Posted November 7, 2007 Hey Guys, I am trying to code a serarch for results page which will contain 3 parameters... Category Any Apartment Cottage House Location Any Dublin Cork Meath Price Any Price 20000 - 30000 40000 + I just want to know whow I handle the sql statement if the search was like.. Category: Any Location: Any Price: 40000+ I am not quite sure how to construct the sql statement based on whether the user has choosen All/ Any of for a specific item as All/ Any would use Select * from.. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/76404-search-for-results-based-on-3-parameters/ Share on other sites More sharing options...
Rhysyngsun Posted November 7, 2007 Share Posted November 7, 2007 I would generate your SQL statements by concatenating a conditional statement on only if the parameter was not equal to "Any". That way if they do chose "Any", you simply don't append that condition into your SQL statement. If they choose a specific option, then you simply append the relevant condition since that's the only one you're checking for. Quote Link to comment https://forums.phpfreaks.com/topic/76404-search-for-results-based-on-3-parameters/#findComment-386897 Share on other sites More sharing options...
Solarpitch Posted November 7, 2007 Author Share Posted November 7, 2007 Yeah I was think of that alright... just use a switch statement or something. I'll give it a go anyway. cheers Quote Link to comment https://forums.phpfreaks.com/topic/76404-search-for-results-based-on-3-parameters/#findComment-386910 Share on other sites More sharing options...
Solarpitch Posted November 7, 2007 Author Share Posted November 7, 2007 Umm.. I am trying this here but seem to be getting a little lost. Can anyone just show me a quick one line example? Quote Link to comment https://forums.phpfreaks.com/topic/76404-search-for-results-based-on-3-parameters/#findComment-386932 Share on other sites More sharing options...
Rhysyngsun Posted November 7, 2007 Share Posted November 7, 2007 Say we assign each category, location, etc. choice an id for simplicity and extensibility ("Any" option will always be 0): $hasWhere = false; $q = "SELECT * FROM `table`"; if( $category != "0" ) { if( !hasWhere ) $q .= " WHERE "; $q .= " 'categoryid' = `$category`"; $hasWhere = true; } And so on... You will have to make sure you keep track of whether or not you have WHERE in your query yet, as you should have it only once and only then if you have statements to go with it. Quote Link to comment https://forums.phpfreaks.com/topic/76404-search-for-results-based-on-3-parameters/#findComment-387057 Share on other sites More sharing options...
fenway Posted November 7, 2007 Share Posted November 7, 2007 Easier to push() onto an array and join with AND at the end. Quote Link to comment https://forums.phpfreaks.com/topic/76404-search-for-results-based-on-3-parameters/#findComment-387060 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.