Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 02-15-2005, 04:37 PM   #1 (permalink)
nameless
Registered User
 
nameless's Avatar
 
Join Date: Aug 2002
Posts: 4
nameless is on a distinguished road
search funtion

Hey, I use to use the forums regularily a few years ago but since then I kinda stopped coding and started pursuing other projects.
Anyways a new website fell into my lap that I am making for someone and I need to write a search function that basically allows the user to search my mysql database for a name - first and last.

What I dont get and I might have my database set up wrong, but I have the first and last name in different fields, and if a user searches for a full name, what would be the easy way for my search code to find it? What if they only enter half the name etc?
I am sure it is a pretty simple concept just my brain has frozen up on me.

Thanks,

Trevor
nameless is offline   Reply With Quote
Old 02-15-2005, 05:21 PM   #2 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 596
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
$SQL = sprintf( "SELECT ID FROM Person WHERE nameLast = '%s' AND nameFirst = '%s' ", $lastName, $firstName);
teknomage1 is offline   Reply With Quote
Old 02-15-2005, 06:25 PM   #3 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
Split the input name into two vars (or an array) and do something similar to tekno's SQL statement. Although depending on the requirements, you might want to use the "LIKE" statement to perform partial matches.

For a loose search I typically do something like:

eg:
PHP Code:
$k 'SMITH, MIKE';
$k preg_replace('/\,/'' 'trim($k));
$k preg_replace('/\s+/'' 'trim($k));
$terms explode(' 'trim($k));

$sql '(';
while(list(
$i$v) = each($terms)) {

   if (
$i 0) {
      
$sql .= 'OR ';
   }

    
$sql .= "(last_name LIKE '%{$v}%' OR first_name LIKE '%{$v}%') ";
}
$sql .= ')';

$sql "SELECT ID FROM table_name WHERE {$sql}";
echo 
$sql
idx is offline   Reply With Quote
Old 07-15-2005, 04:17 PM   #4 (permalink)
nameless
Registered User
 
nameless's Avatar
 
Join Date: Aug 2002
Posts: 4
nameless is on a distinguished road
Ok yeah, its crazy how easily I get distracted from something, and end up not working on it again for 2-3months.
Anyways yeah, I dunno what I am doing wrong but I am a little confused, and neither one of those scripts seem to be working for me......is it possible they can be simplified?

thanks
nameless is offline   Reply With Quote
Old 07-16-2005, 08:33 AM   #5 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
I'm not sure. That code bit was highly trimmed down from my real version. Were you getting parse errors, or was it just not finding anything?

-r
idx is offline   Reply With Quote
Reply

Bookmarks

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
SUID/GUID files search ! jackjill Linux / BSD / OS X 1 02-01-2005 05:25 AM
Search keystoneman Standard C, C++ 8 10-06-2004 07:04 PM
Britain cracks down on paid search sde Code Newbie News 0 06-16-2004 06:53 AM
Search -That Can Match Out Of Order Items JBurke All Other Coding Languages 11 09-25-2003 02:09 PM
Please Review ZapMeta - New Search Engine khn Program Design and Methods 3 07-06-2003 09:15 AM


All times are GMT -8. The time now is 12:17 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting