Database Articles

  Home arrow Database Articles arrow Page 27 - Creating a Search Application
DATABASE ARTICLES

Creating a Search Application
By: Matt Wade
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 16
    2003-07-15

    Table of Contents:
  • Creating a Search Application
  • Database Usage
  • Creating a Search Application
  • Searching
  • Conclusion

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Creating a Search Application -


    (Page 27 of 29 )

    Now, we present the search class script as a whole.

    <?php
    require('dbclass.php');

    class 
    Search {

        var 
    $_db;
        var 
    $_searchterms;
        var 
    $_numterms;

        function 
    Search($keywords) {
            
    $this-&gt;_db = new DB_Class('test''username''password');
            if (!
    get_magic_quotes_gpc()) {
                
    $keywords addslashes ($keywords);
            }
            
    $this-&gt;_searchterms explode(' '$keywords);
            
    $this-&gt;_numterms count($this-&gt;_searchterms);
        }

        function 
    doSearch() {
            
    $match "keywords.keyword in ('"
                   
    $this-&gt;_searchterms[0] . "'";
            for (
    $i 1$i &lt$this-&gt;_numterms$i++) {
                
    $match .= ", '" $this-&gt;_searchterms[$i] . "'";
            }
            
    $match .= ")";
            
    $query "SELECT urls.url, count(*) as counter "
                   
    "FROM urls, keywords "
                   
    "WHERE $match "
                   
    "AND keywords.url_id = urls.id "
                   
    "GROUP BY keywords.url_id "
                   
    "ORDER BY counter DESC";
            
    $result $this-&gt;_db-&gt;fetch($query);
            if (
    count($result) &gt0)
                
    $return $result;
            else
                
    $return FALSE;

            return 
    $return;
        }

        function 
    _highpercent(&amp;$item$key$array) {
            if (
    $item &lt60)
                unset(
    $array["$key"]);
        }

        function 
    _slashit(&amp;$item$key) {
            
    $item addslashes($item);
        }

        function 
    doFuzzy() {
            
    $match "LEFT(keyword,1) in ('"
                   
    substr($this-&gt;_searchterms[0], 01) . "'";
            for (
    $i 1$i &lt$this-&gt;_numterms$i++) {
                
    $match .= ", '" substr($this-&gt;_searchterms[$i], 01)
                        . 
    "'";
            }
            
    $match .= ")";
            
    $query "SELECT DISTINCT(keyword) FROM keywords "
                   
    "WHERE $match";
            
    $keywords $this-&gt;_db-&gt;fetch($query);
            
    reset($this-&gt;_searchterms);
            foreach (
    $this-&gt;_searchterms as $term) {
                foreach (
    $keywords as $keyword) {
                    
    $word $keyword['keyword'];
                    
    similar_text($term$word,
                    
    $matches["$term"]["$word"]);
                }
                
    array_walk ($matches["$term"],
                            array(
    $this'_highpercent'),
                            &
    amp;$matches["$term"]);
            }
            if(
    $this-&gt;_numterms &gt1) {
                
    $merge '$merged = array_merge($matches["'
                       
    $this-&gt;_searchterms[0] . '"]';
                for (
    $i 1$i &lt$this-&gt;_numterms$i++) {
                    
    $merge .= ', $matches["'
                            
    $this-&gt;_searchterms[$i] . '"]';
                }
                
    $merge .= ');';
                eval (
    $merge);
            } else {
                
    $merged $matches[$this-&gt;_searchterms[0]];
            }
            
    arsort($merged);
            
    $search array_keys($merged);
            
    array_walk($search, array($this'_slashit'));
            
    $match "keywords.keyword in ('"
                   
    $search[0] . "'";
            
    $numwords count ($search);
            for (
    $i 1$i &lt$numwords$i++) {
                
    $match .= ", '" $search[$i] . "'";
            }
            
    $match .= ")";
            
    $query "SELECT urls.url, keywords.keyword, "
                   
    "count(*) as counter "
                   
    "FROM urls, keywords "
                   
    "WHERE $match "
                   
    "AND keywords.url_id = urls.id "
                   
    "GROUP BY keywords.url_id, keywords.keyword "
                   
    "ORDER BY counter DESC";
            
    $result $this-&gt;_db-&gt;fetch($query);
            if (
    count($result) &gt0)
                
    $return $result;
            else
                
    $return FALSE;

            return 
    $return;
        }
    }
    ?>

    More Database Articles Articles
    More By Matt Wade

    blog comments powered by Disqus

    DATABASE ARTICLES ARTICLES

    - Completing a Book Inventory Management System
    - Uploading Images for a Book Inventory Manage...
    - Finishing the Add Book Story for a Book Inve...
    - Integration Testing for a Book Inventory Man...
    - User Stories for a Book Inventory Management...
    - Unit Testing a Book Inventory Management Sys...
    - Testing a Book Inventory Management System
    - Implementing Models for a Book Inventory Man...
    - Book Inventory Application: Publishers and B...
    - Handling Publishers in a Book Inventory Mana...
    - Publisher Administration for Book Inventory ...
    - Book Inventory Management
    - Using the SQL Reference Manual
    - Using Oracle SQL Developer with SQL Statemen...
    - Fixing Errors with Oracle SQL Developer


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap