Current location: Hot Scripts Forums » Programming Languages » PHP » how to handle search function code..


how to handle search function code..

Reply
  #1 (permalink)  
Old 03-20-09, 07:45 AM
Miben Miben is offline
Wannabe Coder
 
Join Date: Sep 2008
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
how to handle search function code..

Im stuck to do a proper Search Function.
I need to do searching base on few criteria.

For example, book title, book serial number, book specific code number and so on....

Then way I use in my php code is,
if(!empty(book tiltle)&& !empty(book serial number) && !empty(book specific code number) && ...){
// sql code here
}
else if(!empty($something) && ...){
//sql code here
}

... ...

The problem is, if i have more 3 or more criteria need to be consider in, then i need to write a lot of if...else to determine which sql query and it will become more messy in coding.

Anyone, pls help me to solve my situation more organize or proper way????
pls help!!!
Reply With Quote
  #2 (permalink)  
Old 03-20-09, 09:07 AM
landing's Avatar
landing landing is offline
Coding Addict
 
Join Date: Jul 2006
Location: Scotland
Posts: 302
Thanks: 0
Thanked 0 Times in 0 Posts
Code:
SELECT * FROM `table` WHERE `book` LIKE '%$book_title%' AND `serial_number` LIKE '%$book_serial_number%' AND ...
... and so forth.

By using 1 query with the LIKE operator, it allows the user to perform a custom search. If they leave a field blank it will act as a wildcard character effectively ignoring the field.

If the user enters a book title and leaves all other fields blank, it will simply search for the book. It's quite good as it allows easy expansion and gives the user more control.

The downside is that users can be idiots... but that's another story.
__________________
Always sanitise your data


Best regards

Last edited by landing; 03-20-09 at 09:10 AM.
Reply With Quote
  #3 (permalink)  
Old 03-22-09, 08:50 PM
Miben Miben is offline
Wannabe Coder
 
Join Date: Sep 2008
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for your reply..

But, how if I got another text field for user to search by keyword???
In this keyword, user can key in any word they like.

This is the way i do:
Quote:
SELECT * FROM `table` WHERE book_id='%keyword%' OR book_name='%keyword%' OR book_type='%keyword%' AND book_title= '%$book_title%' AND serial_number='%$book_serial_number%' AND book_type="%book_type%" AND... so on
But, the search result i get by using the query above doesnt return exactly or aproached. The result seem like ignore all "AND" in my query.Means, no matter I put book_title, serial_number ..or not, it doesnt filter the book i search..

Any idea???
Reply With Quote
  #4 (permalink)  
Old 03-22-09, 10:09 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
Group them...

Code:
SELECT * FROM `table` WHERE (book_id='%keyword%' OR book_name='%keyword%' OR book_type='%keyword%') AND book_title= '%$book_title%' AND serial_number='%$book_serial_number%' AND book_type="%book_type%" AND... so on
edit: srry, your syntax is wrong, only use wildcard (%) with LIKE.

Code:
SELECT * FROM `table` WHERE (book_id LIKE '%keyword%' OR book_name LIKE '%keyword%' OR book_type LIKE '%keyword%') AND book_title LIKE '%$book_title%' AND serial_number LIKE '%$book_serial_number%' AND book_type LIKE '%book_type%' AND... so on

Last edited by Jcbones; 03-22-09 at 10:13 PM.
Reply With Quote
  #5 (permalink)  
Old 03-22-09, 11:06 PM
dgreenhouse's Avatar
dgreenhouse dgreenhouse is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: San Francisco
Posts: 457
Thanks: 0
Thanked 3 Times in 3 Posts
Here's a possibility:

Code:
<?php

$_POST['book_title'] = 'Bambi does Hawaii!';
$_POST['book_sn'] = '12342';

$criteria = array(
  'book_title',
  'book_sn',
  'book_code'
);

$query_base = "SELECT from table";

$where = '';

foreach($criteria as $criterion) {
  if (isset($_POST[$criterion])) {
    if (!empty($where)) {
      $where .= ' AND';
    } else {
      $where .= ' WHERE';
    }
    $where .= ' ' . $criterion . ' LIKE "%' . $_POST[$criterion] . '%"';
  }
}

$query = $query_base . $where;

print $query;

?>
You can embellish it with ON ORDER based on which WHERE clauses get appended later.

Adding OR or parenthetical logic; which could be done using the $_POST or $criteria array(s), makes it a bit more complicated, but that's for another post.

Last edited by dgreenhouse; 03-22-09 at 11:15 PM.
Reply With Quote
  #6 (permalink)  
Old 03-24-09, 12:56 AM
Miben Miben is offline
Wannabe Coder
 
Join Date: Sep 2008
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for help...
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Calculating Age Help jamesbuk PHP 9 03-07-09 01:21 PM
[SOLVED] searching through a grid view painthu ASP.NET 5 05-21-08 10:11 AM
Full Site Search Function loeddie PHP 1 03-22-05 02:33 AM
Multi lingual search code wild_honey ASP 0 02-26-05 04:13 AM
Object Oriented Programming Stefan PHP 29 12-30-03 11:22 AM


All times are GMT -5. The time now is 08:31 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.