Jump to content

php search feature with additonal search options form


jrheeder

Recommended Posts

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 :confused:

 

Any advise will be appretiated.

 

Regards,

J

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");
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.