1) {
$i = goto_page("i", $tables, $total);
$seek_to = goto_page ("seek_to", $tables, $total) + 1;
}
$results_page = 0;
/* Pull the page together */
for ($i = $i; $i < $num_tables; $i++) {
$query = build_query ($tables[$i]);
$result = db_Query ($query);
$num_results = db_num_Rows ($result);
$num_results -= $seek_to;
db_seek_to ($result, $seek_to);
$seek_to = 0;
for ($j = 0; $j < $num_results; $j++) {
$row = db_fetch_array ($result);
/* ensures backward compatiblity with tables with
different field names */
$db_title = "title";
$db_date = "date";
$db_text = "text";
/* sets type, table fields, and doc URL */
switch ($tables[$i]) {
case "docs_comments":
$type = "Document Comment";
$id = substr ($row["topic"], 5, 3);
$href = "./index.php?main=sections&showdoc=$id&comment_topic=".$row["topic"];
break;
case "news_comments":
$type = "News Comment";
$id = substr ($row["topic"], 5, 3);
$href = "./index.php?main=news_single&newsid=$id&comment_topic=".$row["topic"];
break;
case "poll_comments":
$type = "Poll Comment";
$href = "./index.php?main=poll_results";
break;
case "mboard_messages":
$type = "BB Message";
$title = get_topic ($row["topic"]);
$href = make_mboard_link ($row["topic"], $row["id"]);
break;
case "project":
$type = "Project";
$db_text = "description";
$db_title = "name";
break;
case "docs":
$type = $row["type"];
$db_date = "time";
$db_text = "descr";
break;
case "news":
$type = "News";
$href = "./index.php?main=news_single&newsid=".$row["id"];
}
if ($tables[$i] != "mboard_messages")
$title = $row[$db_title];
$uid = $row["poster"];
$poster = get_poster ($uid);
$results_page++;
$date = $row[$db_date];
$desc = $row[$db_text];
if (strlen ($desc) > $max_desc_len)
$desc = substr ($desc, 0, $max_desc_len)."...";
$desc = change_smilies ($desc);
if ($results_page <= $results_pp) {
include ("./templates/$user_theme/search_result.php");
}
}
}
/* include the search footer and add an extra index for convenience */
include ("./templates/$user_theme/search_footer.php");
include ("./templates/$user_theme/search_index.php");
}
function build_query($s_form)
{
global $q;
$query_string = explode (" ", $q);
$num_words = sizeof ($query_string);
/* Pull together the fields we'll need */
switch ($s_form) {
case "docs_comments":
case "news":
case "poll_comments":
case "news_comments":
$fields = array ("text", "title");
break;
case "projects":
$fields = array ("name", "description");
break;
case "docs":
$fields = array ("title", "descr");
break;
case "mboard_messages":
$fields = array ("text");
}
$num_fields = sizeof ($fields);
/* Make the SQL query */
$query = "SELECT * FROM $s_form WHERE ";
for ($i = 0; $i < $num_fields; $i++) {
for ($j = 0; $j < $num_words; $j++) {
if ($j == ($num_words - 1) && $i == ($num_fields - 1))
$query .= "$fields[$i] LIKE '%$query_string[$j]%'";
else
$query .= "$fields[$i] LIKE '%$query_string[$j]%' OR ";
}
}
return $query;
}
function get_poster($uid)
{
if ($uid == 0) return "Guest";
$query = "SELECT * FROM users WHERE id=$uid";
$result = db_Query ($query);
$row = db_fetch_array ($result);
return $row["username"];
}
function get_topic($id)
{
$query = "SELECT * FROM mboard_topics WHERE id=$id";
$result = db_Query ($query);
$row = db_fetch_array ($result);
return $row["subject"];
}
function get_num_results($tables)
{
global $q;
$num_tables = sizeof ($tables);
$num_results = 0;
for ($i = 0; $i < $num_tables; $i++) {
$query = build_query ($tables[$i]);
$result = db_Query ($query);
$num_results += db_num_Rows ($result);
}
return $num_results;
}
function goto_page($what_var, $tables, $total)
{
global $page, $results_pp;
$num_tables = sizeof ($tables);
$go_till = ($page - 1) * $results_pp;
$results = 0;
for ($i = 0; $i < $num_tables; $i++) {
$query = build_query ($tables[$i]);
$result = db_Query ($query);
$num_results = db_num_Rows ($result);
for ($j = 0; $j < $num_results; $j++) {
$results++;
if ($results == $go_till) {
/* set the return values */
$r_i = $i;
$r_results = $j;
/* no use wasting valuable server time */
$j = $num_results;
$i = $num_tables;
}
}
}
// echo "i: ".$r_i." results: ".$r_results;
if ($what_var == "i")
return $r_i;
else
return $r_results;
}
function change_smilies($text)
{
global $user_theme;
$text = str_replace ("[smile]", "", $text);
$text = str_replace ("[wink]", "", $text);
$text = str_replace ("[razz]", "", $text);
$text = str_replace ("[sad]", "", $text);
$text = str_replace ("[vague]", "", $text);
$text = str_replace ("[neutral]", "", $text);
$text = str_replace ("[grin]", "", $text);
$text = str_replace ("[mad]", "", $text);
$text = str_replace ("[suprised]", "", $text);
$text = str_replace ("[cool]", "", $text);
$text = str_replace ("[confused]", "", $text);
return $text;
}
function make_mboard_link ($topic, $id)
{
global $msgs_per_page;
$query = "SELECT * FROM mboard_messages WHERE topic=$topic";
$result = db_Query ($query);
$num_results = db_num_Rows ($result);
$thing = 0;
$db_id = 0;
while ($db_id != $id) {
$row = db_fetch_array ($result);
$db_id = $row["id"];
$thing++;
}
$page_num = ceil ($thing / $msgs_per_page);
return "./index.php?main=mb&topic=$topic&page=$page_num";
}
?>