Advanced search

General discussions related to php

Moderators: macek, egami, gesf

Post Reply
User avatar
ejsexton82
New php-forum User
New php-forum User
Posts: 86
Joined: Mon Jun 04, 2012 10:05 pm
Location: Ankara, Turkey
Contact:

Re: Advanced search

Post by ejsexton82 » Fri Jun 15, 2012 5:02 am

Yes, it is possible to have an Advanced Search which allows users to fill in one or more fields. Solutions usually involve a series of conditionals which build an SQL statement based on the information submitted by the user.

Assuming you are using MySQL, such a solution might look like this:

Code: Select all

$where = array();
if(!empty($_POST['name'])) {
	$where[] = sprintf("`name` LIKE '%%%s%%'",
		mysql_real_escape_string($_POST['name']));
}
if(!empty($_POST['description'])) {
	$where[] = sprintf("`description` LIKE '%%%s%%'",
		mysql_real_escape_string($_POST['description']));
}

// Repeat for all other fields...

$sql = "SELECT * FROM `table` WHERE " . implode(' OR ',$where) . ";";
As far as efficiency, it depends on how your application is setup. Optimizing the query in PHP before sending it to an SQL database could improve the performance of your application. However, if you are using an existing Stored Procedure in an SQL database to perform the search, it may save you time to optimize the Stored Procedure, rather than mess with PHP.

I hope that helps. Good luck!

Post Reply

Who is online

Users browsing this forum: Baidu [Spider] and 1 guest