|
|
|
| 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 : |
26040 |
| 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);
?>
*/
?>
|
|
| 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 | | | 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 | | | How to thread a list of messages in database
and show it in a treelike structure Categories : PHP, MySQL, Databases | | | AJAX Application Categories : PHP, AJAX, Databases, MySQL | | | Checks Date-Input from HTML-Forms and converts to YYYY-MM-DD Format for MySQL Date-Fields Categories : MySQL, Date Time, PHP, Databases | | | Report Generation in Microsoft Access from a MYSQL database Categories : PHP, MySQL, Databases, MS Access | | | Inserting data to a MySQL Database using AJAX Categories : AJAX, HTTP, PHP, Databases, MySQL | | | The first step Guest Book ... ^^ Categories : MySQL, PHP, Apache, HTML, HTTP | | | A complete Webmaster Content Managment Tool, with differing levels of
access. Allowing the dynamic additon and showing of: Links, Multiple
Articles and News.
Categories : PHP, MySQL, Content Management | | | google like search function with bolded search terms Categories : PHP, Search, Databases, General SQL | | | Simple pipe delimited file export program that downloads to a local machine Categories : PHP, Filesystem, Databases, MySQL, HTTP | | | A script to generate a report from a valid mysql connection. The user has to supply which fields he wants to display in table. All properties are changable.
Categories : PHP, PHP Classes, Databases, MySQL, HTML and PHP | | | Simple database class Categories : PHP, PHP Classes, MySQL, Databases | | | A very simple way to build and do a hierarchical html categories browser without javascript , just using html php and mySql
Categories : HTML and PHP, Databases, Algorithms, PHP, MySQL | |
| |
| | | | | 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
| |
|
|