Jump to content

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

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.