<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{
        
// Connect to the database
        
$dServer = "localhost";
        
$dDb = "test";
        
$dUser = "admin";
        
$dPass = "";

        
$s = @mysql_connect($dServer, $dUser, $dPass)
            or die(
"Couldn't connect to database server");

        @
mysql_select_db($dDb, $s)
            or die(
"Couldn't connect to database");

        
$CommonWords=array("a"=>1,"as"=>1,"any"=>1,"all"=>1,"am"=>1,"an"=>1,"and"=>1,"are"=>1,"at"=>1,
                
"b"=>1,"be"=>1,"but"=>1,"by"=>1,
                
"c"=>1,"can"=>1,
                
"d"=>1,"did"=>1,"does"=>1,"do"=>1,
                
"e"=>1,"each"=>1,"else"=>1,"even"=>1,"ever"=>1,
                
"f"=>1,"for"=>1,"from"=>1,
                
"g"=>1,"go"=>1,"get"=>1,
                
"h"=>1,"hi"=>1,"he"=>1,"his"=>1,"him"=>1,"her"=>1,"has"=>1,"how"=>1,"had"=>1,"here"=>1,"have"=>1,
                
"i"=>1,"in"=>1,"is"=>1,"if"=>1,"its"=>1,
                
"j"=>1,"just"=>1,"k"=>1,
                
"l"=>1,"like"=>1,"led"=>1,"lap"=>1,"let"=>1,
                
"m"=>1,"my"=>1,"me"=>1,"many"=>1,"must"=>1,"more"=>1,
                
"n"=>1,"no"=>1,"not"=>1,"new"=>1,"now"=>1,
                
"o"=>1,"of"=>1,"on"=>1,"or"=>1,"once"=>1,
                
"p"=>1,"q"=>1,"r"=>1,
                
"s"=>1,"so"=>1,"some"=>1,"say"=>1,"she"=>1,
                
"t"=>1,"to"=>1,"the"=>1,"then"=>1,"that"=>1,
                
"u"=>1,"use"=>1,"us"=>1,"up"=>1,"upon"=>1,
                
"v"=>1,"via"=>1,"vow"=>1,
                
"w"=>1,"was"=>1,"why"=>1,"who"=>1,"whose"=>1,"were"=>1,
                
"y"=>1,"yes"=>1,"ya"=>1,"you"=>1,"your"=>1,
                
"z"=>1,"zoo"=>1,);


        
//START TIMER
        
$start=getmicrotime();

        
$search_keywords=strtolower(trim($keywords));
        
$arrWords = explode(" ", $search_keywords);

        
//remove duplicates
        
$arrWords=array_unique($arrWords);

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

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


        
//explode 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);

        
//array to store the content id and occurances
        
$contArray=array();
        
$rescount=0;
        
//search for the link table only if all the given keywords present in the keytable

        
if(mysql_num_rows($kResult) == $noofSearchWords){
            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

            
if(isset($contArray)){
                
//declare an array to store the results
                
$FoundRef=array();

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

               
// while(list($contentId,$occurances)=each($contArray)){
                   
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
            
}


                
//end TIMER
                
$end=getmicrotime();

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

            }
//end of if countwords == mysql_number_of _ records



            //end TIMER
            
$end=getmicrotime();

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

            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
"&nbsp"."'".$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


        
}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);
}
?>