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 : 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 Update Picture
Ed Williams
Date : Apr 30th 1999
Grade : 2 of 5 (graded 7 times)
Viewed : 20341
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Ed Williams
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

<?php
/******************************************************************************
Class Query
*******************************************************************************

Purpose:

                To add a logical query to the SearchEngine class. The logic can
be
                one of the following : AND , OR, NOT.

Usage:                
         aquery = new Query("NOT", array("apple", "pear", "bannana"));
                
*****************************************************************************/

class Query {

//PRIVATE:

        var $m_words;
        var $m_logic;

//PUBLIC

        //
        // Constructor        
        
        // logic: <AND | OR | NOT>
        // words: words to be searched for given logic
        
        function Query($logic, $words) {
        
                $this->m_words = $words;
                $this->m_logic = $logic;
        }        
        
        // print attributes of query
        
        function p() {
        
                echo "$this->m_logic";
                reset($this->m_words);
                while(list($i, $word) = each($this->m_words))
                        echo ":$word";
        }
};

/******************************************************************************
Class QueryGenerator
*******************************************************************************

purpose:
                
                Generates selection query strings
usage:                
                query1 = new Query("AND", array("rabbit", "horse", "cow"));
         query2 = new Query("OR", array("dogs", "cats", "kangaroos"));
                query3 = new Query("NOT", array("sheep"));

        querygen = new QueryGenerator(array(query1, query2, query3));
                query = querygen->GetQueryString
("animal_table", "mamals_field", "ID_field");
                
                resultid = mysql_query(query);                
                while(row = mysql_fetch_row(resultid))
                        echo "got row id ",row["ID_field"],"<br>";
                
*******************************************************************************/

class QueryGenerator {

        var $m_Querys;
        var $m_start;
        var $m_offset;        
        
        var $m_wordmatch;
        var $m_casematch;

//PUBLIC:

        // querys : array of Query objects
        // wordmatch: only match whole words

        function QueryGenerator($Querys, $wordmatch=false) {

                $this->m_Querys = $Querys;        
                $this->m_wordmatch = $wordmatch;
                $this->m_casematch = $casematch;
                
                $this->Restrict(0,0);
        }
        
        // start : start searching data at this pos
        // offset : number of records to search after pos
        
        function Restrict($start, $offset) {
        
                $this->m_start = $start;
                $this->m_offset = $offset;        
        }
        
        // table : database table name
        // searchfield: field(s) to search in for resultfield(s)
        // resultfield: field(s) retrieved from search match
        // NOTE: field must be separated by "," eg, "field1,field2"

        // RETURN: function returns resultfield
        
        function GetQueryString($table, $searchfield, $resultfield="ID") {
        
                // get the query string         
                $query_string = $this->BuildQueryString($table, $searchfield,
$resultfield);
        
                // append limit                
                if ($this->m_start || $this->m_offset) {
                        $query_string .= "LIMIT $this->m_start";        
                        if($this->m_offset)                        
                                $query_string .= ",$this->m_offset";
                }
                                        
                // exec query                
                return $query_string;
        }        

//PRIVATE:

        // build where clause
        function BuildWhereClause($searchfield, $query, $wordmatch) {
                $query_string.="(";                                                
                                
                while($word = current($query->m_words)) {                        
        
                
                        if ($wordmatch)
                                $search_type = "REGEXP '(^|[^a-zA-Z])$word($|
[^a-zA-Z])'";
                        else
                                $search_type = "LIKE '%$word%'";
                                                                                
                                
                        if ($query->m_logic == "NOT")
                                $query_string.="$searchfield NOT ";
                        else
                                $query_string.="$searchfield ";
                                
                        $query_string.= $search_type;                                
        
                        if (next($query->m_words))                                 
                                $query_string.=" OR ";
                }                                                                
                                                                
                $query_string.=")";                                                
                                                                
                return $query_string;
        }
        
        // main function that generates the entire query
        function BuildQueryString($table, $searchfield, $resultfield) {

                // begin query string
                $query_string = "SELECT $resultfield FROM $table WHERE ";
                
                // parse searchfields
                $searchfields = explode(",", $searchfield);                
                if(!is_array($searchfields))
                        $searchfields=array($searchfield);
                                        
                // build query for each searchfield
                while ($searchfield = current($searchfields)) {

                        $firstquery = true;
                        $query_string .= "(";
                        
                        // build current query
                        reset($this->m_Querys);                                        
                
                        while($query = current($this->m_Querys)) {                
                                        
                                
                                // append logic                        
                                if ($firstquery)
                                        $firstquery=false;
                                else if ($query->m_logic == "NOT")
                                        $query_string.="AND";        
                                else
                                        $query_string.=$query->m_logic;                 
                                                        
                                                
                                // append where clause
                                $query_string .=
                                $this->BuildWhereClause($searchfield, $query,
$this->m_wordmatch);                                                                
                                
                                next($this->m_Querys);
                        }
                        $query_string .=")";                                        
                        
                        if (next($searchfields))
                                $query_string .= "OR";        
                }                                                                
        
                return $query_string;
        }
};        

?>



SearchEngine class that extends the QueryGenerator class provides an easy interface for a MySQL search engine.Have a look at the QueryGenerator class as well.
Categories : PHP Classes, MySQL, Search
YellowPages Content Grabber (PHP5 +)
Categories : PHP, PHP Classes, Regexps, Databases, MySQL
Search and Replace Text : Searches Files for Specified Text and Replaces It by a Given Text
Categories : PHP, PHP Classes, Search, Filesystem
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
Writing Portable MySQL Code in PHP: Porting to Oracle, Microsoft SQL Server, Sybase, Interbase, PostgreSQL and other databases using ADODB class library.
Categories : MySQL, PHP, PHP Classes, ODBC, General SQL
PHP Transfer data from text file to Mysql Table
Categories : PHP, PHP Classes, Filesystem, Databases, MySQL
MySQL Handler
Categories : PHP, Databases, MySQL, Classes and Objects, PHP Classes
Simple Mini Poll class library (SimPoll)
Categories : PHP, PHP Classes, Databases, MySQL, Complete Programs
Sort the results from a SELECT query (any number of columns) into an array automatically.
Categories : PHP, PHP Classes, Arrays, Databases, MySQL
usercounter class
Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables
Parsing Simple Template Files and Data
Categories : PHP, PHP Classes, Templates, Regexps
Simple usersOnline class - keep track of how many users are online on your site
Categories : PHP, PHP Classes, Databases, MySQL
Banknote Validation - A PHP class that provides several methods to quickly validate banknote serial numbers of the following currencies: AUD, CAD, CHF, CNY, EUR, GBP, JPY, USD.
Categories : PHP, PHP Classes, Data Validation, Regexps
PostGreSQL and MySQL 2 in 1 db Manager
Categories : PHP, PHP Classes, Databases, PostgreSQL, MySQL
PHP classes used to connect to MySQL like is done with ADO in VB.
Categories : Databases, MySQL, PHP Classes
 Krzysztof Wysocki wrote :224
Very nice example of using objects. I wish more stuff were
written like that.
Congratulations