Home arrow MySQL arrow Page 4 - 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 4 of 5 )

//Sort array in descending order of the key value
                arsort($contArray,SORT_DESC);

In the next step we have to fetch the title and first 200 words from the content table into an array called $FoundRef:

//declare an array to store the results
                $FoundRef=array();

while(list($contentId,$occurances)=each($contArray)){

            $aQuery = "select contid,title,left(abstract,200) as summary from content where contid = " . $contentId;
            $aResult = mysql_query($aQuery);

            if(mysql_num_rows($aResult) > 0){
                $aRow = mysql_fetch_array($aResult);
                $FoundRef[] = array (
                                  "contid" => $aRow["contid"],
                                  "title" => $aRow["title"],
                                  "summary" => $aRow["summary"],
                                  "occurance"=>$occurances
                );
                }//end of  if
            }

Finally, we have to display the results in the browser. Here is the code:

if(isset($FoundRef))
            {
                echo "<table width=\"100%\"><tr><th class=\"title\">Search Result</td></tr></table>";
                echo "<a href=\"#\" onclick=\"history.back()\">Back</a>";
                echo "<br>";
                echo sizeof($FoundRef);
                echo (sizeof($FoundRef) == 1 ? " reference" : " references");
                echo " found";
                echo "<p>";
     if($junkWords){
                    echo "Common words like";
                    foreach($junkWords as $jWords){
                        echo " "."'".$jWords."'";
                    }
                    echo "are removed from the search string";
                }
                echo "</h5>";
                foreach($FoundRef as $a => $value)
                {
                    echo "<table>";
                    echo "<tr><td valign=\"top\">";
                   // echo $FoundRef[$a]["contid"];
                    ?>

                        <a href=showref.php?refid=<? echo $FoundRef[$a]["contid"]?>><emp><b><? echo $FoundRef[$a]["title"]?></b></emp></a><div align="right"> Occurance(s): <? echo $FoundRef[$a]["occurance"] ?></div>

                        <br><small><? echo $FoundRef[$a]["summary"] ?>...</small><br><br>
                        <? echo "</td></tr>";
                }?>


                <?
                    echo "</table>";
            }//end of isset FoundRef

The HTML page to get input from user is shown below:

<html>
<head>
<title>Search Engine</title>
<style type="text/css">

body{ font-size:20; font-weight:bold; font-stretch:semi-expand; font-family:MSserif; color:#0066CC; background-color:#EEEEE4;
align:center; background-color:white }
h4{   background-color:#0066CC;     color:#FFFFFF;   font-family:verdana;  }
h3{  color:#0066CC;   }
th{  background-color:#6996ED;  color:#FFFFFF;  font-family:Arial;   }
a{text-decoration:none;}
</style>
</head>
<body>
    <?php
if($submit)
{
    if(!$keywords){
        $errmsg="Sorry, Please fill in search field";
        form($errmsg);
    }else{
           //Start Timer
            $start = getmicrotime();

          //PERFORM SEARCH OPERATION AND DISPLAY RESULT
        }else {
            //end Timer
            $end = getmicrotime();

            //TOTAL TIME TAKEN TO DO SEARCH OPERATION
            $time_taken=(float)($end-$start);
            $time_taken=number_format($time_taken,2,'.','');

            echo "<p>Your Query Executed in $time_taken Seconds";

            $errmsg="<p>No Search result found for '$keywords'";
            echo $errmsg;
            echo "<br><a href=\"#\" onclick=\"history.back()\">Back</a>";
        }//endof isset ref
    }//end of if key word exists
} else{  //display the form
    form($keyword);
} //END OF FORM DISPLAY ?>
</body>
</html>
    <?
function form($errmsg)
{  ?>
    <h4 align="center">Search Engine</h4>
        <b><? echo $errmsg; ?></b>
        <center>
        <form method=POST action=<? echo $PHP_SELF ?>>
        </div>
        Enter keywords to search on:
        <input type="text" name="keywords" maxlength="100">
        <input type="submit" name="submit" value="Search">
        </form>
        </body>
        </html>
        <?
}


function getmicrotime()
{
    list($usec,$sec)=explode(" ",microtime());
    return ((float)$usec+(float)$sec);
}
?>

The function getmicrotime() returns the time in microseconds. This function is called during the start and end of the search process.


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