Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

Advanced search

General discussions related to php

Moderators: macek, egami, gesf

Advanced search

Postby letiti » Thu Jun 14, 2012 10:19 am

Hi, I have a problem.. I'm trying to do an advanced search. It is possible that the user fill some fields or fill all fields? How can I do it? It's more efficient use SQL statement or PHP?
letiti
New php-forum User
New php-forum User
 
Posts: 1
Joined: Thu Jun 14, 2012 10:16 am

Re: Advanced search

Postby 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!
User avatar
ejsexton82
New php-forum User
New php-forum User
 
Posts: 86
Joined: Mon Jun 04, 2012 10:05 pm
Location: Ankara, Turkey


Return to PHP General

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron