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.
Next: Conclusion >>
More MySQL Articles
More By Murali Dharan