Hi all,
I want to integrate a search tool into my site.
It is based on one page using get to send variables to itself.
How do I change it so that it uses two pages. One to act as a form to take in the variables. The other to search.
Here is my search form:
<form action="http://mysite/search/search.php" method="post">
<label for="search" class="bold">Search:</label>
<input type="text" name="query" id="search" />
<br />
<input type="submit" name="submit" value="Submit" />
<br /><br />
</form>
Here is the code I need to integrate with the above form(works fine if used without the above form)...
<?
/*******************************************
* Sphider Version 1.2.x
* This program is licensed under the GNU GPL.
* By Ando Saabas ando(a t)cs.ioc.ee
********************************************/
$include_dir = "./include";
include "$include_dir/connect.php";
include 'searchfuncs.php';
extract(getHttpVars());
include "$include_dir/index_header.inc";
include "$include_dir/conf.php";
if ($results != "") {
$results_per_page = $results;
}
if (get_magic_quotes_gpc()==1) {
$query = stripslashes($query);
}
if ($type == "phrase") {
$check3 = "checked";
} else if ($type == "or") {
$check2 = "checked";
} else {
$check1 = "checked";
}
?>
<center><table cellpadding="5"
cellspacing="1" class="searchBox"><tr><td align="center">
<form action="<?print $_SERVER['PHP_SELF'];?>" method="get">
<input type="text" name="query" size="40" value="<? print quote_replace($query);?>">
<input type="submit" value="<?print $sph_messages['Search'];?>">
<?
if ($catid<>0) {
$query = "SELECT category FROM ".$mysql_table_prefix."categories WHERE category_id='$catid'";
$result = mysql_query($query);
echo mysql_error();
$row = mysql_fetch_array($result);
$category = $row['category'];
print "<center><b>".$sph_messages['Search']."</b>: <input type=\"radio\" name=\"category\" value=\"$catid\">".$sph_messages['Only in category']." \"$category\" <input type=\"radio\" name=\"category\" value=\"-1\" checked>".$sph_messages['All sites']."</center>";
}
?>
<input type="hidden" name="search" value="1">
</form>
<?
$get_cats = "SELECT * FROM ".$mysql_table_prefix."categories WHERE parent_num=0 ORDER BY category";
$result = mysql_query($get_cats);
echo mysql_error();
$rows = mysql_num_rows($result);
if ($rows >0 && $search==1)
print "<a href=\"".$_SERVER['PHP_SELF']."\">".$sph_messages['Categories']."</a>";
?>
</td></tr></table>
</center>
<?
function dosearch($query, $start, $category, $searchtype, $results) {
global $sph_messages, $results_per_page,
$links_to_next,
$show_query_scores,
$mysql_table_prefix,
$desc_length;
if ($results != "") {
$results_per_page = $results;
}
if ($searchtype == "phrase") {
$query=str_replace('"','',$query);
$query = "\"".$query."\"";
}
$starttime = getmicrotime();
// catch " if only one time entered
if (substr_count($query,'"')==1){
$query=str_replace('"','',$query);
}
$words = makeboollist($query);
$ignorewords = $words['ignore'];
print "<br>";
if (count($ignorewords)>0) {
print "<center class=\"commonReport\">".$sph_messages["ignoredWords"]." ";
while ($thisword=each($words['ignore'])) {
print " ".$thisword[1];
}
print "</center>";
}
if ($start==0)
$start=1;
$result = search($words, $category, $start, $results_per_page, $searchtype);
$query= stripslashes($query);
$entitiesQuery = htmlspecialchars($query);
if ($result==0)
print "<table width=\"100%\" class =\"resultReport\"><tr><td>".$sph_messages["The search"]." \"$entitiesQuery\" ".$sph_messages["noMatch"].".</td></tr></table>";
$endtime = getmicrotime() - $starttime;
$rows = $result['results'];
$time = round($endtime*100)/100;
$matchword = $sph_messages["matches"];
if ($rows == 1) {
$matchword= $sph_messages["match"];
}
$num_of_results = count($result) - 2;
if ($start < 2)
saveToLog(addslashes($query), $time, $rows);
$from = ($start-1) * $results_per_page+1;
$to = min(($start)*$results_per_page, $rows);
if ($result != 0 && $from <= $to)
print "<table width=\"100%\" class =\"resultReport\"><tr><td>".$sph_messages["Results"]." $from-$to".$sph_messages["of"]." $rows $matchword ($time ".$sph_messages["seconds"].")</td></tr></table><br>";
print "<table class=\"results\"><tr><td>";
if ($rows>0) {
$maxweight = $result['maxweight'];
$i = 0;
while ($i < $num_of_results && $i < $results_per_page) {
$title = $result[$i]['title'];
$url = $result[$i]['url'];
$fulltxt = $result[$i]['fulltxt'];
$pageSize = $result[$i]['size'];
if ($pageSize!="")
$pageSize = "- ".number_format($pageSize, 1)."kb";
$txtlen = strlen($fulltxt);
if ($txtlen > $desc_length) {
$places = array();
foreach($words['hilight'] as $word) {
$tmp = strtolower($fulltxt);
$found_in = strpos($tmp, $word);
$sum = -strlen($word);
while (!($found_in =='')) {
$pos = $found_in+strlen($word);
$sum += $pos; //FIX!!
$tmp = substr($tmp, $pos);
$places[] = $sum;
$found_in = strpos($tmp, $word);
}
}
sort($places);
$x = 0;
$begin = 0;
$end = 0;
while(list($id, $place) = each($places)) {
while ($places[$id + $x] - $place < $desc_length && $x+$id < count($places) && $place < strlen($fulltxt) -$desc_length) {
$x++;
$begin = $id;
$end = $id + $x;
}
}
$begin_pos = max(0, $places[$begin] - 30);
$fulltxt = substr($fulltxt, $begin_pos, $desc_length);
if ($places[$begin] > 0) {
$begin_pos = strpos($fulltxt, " ");
}
$fulltxt = substr($fulltxt, $begin_pos, $desc_length);
$fulltxt = substr($fulltxt, 0, strrpos($fulltxt, " "));
$fulltxt = "...".$fulltxt."...";
}
$weight = number_format($result[$i]['weight']/$maxweight*100, 2);
if ($title=='')
$title = $sph_messages["Untitled"];
$regs = Array();
foreach($words['hilight'] as $change) {
while (@eregi("[^\>](".$change.")[^\<]", " ".$title." ", $regs)) {
$title = eregi_replace($regs[1], "<b>".$regs[1]."</b>", $title);
}
while (@eregi("[^\>](".$change.")[^\<]", " ".$fulltxt." ", $regs)) {
$fulltxt = eregi_replace($regs[1], "<b>".$regs[1]."</b>", $fulltxt);
}
$url2 = $url;
while (@eregi("[^\>](".$change.")[^\<]", $url2, $regs)) {
$url2 = eregi_replace($regs[1], "<b>".$regs[1]."</b>", $url2);
}
}
if ($show_query_scores == 0) {
$weight = '';
} else
$weight = '['.$weight.'%]';
$num = $from + $i;
print "<b>$num.</b> $weight <a href=\"$url\"><font class=\"title\">$title</font></a><br><div class=\"description\">$fulltxt</div><font class=\"url\">$url2 $pageSize</font><br><br>\n";
$i++;
}
}
$pages = ceil($rows / $results_per_page);
$prev = $start - 1;
$next = $start + 1;
if ($from <= $to) {
if ($pages > 0)
print "<center>".$sph_messages["Result page"].": ";
if ($start<>1)
print "<a href=\"".$_SERVER['PHP_SELF']."?query=".addmarks($query)."&start=".$prev."&search=1&results=$results_per_page&type=$searchtype\">".$sph_messages["Previous"]."</a> ";
$firstpage = $start - $links_to_next;
if ($firstpage < 1) $firstpage = 1;
$lastpage = $start + $links_to_next;
if ($lastpage > $pages) $lastpage = $pages;
for ($x=$firstpage; $x<=$lastpage; $x++)
if ($x<>$start) {
print "<a href=".$_SERVER['PHP_SELF']."?query=".addmarks($query)."&start=$x&search=1&results=$results_per_page&type=$searchtype>$x</a> ";
}
else
print "<b>$x </b>";
if ($next <= $pages)
print "<a href=\"".$_SERVER['PHP_SELF']."?query=".addmarks($query)."&start=".$next."&search=1&results=$results_per_page&type=$searchtype\">".$sph_messages["Next"]."</a>";
}
if ($pages > 0 && $from <=$to) {
print "</center>";
?>
<?
}
function saveToLog ($query, $elapsed, $results) {
global $mysql_table_prefix;
mysql_query("insert into ".$mysql_table_prefix."query_log (query, time, elapsed, results) values ('$query', now(), '$elapsed', '$results')");
echo mysql_error();
}
switch ($search) {
case 1:
if (!isset($type)) {
$type = "";
}
if (!isset($results)) {
$results = "";
}
dosearch($query, $start, $category, $type, $results);
break;
default:
categories($catid);
break;
}
include "$include_dir/index_footer.inc";
?>