MySQL
  Home arrow MySQL arrow Page 3 - Developing a Dynamic Document Search in PH...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MYSQL

Developing a Dynamic Document Search in PHP Part 1/2
By: Murali Dharan
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 16
    2003-06-26

    Table of Contents:
  • Developing a Dynamic Document Search in PHP Part 1/2
  • The Article
  • The Article (contd.)
  • The Article (contd.)
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Developing a Dynamic Document Search in PHP Part 1/2 - The Article (contd.)


    (Page 3 of 5 )

    The following code snippet is the starting place of execution, which calls all of the above functions. Here it connects to database server and database.  Initially, the form() function is called that allows you to enter the title and abstract of the document.

    if($submit){

        global $allWords;

        mysql_connect( "localhost", "root", "" ) or die( "Unable to connect to database" );
        mysql_select_db( "kpp" ) or die( "Unable to select database" );

        LoadCurrentWords();

        if ( $title and $body){
                ProcessForm($title ,$body);
            }

    }else{ //end of main
           $err="Please fill in the fields to upload\n";
           form($err);
    }

    function form($errmsg)
    {  ?>
       <h4 align="center">File Parser & Uploader</h4>
       <b><? echo $errmsg; ?></b>
       <center>
       <form method="POST" action=<? echo $PHP_SELF ?>>
       Title:   <input type="text" name="title" ><p>
       Abstract:   <input type="text" name="body" ><p>
         <input type="submit" name="submit" value="Start Parsing and Upload Content">
       </table>
       </form>

       </center>
    <?
    }

    ?>

    Search Engine
    As you know, you can use PHP to a database through a HTML form. This will work as any other search engine: the user enters a word in a textbox, hits enter, and the interface presents a result page with links to the pages which contain the word that was searched for.

    In this example, the results are displayed in the order in which the pages are presented. Next, we declare an associative array called $CommonWords that contains common words like ‘is’, ‘in’, ‘was’ etc.
     
    First convert all the search words in to lower case:

    $search_keywords=strtolower(trim($keywords));

    Next, we have to perform an explode operation on search words that will store each search word in an array.  The code is shown here:

    $arrWords = explode(" ", $search_keywords);

    Next, remove duplicate words in $arrWords:

    $arrWords = array_unique($arrWords);

    In a search operation, first we have to remove the common words like ‘is’, ‘in’, ‘was’ … This refines our search criteria. To implement this, we store common words in the associative array, $CommonWords.

    Next, remove common words in the search words. Search words are stored in $searchWords and common words are stored in $junkWords. Here is the code:

            $searchWords=array();
            $junkWords=array();
            foreach($arrWords as $word)
                //remove common words
                if(!$CommonWords[$word]){
                    $searchWords[]=$word;
                }else{
                    $junkWords[]=$word;
                }

    We can display results in two ways.
    Type 1: Display the document if all the search words are present in the document
    Type 2: Display the document if any one of the search words is present.

    If you want to perform the Type 1 operation, include the following code snippet in to your program:

    //count no of words in the search words and store in a variable
            $noofSearchWords=count($searchWords);

    $noofSearchWords stores the number of search words. Later after searching the search words in the keyword table, we get the results.  Then we can perform a logical AND operation that will display our desired results. If $noofSearchWords is equal to the number of records, the next part of the program gets executed. If not, “NO SEARCH RESULT FOUND” is displayed.

    In the next step, we have to search for words in the $searchWords array in the keyword table. The following code snippet will return you a list of keyids that match the query:

            //implode to an array
            $arrWords = implode("'  OR keyword='", $searchWords);

            //get the key ids from the key table
            $query = "select * from keytable where keyword='$arrWords'";

            $kResult = mysql_query($query);

    As discussed earlier, if you need to perform the type 1 operation, you have to check whether the number of search words equal the number of records in the query. If they are equal, you can proceed to the next step. If not, display the search result as not found. Here is the code:

         if(mysql_num_rows($kResult) == $noofSearchWords){

     //search for the keyids in the link table and get the content id
     //Fetch title, first 200 words of the abstract in to an array
     //Display the result
        }else{
     echo “NO SEARCH RESULT FOUND”;
       }

    The following code searches the link table for occurrences of the key ID’s. This will return an array that contains the content ID’s:

    while($kRow=mysql_fetch_array($kResult))
            {
                 //get the link ids for each key id
                 $kid= $kRow['keyid'];
                 $query = "SELECT * FROM link WHERE keyid=$kid";
                 $lResult = mysql_query($query);
                  //echo mysql_num_rows($lResult);
                  while($lRow=mysql_fetch_array($lResult))
                  {
                      $thisContentId=$lRow["contid"];
                      if(!$contArray[$thisContentId]){
                          $contArray[$thisContentId]=1;
                      }else{
                          $contArray[$thisContentId]++;
                      }
                  }
             }//end of while

    Sort the array in descending order based on the key value. This will order the results from highest occurrences to the lowest. For example, if the number of search words is four, the order is displayed 4 then 3 then 2 and last 1.

    More MySQL Articles
    More By Murali Dharan


       · hi,i am umesh yadav, i am working on php last 6 months but i was developing a...
     

    MYSQL ARTICLES

    - MySQL and BLOBs
    - Two Lessons in ASP and MySQL
    - Lord Of The Strings Part 2
    - Lord Of The Strings Part 1
    - Importing Data into MySQL with Navicat
    - Building a Sustainable Web Site
    - Creating An Online Photo Album with PHP and ...
    - Creating An Online Photo Album with PHP and ...
    - PhpED 3.2 – More Features Than You Can Poke ...
    - Creating An Online Photo Album with PHP and ...
    - Creating An Online Photo Album with PHP and ...
    - Security and Sessions in PHP
    - Setup Your Personal Reminder System Using PHP
    - Create a IP-Country Database Using PERL and ...
    - Developing a Dynamic Document Search in PHP ...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway