Advanced search

General discussions related to php

Moderators: egami, macek, gesf

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

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%%'",
if(!empty($_POST['description'])) {
	$where[] = sprintf("`description` LIKE '%%%s%%'",

// 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