jrheeder Posted June 10, 2011 Share Posted June 10, 2011 Hey Peeps, I am currently developing a search feature using php/mysql now the question I have is how would I go about adding addtional search options to my search feature? Example: User serached for Name -> John. search feature lists all people with the name John in the database. The user want to narrow the result by Johns that are 50 and over in age and lives in England. I got the first part hwo do I implement the "narrowing of results" feature Any advise will be appretiated. Regards, J Quote Link to comment https://forums.phpfreaks.com/topic/238974-php-search-feature-with-additonal-search-options-form/ Share on other sites More sharing options...
The Little Guy Posted June 10, 2011 Share Posted June 10, 2011 Maybe something like this: <?php $filter = array(); if(isset($_POST['name'])){ $name = mysql_real_escape_string($_POST['name']); $filter[] = "name = '$name'"; } if(isset($_POST['age'])){ $age = mysql_real_escape_string($_POST['age']); if($_POST['age_type'] == 'greater_than') $filter[] = "age > '$age'"; elseif($_POST['age_type'] == 'equal_to') $filter[] = "age = '$age'"; elseif($_POST['age_type'] == 'less_than') $filter[] = "age < '$age'"; else $filter[] = "age = '$age'"; } if(isset($_POST['location'])){ $location = mysql_real_escape_string($_POST['location']); $filter[] = "location = '$location'"; } $filter_string = implode(' and ', $filter); if(strlen($filter_string) > 0) $filter_string = "where $filter_string"; $sql = mysql_query("select * from table $filter_string"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/238974-php-search-feature-with-additonal-search-options-form/#findComment-1227951 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.