WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : How to build a search query for any N number of words in a search string
Categories : PHP, Regexps, Search Engines, Search Update Picture
Kelvin Poon
Date : Mar 14th 2003
Grade : 4 of 5 (graded 1 times)
Viewed : 12334
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Kelvin Poon
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

the Problem:

I am making a search engine and I would have the user to type a sentence to search for. Let's say
it is stored in $search. My search engine will bring up a result If and only if ALL of the words
in $search exist in the result. For example if

$search = Vax Useful Commands

Then the result is only true if

<?
(eregi(".*Vax.*Useful.*Commands.*", 'possible result'))
?>

is true

Now, I don't know how many words there are in $search initially. How do I do a search like that?
I mean if I know $search is 3 words then I can do

<?
$words = preg_split("/ /", $search);

if (eregi(".*words[0].*words[1].*words[2].*", 'possible result')) {
.....
}
?>

Even if I know how many words there are, every time the number of words in $search can be different.
another note is that my data is taken from a database.

The Solution:

If you have used explode() you can do a loop that goes through the array, for instance

<?
$searchwords=explode(' ', $search);

for ($x=0;$x<count($searchwords);$x++){
//build the search command OR build the query
// i recommend to make it a query, so:

$WHERE.= ' AND textfield=LIKE '"%'.$searchwords[$x].'%"';
}

//now cut off the first ' AND '
$WHERE=substr($WHERE,4,strlen($WHERE));


//now finish the query
$query="SELECT title,text,ID FROM tablename ".$WHERE;
?>

then proceed by doing the query and showing the results.



SiteSearch 1.1: This app lets end users search your site for keywords. You specify which directories should be included in the search.
Categories : Search, Search Engines, PHP
Extract keywords from a string having words in " " count as one string.
Categories : PHP, Strings, Regexps, Search
Simple way to replace a variable value in a .conf (.ini) file using a webbrowser - the first stage of a complete universal configuration editor
Categories : PHP, Regexps, Code Editors, Filesystem
columned txt file to array()?
Categories : Arrays, Strings, Regexps, PHP
ereg -- Regular expression match
Categories : PHP, PHP Functions, Regexps
Calculate Body Mass Index
Categories : PHP, Algorithms, Regexps
Word normalizing
Categories : Search, PHP
php table decoder used to convert an html table to individual tokens through regular expressions
Categories : PHP, Regexps, HTML and PHP
Designed to help those implementing a MySQL search engine. Generates select query strings, given logical querys.I made a search engine class that extends this one mail me if intrested.Needs a few changes ( well its only version 1.0 ;)
Categories : PHP Classes, MySQL, Search, Regexps
An array of functions to use in checking user input to HTML forms : text, firstName, middleNameOrInit, lastName, email, web, digits, decimal, hex, genNum, USD, BPS, Euro, USphone, USzip
Categories : PHP, Data Validation, Regexps
DirtSearch Version 3.5 full function robust PHP and MySQL (and other databases) Site or Web Wide Search Engine
Categories : PHP, MySQL, Complete Programs, Search, Databases
Extended Get File List Function
Categories : PHP, Filesystem, Search, Directories
Simple PHP program which calls other PHP program you can pass the variables to other PHP program : by Raju
Categories : PHP, PHP Options and Info, Regexps, Program Execution
Clever Email Validation Function - E-Mail validation function with an eregi expression and socket connection.
Categories : Email, PHP, Regexps
Ping a Server and run a command to fix it if it is down
Categories : PHP, Errors and Logging, Regexps
 Jasp Jones wrote : 908
not sure of the php internals but would it be better to get the count before the for loop to save re-evaluating the count on each loop? also a nice way to get around chopping the first `AND` off is to put `WHERE 1 = 1` || $ANDS

cheers

Jasp
 
 Roberto Schiabel wrote :917
what if your search must apply more than one field.
do you use a nested-cicle to build a sql statement to mach all the words in every field ?

thanks
(sorry for my english)