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-23-2007, 05:26 PM   #1 (permalink)
trevor
Code Monkey
 
Join Date: Jan 2003
Location: Canada
Posts: 91
trevor is on a distinguished road
basic search engine with PHP

Hi everyone,

I'm back

Yeah, I am having a little trouble getting a basic search engine working on a website that I'm building.
I got one working where it is only looking for a single word, ie.

search by city - user enters "toronto",

and I have a line:

$query = "SELECT * FROM users WHERE city LIKE 'search' order by Id";

and that works fine because I only have to search one field with one word.

But I need to add a search engine that searches for users by first and last name.

So say someone enters 'John Smith' and my database is set up where the users name is stored under 'first' and 'last'.

Sorry my PHP is pretty rusty, I played around with it a fair bit a few years ago (some people here may remember me), but anyways I took on a little project to I have to learn it all again :-)

I hope that makes sense.

thanks,
Trevor
trevor is offline   Reply With Quote
Old 02-23-2007, 11:23 PM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,692
redhead is on a distinguished road
How about something like this:
PHP Code:
$string $_POST['search'];
/* split string at spaces */
$tok strtok($string" ");
$items = array();
/* fetch every item from the list */
while ($tok !== false) {
   
$items[]=$tok;
   
$tok strtok(" ");
}
/* create SQL statement */
if(count($items)){
  
/* theres somethign to search for */
  
$query="SELECT * FROM users WHERE ";
  foreach(
$items as $item){
    
/* we have no knowledge of how many items they've entered */
    
if(isset($been_here))
      
$query .= " OR ";
    
/* we dont know if they entered "firstname lastname" or "lastname firstname" */
    
$query .= "firstname LIKE \"%$item%\" OR lastname LIKE \"%$item%\"";
    
$been_here 1;
  }

__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001

Last edited by redhead; 02-24-2007 at 02:44 AM.
redhead is offline   Reply With Quote
Old 02-24-2007, 05:26 AM   #3 (permalink)
trevor
Code Monkey
 
Join Date: Jan 2003
Location: Canada
Posts: 91
trevor is on a distinguished road
awesome I'll give it a try. Thanks!
trevor is offline   Reply With Quote
Old 02-24-2007, 07:43 AM   #4 (permalink)
trevor
Code Monkey
 
Join Date: Jan 2003
Location: Canada
Posts: 91
trevor is on a distinguished road
Yeah, awesome, I got it workin now, at least I think so. Thanks redhead!
trevor 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
Designing a search engine simpler than GOOGLE apponez Program Design and Methods 4 03-06-2006 08:02 PM
Make a search engine for your website with PHP zhisede PHP 3 10-25-2005 07:54 AM
PHP 5.0.4 and 4.3.11 Released sde Code Newbie News 0 04-20-2005 10:56 AM
Search Engine Submittions Eyelfixit Lounge 12 07-25-2003 08: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 06:34 PM.


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