|
|
|
| Title : |
Add Boolean Logic Functions to Database Queries Easily. A
function to write a WHERE clause dynamically to search a
database from a search form.
|
| Categories : |
MySQL, PHP, Search |
 Michael Patrick |
| Date : |
Jul 29th 1999 |
| Grade : |
2 of 5 (graded 9 times) |
| Viewed : |
17622 |
| File : |
No file for this code example. |
| Images : |
No Images for this code example. |
|
| Search : |
More code by Michael Patrick |
|
| Action : |
Grade This Code Example
|
|
| Tools : |
My Examples List |
|
|
|
|
|
|
<?php
/*
the function below was written by Michael Patrick. This code is available for free use. Go
ahead and strip it and include it in your own programs, I know you would anyway. So, why tell
you not to do it without my express written permission?
If you have any questions, please contact me at mpatrick@xtn.net. I am afraid, however, that
I can only give limited support for this software. There are probably several ways to simplify
this code, but it works... ;-)
The function creates only the WHERE clause of the SQL statement dynamically. An example is
shown at the end of the code.
An example can be found at: http://commerce.xtn.net/automaxx2/index.php?page=products
Or, if it has moved, try: http://automaxx.com/index.php?page=products
*/
function makeboolean($sqlfieldname, $keywordstr) {
// pass into this function the sqlfieldname that you want to search by and the original string
// that the user entered into the form field on the search engine.
// After you get back the result from this function, include it into your complete search string.
// Note, you may want to call this function repeatedly to search through various fields in your
// tables.
$keyword = $keywordstr;
// Convert String To Lower Case
$keyword = strtolower($keyword);
// Replace Word Operators With Single Character Operators
$keyword = ereg_replace(" and ","+",$keyword);
$keyword = ereg_replace(" or ","|",$keyword);
$keyword = ereg_replace(" not ","-",$keyword);
// Build The Keywords String Based On Operators Assigned Above
$operatorcount = 0;
$len = strlen($keyword);
for ($z = 0; $z < $len; $z++) {
if(($keyword[$z] == "+") || ($keyword[$z] == "|") || ($keyword[$z] == "-")) {
$operatorpos[$operatorcount] = $z;
$operatorcount++;
}
}
if ($operatorcount != 0) {
for ($z = 0; $z < $operatorcount; $z++) {
if($z == 0) {
$startpos = 0;
$endpos = $operatorpos[$z];
} else {
$startpos = $operatorpos[$z - 1] + 1;
$endpos = $operatorpos[$z];
}
$word = $endpos - $startpos;
$keystring = substr($keyword,$startpos,$word);
$keystring = ereg_replace("\(","",$keystring);
$keystring = ereg_replace("\)","",$keystring);
$keywords[$z] = $keystring;
$operator_pos = $operatorpos[$z];
$operators[$z] = $keyword[$operator_pos];
} // end the for loop
$wordcount = $operatorcount + 1;
$startpos = $operatorpos[$z - 1] + 1;
$len2 = strlen($keyword) - $startpos;
$linestr = substr($keyword,$startpos,$len2);
//store the line into the keywords array
$keywords[$wordcount - 1] = $linestr;
//loop through all of the words in the words array replacing them in the original string with a
LIKE clause
for ($z=0; $z < $wordcount; $z++) {
$replacekeyword = $keywords[$z];
$y = $z -1;
if ($operators[$y] != "-") //odd case is in a NOT...must do something different!
$keyword = ereg_replace($replacekeyword,"$sqlfieldname LIKE '%
$replacekeyword%'",$keyword);
else
$keyword = ereg_replace($replacekeyword,"$sqlfieldname NOT LIKE '%
$replacekeyword%'",$keyword);
}
// Replace Our Operators With The Correct SQL Operators
$keyword = ereg_replace("\+"," AND ", $keyword);
$keyword = ereg_replace("\|"," OR ", $keyword);
$keyword = ereg_replace("\-"," AND ", $keyword); //I fudged in the above statement so this
possible :-)
} // end if operatorcount != 0
else { //there were no operators in the string
$replacekeyword = $keyword;
if ($keyword != "") {
$keyword = ereg_replace($replacekeyword,"$sqlfieldname LIKE '%
$replacekeyword%'",$keyword);
}
}
return($keyword);
} //end the makebooleanstatement function
/*
NOTE: Here is a sample call to the function assuming keyword is the form field where the
user entered their search criteria into their search engine form. Let's assume that
you have a form with a keyword input field and a submit button.
Sample form:
<form name=keywordsearch>
Keyword: <input type=text name=keyword>
<input type=submit name="Submit Query" value="Submit Query">
</form>
Sample PHP Code:
<?
$keyword = makeboolean("keyword",$keyword);
$statement = "SELECT * FROM sometablename WHERE $keyword";
$result = mysql("somedatabase",$statement);
?>
*/
?>
|
|
| free, search engine, indexing, system, information, web,
ftp, http, free, software, cgi, php, MySQL, database, php3,
FreeBSD, Linux, Unix, UdmSearch Categories : MySQL, Complete Programs, PHP, Databases, Search | | | bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager Categories : MySQL, PHP, MySQL, Complete Programs, Databases | | | 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 | | | Retrieve text from table and email to your e-
address in pipe delimited format. Categories : PHP, MySQL | | | Accepts a database & hostname from a user and then HTTP username and password. Uses this to connect to a MySQL database. Produces a form based on the tables it finds there to allow the user to do SELECTs, INSERTs, and DELETEs. Categories : Databases, PHP, MySQL, Complete Programs | | | Cut your MySQL Connections to 1 line of code Categories : PHP, Beginner Guides, Databases, MySQL | | | phpAds, a complete banner and ad management system with detailled tracking and stats. Categories : MySQL, Complete Programs, Ecommerce, PHP, Databases | | | Point and Click Interface ala MS Access for creating SQL statements. Categories : MySQL, Complete Programs, General SQL, PHP, Databases | | | Displaying records of database in more than one page (paging) Categories : Databases, MySQL, PHP | | | PCAC (pretty cool auction client), auction client server applications Categories : PHP, MySQL, Ecommerce | | | Searches through a local INN server's discussions Categories : Search, Complete Programs, PHP | | | phpCards - PHP/mySQL postcard script with web based admin to add, edit, and delete cards and categories. Very easy to install. Categories : PHP, Complete Programs, HTML and PHP, MySQL | | | html split bar used to split in multiple pages a database result Categories : HTML and PHP, Databases, MySQL, PHP | | | Tropicalm Genetree Family (MySQL based family tree) Categories : PHP, Interfaces, Databases, MySQL, Complete Programs | | | Report Generation in Microsoft Access from a MYSQL database Categories : PHP, MySQL, Databases, MS Access | |
| | | | Hans Rakers wrote : 79
The code sure as hell screws up when users enter multiple, allmost identical strings.
Example: if user enters `abc+abcdef` the code returns a where clause like :
`fieldName LIKE `%abc%` AND fieldName LIKE `%abc%`def
or even worse, when a user enters `bla+blabla` it returns :
fieldName LIKE `%bla%` AND fieldName LIKE `%bla%`fieldName LIKE `%bla%`
I`m looking for a way to fix this.
| | | | ret ret wrote : 250
ewrewr
| | | | Martijn Naber wrote : 728
A quick fix, it`s not perfect because I was to lazy to read the complete code:
//loop through all of the words in the words array replacing them in the original string with a LIKE clause
$temp= "";
for ($z=0; $z < $wordcount; $z++) {
$replacekeyword = $keywords[$z];
$y = $z -1;
if ($operators[$y] != "-") { //odd case is in a NOT...must do something different!
if($y>=0) { $extra = $operators[$y]; }
$temp .= $extra."$sqlfieldname LIKE `%$replacekeyword%`";
} else {
$temp .= "$sqlfieldname NOT LIKE `%$replacekeyword%`";
}
}
$keyword = $temp;
| | | | z00rg z00rg wrote :850
very bad piece of code , try to enter a single qoute in the search and it will return nothing, again try to add a larger piece of search string thi function will take white spaces as "and" keyword ......please some body find a solution to this
| |
|
|
|