hi
i'm doing a searching engine for my website
i got stuck with my coding, where i have to search the keyword from database and directory. i have no idea how can i merge these results and randomly sort by their relevency. at the moment, i could only display the results order by the keyword it found in the dbse and follow by directory it found from the dbse. as i mention before, i wanted randomly display the result by their relevency...
here is my code.
<?php
//$d = array('search/', './');
$dir = array('search/', './','search-try/', './');
$keyword = $_POST['keyword'];
$n = 10;
$domain = "http://localhost/";
$keyword = htmlentities($keyword);
$count=0;
if($keyword==""){
echo"Please enter your keyword";
exit;
}else{
$q_array=explode(" ",$keyword);
$q_num=(integer)count($q_array);
echo "<p>Search by <strong>\"$keyword\"</strong></p>";
echo "<ol>";
}
/* Search Files */
for($i=0;$i<3;$i++)
{
if (is_dir($dir[$i])) {
if ($dh = opendir($dir[$i])) {
$findstrcontent=array();
$file=array();
$fileContent=array();
$cnt=0;
while (($file[$cnt]= readdir($dh)) !== false) {
$htm = substr($file[$cnt], -4, 4);
$html = substr($file[$cnt], -5, 5);
$php = substr($file[$cnt], -4, 4);
if (($htm == ".htm" ) || ($html == ".html" )|| ($php == ".php" )) {
$fileContent[$cnt]=file_get_contents($dir[$i].$file[$cnt]);
$strContent = strip_tags($fileContent[$cnt]);
if($dom=@stristr($strContent,$keyword)){
$findstrcontent[$cnt] = implode(" ", array_slice(explode(" ", $dom), 0, $n));
$link=$dir[$i].$file[$cnt];
$findstrcontent[$cnt] = str_replace($keyword,"<strong>".$keyword."</strong>", $findstrcontent[$cnt]);
/* Search Database */
$query = "SELECT COUNT(*) FROM search WHERE keywords LIKE '%$keyword%'";
$res = mysql_query($query);
$num = mysql_fetch_row($res);
$stmt = mysql_query("SELECT title,url, description FROM search WHERE keywords LIKE '%".$keyword."%' ")
or die(mysql_error());
/* print out result */
// Result From Files //
if($findstrcontent[$cnt]!=""){
$count++;
echo "<li>";
$path = $dir[$i].$file[$cnt];
$path = str_replace ("./","", $path);
?>
<a href=<?=$link?>><? echo "<em> ".$domain.$path."</em>"; ?></a><br />
<? echo $findstrcontent[$cnt]; ?>...
<br />
<?
}
}
}
$cnt=$cnt+1;
}
closedir($dh);
}
}
}
// Result From Database //
$stmt = mysql_query("SELECT title,url, description FROM search WHERE keywords LIKE '%".$keyword."%' ")
or die(mysql_error());
if(mysql_num_rows($stmt)> 0){
while($row = mysql_fetch_array($stmt)){
echo "<li>";
?>
<a href=<?=$row['url']?>><? echo "<em> " .$row['url']."</em>"; ?></a><br />
<? echo $description=$row["description"]; ?>...
<br />
<?
}
}
echo "</li>";
/* Print Total Result */
echo "</ol>";
if ($count > 0) {
$totalResult = ($count)+($num[0]);
echo " <strong> $totalResult</strong> results were found searching for <strong>$keyword</strong><br />";
} else {
echo "Sorry, no results.<br />";
}
?>