Page 1 of 1

key word search using php like google result

Posted: Thu Jan 14, 2010 4:36 am
by tahir_mcs
i have following code and want to show the searching result like google as close the keywords the result show at the top and up to so on..........im little close to it but have some problm is any body help me?
--------------HTML-------------------
<html>
<head>
<title>search script</title>
<meta name="author" content="Steve R, http://www.designplace.org/">
</head>
<body>
<form name="form" action="search.php" method="get">
<input type="text" name="q" />
<input type="submit" name="Submit" value="Search" />
</form>
</body>
</html>
--------------PHP-------------------------------------
<html>
<head>
<title>search script</title>

</head>

<body>


<?php

// Get the search variable from URL

$var = $_GET['q'] ;
$trimmed = trim($var); //trim whitespace from the stored variable
$arrar_string = explode(" ",$trimmed);
//print_r($arrar_string);
$dynamic_query = "like ";
//if(count($arrar_string)>1)
//{
for($q=0;$q<count($arrar_string);$q++)
{
$dynamic_query .= " '%".$arrar_string[$q]."%' or text like ";
}
$dynamic_query = substr($dynamic_query, 0, -13);
$dynamic_query .= ")";
//echo $dynamic_query;
//}
// rows to return
$limit=10;

// check for an empty string and display a message.
if ($trimmed == "")
{
echo "<p>Please enter a search...</p>";
exit;
}

// check for a search parameter
if (!isset($var))
{
echo "<p>We dont seem to have a search parameter!</p>";
exit;
}

//connect to your database ** EDIT REQUIRED HERE **
$con = mysql_connect("hostname","username","password"); //(host, username, password)

//specify database ** EDIT REQUIRED HERE **
mysql_select_db("dbName",$con) or die("Unable to select database"); //select which database we're using

// Build SQL Query
echo $query = "select * from tblvalue where (text ".$dynamic_query; // EDIT HERE and specify your table and field names for the SQL query

$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);

// If we have no results, offer a google search as an alternative

if ($numrows == 0)
{
echo "<h4>Results</h4>";
echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>";

// google
/*echo "<p><a href=\"http://www.google.com/search?q="
. $trimmed . "\" target=\"_blank\" title=\"Look up
" . $trimmed . " on Google\">Click here</a> to try the
search on google</p>";*/
}

// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}

// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");

// display what the person searched for
echo "<p>You searched for: "" . $var . ""</p>";

// begin to show results set
echo "";
$count = 1 + $s ;

// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$title = $row["text"];
$arr_pos=strallpos($title,$var);

$test = keyMatch($var,$title);
$arry_pos[] = $test;

// $two_d[][$row["ID"]] = $test;
//print_r($arr_pos);
$myarray[]= $test."-".$row["ID"];
$array_pos[]=$test;
$array_ids[]=$row["ID"];
// $myarray[]= $arr_pos;
echo "$count.)&nbsp;$title<br>" ;
$count++ ;
}
$sorted_array = bubbleSort($array_pos);
print_r($sorted_array);
echo "<br>";
//print_r($myarray);
for($a=0;$a<count($myarray);$a++)
{
for($c=$a;$c<count($myarray);$c++)
{
$arr_exp =explode("-",$myarray[$c]);
//print_r($arr_exp);
//$array_pos[]=$arr_exp[0];
//$array_ids[]=$arr_exp[1];
//echo $sorted_array[$a];
echo $sorted_array[$a]."==".$arr_exp[0]."<br>";
if($sorted_array[$a]==$arr_exp[0])
{
$arr_idd[]=$arr_exp[1];
}
}
}

echo "<br>";
print_r($arr_idd);
echo "-------------------------Search Result------------------------------<br>";
for($id=0;$id<count($arr_idd);$id++)
{
$sql = "select * from tblvalue where ID='".$arr_idd[$id]."'";
$rs = mysql_query($sql);
$row = mysql_fetch_array($rs);

echo "<li>".$row['text']."</li>";
}
//-------------buble sort-----------------------------
function bubbleSort($array_pos)
{
for($i=0;$i<count($array_pos);$i++)
{
for($j=0;$j<$i;$j++)
{
if($array_pos[$i]>$array_pos[$j])
{
$temp=$array_pos[$i]; //swap
$array_pos[$i]=$array_pos[$j];
$array_pos[$j]=$temp;
}

}

}
return $array_pos;

}
/* print_r($array_pos);
print_r($array_ids);
$max = $array_pos[0];
$min = $array_pos[0];
*/
/*for($a=0;$a<count($array_ids);$a++)
{
if($array_pos[$a]>$max)
{
$max = $array_pos[$a];
}
}
echo $max;*/
/*print_r($two_d);

for($j=0;$j<count($two_d);$j++)
{
for($k=0;$k<count($two_d);$k++)
{
echo $j.$k."=".$two_d[$j][$k]."<br>";
}
}*/
//print_r($arry_pos);
//print_r($myarray);
//for($a=0;$a<count($myarray);$a++)
//{
// echo $myarray[$a]."<br/>";
// /*$arr_exp =explode("-",$myarray[$a]);
// //print_r($arr_exp);
// $array_pos[]=$arr_exp[0];
// $array_ids[]=$arr_exp[1];
//*/
//}

$currPage = (($s/$limit) + 1);

//break before paging
echo "<br />";

// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\"><<
Prev 10</a>&nbsp&nbsp;";
}

// calculate number of pages needing links
$pages=intval($numrows/$limit);

// $pages now contains int of pages needed unless there is a remainder from division

if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}

// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

// not last page so give NEXT link
$news=$s+$limit;

echo "&nbsp;<a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";
}

$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
echo "<p>Showing results $b to $a of $numrows</p>";


//----------------------------------------------------------------
function strallpos($haystack,$needle,$offset = 0)
{

$result = array();
// $result = "";
for($i = $offset; $i<strlen($haystack); $i++){
$pos = strpos($haystack,$needle,$i);
if($pos !== FALSE){
$offset = $pos;
if($offset >= $i){
$i = $offset;
//$result[] = $offset;
$result = $offset;
}
}
}

return $result;
}
//---------------------------------------------------------------------
function keyMatch($needles,$haystack) {
$nArray=split(" ",$needles);
//print_r($nArray);
$found=0;
$best = count($nArray);
$mtch = "";
for($i=0;$i<count($nArray);$i++)
{
// $pzn=strpos(strtoupper($haystack),strtoupper($nArray[$i]));
//$nArray[$i];
$pzn=strpos($haystack,$nArray[$i]);
if ($pzn>-1)
{

$found++;
}

//echo $found;
}

//echo $foundl;
$pct=($found*100)/$best;
return round($pct);
//return $pct;
}
$test = keyMatch("the quick fo","the quick brown fox jumps over the lazy dog");
//echo($test);

?>
</body>
</html>