Jump to content

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/76404-search-for-results-based-on-3-parameters/
Share on other sites

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.