Home arrow MySQL arrow Page 3 - Developing a Dynamic Document Search in PHP Part 1/2
MYSQL

Developing a Dynamic Document Search in PHP Part 1/2


In today's article Murali discusses how to implement a complete document content search using only PHP and MySQL...

Author Info:
By: Murali Dharan
Rating: 5 stars5 stars5 stars5 stars5 stars / 26
June 26, 2003
TABLE OF CONTENTS:
  1. · Developing a Dynamic Document Search in PHP Part 1/2
  2. · The Article
  3. · The Article (contd.)
  4. · The Article (contd.)
  5. · Conclusion

print this article
SEARCH DEVARTICLES

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.


blog comments powered by Disqus
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 ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials