Database Articles

  Home arrow Database Articles arrow Page 18 - 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 18 of 29 )

    Now, we will glue all the bits together for you so that you may see the class file in its entirety. At the beginning of the class file, we have used to require statement to include the database class from the earlier section.

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

    class 
    Harvest_Keywords {

        var 
    $_db;
        var 
    $_urlarray;
        var 
    $_stopwords = array ('and''but''are''the');
        var 
    $_allowwords = array ('c++''ado''vb');

        function 
    Harvest_Keywords($urls) {
            
    $this-&gt;_db = new DB_Class('test''username''password');
        
    $this-&gt;_urlarray trim ($urls);
        
    $this-&gt;_urlarray explode ("\n"$this-&gt;_urlarray);
        }

        function 
    _prune (&amp;$item$key$array) {
            
    $item strtolower ($item);
            if (((
    preg_match ("/[^a-z0-9'\?!-]/"$item))
               || (
    strlen ($item) &lt3)
               || (
    in_array($item$this-&gt;_stopwords)))
               &
    amp;&amp; (!in_array($item$this-&gt;_allowwords))) {

                 unset(
    $array[$key]);
            } else {
                
    $item addslashes(preg_replace("/[^a-z0-9'-]/i",
                                                
    ''$item));
            }
        }

        function 
    _checkURL($url) {
            return 
    preg_match ("/http:\/\/(.*)\.(.*)/i"$url);
        }

        function 
    _getData($url) {
            
    $filehandle = @fopen($url'r');
            if(!
    $filehandle) {
                echo 
    "Could not open URL ($url).&lt;br /&gt;\n";
                
    $return FALSE;
            } else {
                
    $data fread($filehandle25000);
                
    fclose($filehandle);
                
    $data strip_tags ($data);
                
    $data str_replace('&amp;nbsp;'' '$data);
                
    $return $data;
            }
            return 
    $return;
        }

        function 
    _harvest($url) {
            if(!
    $this-&gt;_checkURL($url)) {
                echo 
    "URL is not valid ($url).&lt;br /&gt;\n";
            } elseif (
    $data $this-&gt;_getData($url)) {
                
    $words preg_split ("/[\s,.]+/"$data);
                
    array_walk ($words, array($this'_prune'), &amp;$words);
                
    sort ($words);
                
    $url_id $this-&gt;_db-&gt;getone("SELECT id FROM urls "
                                             
    "WHERE url='$url'");
                if(
    $url_id) {
                    
    $this-&gt;_db-&gt;query("DELETE FROM keywords "
                                      
    "WHERE url_id=$url_id");
                } else {
                    
    $this-&gt;_db-&gt;query("INSERT INTO urls SET url='$url'");
                    
    $url_id mysql_insert_id();
                }
                
    $values "($url_id, '$words[0]')";
                
    $numwords count ($words);
                for (
    $i 1$i &lt$numwords$i++) {
                    
    $values .= ", ($url_id, '$words[$i]')";
                }
                
    $this-&gt;_db-&gt;query("INSERT INTO keywords VALUES $values");
            }
        }

        function 
    process() {
            foreach(
    $this-&gt;_urlarray as $url) {
                
    $this-&gt;_harvest($url);
            }
        }
    }
    ?>

    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 9 - Follow our Sitemap