<?

   $link 
mysql_connect("localhost","dbuser","dbpasswd");
   
mysql_select_db("news"$link);

  if (!(
$start)) {
    
$start 0;
  }

?>
<html>
<head>
<title>Quick search</title>
</head>

<body bgcolor="#ffffff">

<form action="/news/search.php" method="get">

<b>Search for:</b> <input type="text" name="searchstring" size="40" maxlength="254" value="<? echo $searchstring?>" /> <input type="submit" value="Search" />

</form>

<? if ($searchstring != "") { ?>

<table border="0" cellpadding="5" cellspacing="0">

  <tr>
    <td bgcolor="#cccccc"><b>Subject</b></td>
    <td bgcolor="#cccccc"><b>Author</b></td>
    <td bgcolor="#cccccc"><b>Date</b></td>
    <td bgcolor="#cccccc"><b>Group</b></td>
  </tr>

  <?
  
  $searchstring_clean 
addslashes($searchstring);
  
  
$query "SELECT posts.id, posts.subject, posts.msg_from, posts.post_date, groups.name,
            (MATCH(subject, post_body) AGAINST('
$searchstring_clean')) AS score
            FROM posts LEFT JOIN posts_to_groups ON posts_to_groups.post_id=posts.id
        LEFT JOIN groups ON groups.id=posts_to_groups.group_id
        WHERE 1=1"
;

  
/* split up the search string into words */

  
$searchstrings = array();

  
$searchstrings explode(" "$searchstring);
  
  while (list(
$key$searchword) = each($searchstrings)) {

    
$query .= " AND MATCH(subject, post_body) AGAINST('$searchword')";
    
  }

  
$start_time = array();
  
$start_time gettimeofday();
        
  
$result mysql_query($query$link);
  
$numRows mysql_num_rows($result);
  
  if (
$numRows 0) {
    
mysql_data_seek($result$start);
  }
  
  
$next 100;
  
  if (
$numRows < ($start + ($next 2))) {
    
$next $numRows $start $next;
  }
  
  
$end_time = array();
  
$end_time gettimeofday();
  
  
$searchtime = ($end_time[sec] - $start_time[sec]) . "." . (abs($end_time[usec] - $start_time[usec]));
  
  
?>
  
  <? if ($numRows 0) { ?>
  <tr>
    <td bgcolor="#eeeeee" colspan="2"><? if ($start 99) { ?><a href="/news/search.php?searchstring=<? echo urlencode($searchstring); ?>&start=<? echo ($start 100); ?>">&lt;&lt; prev 100</a><? } else { ?>&nbsp;<? ?></td>
    <td bgcolor="#eeeeee" colspan="2"><? if ($next 0) { ?><a href="/news/search.php?searchstring=<? echo urlencode($searchstring); ?>&start=<? echo ($start 100); ?>">next <? echo $next?>&gt;&gt;</a><? } else { ?>&nbsp;<? ?></td>
  </tr>
  <? ?>
  
  <tr>
    <td colspan="4">
    <i><? echo $numRows?> results, displaying <? echo $start?> - <? if ($next 0) { echo ($start 100); } else { echo $numRows; } ?>; search took <? echo $searchtime?> seconds</i>
    </td>
  </tr>

  <?

  $cnt 
0;
  
  while(
$row mysql_fetch_array($result)) {
  
    
?>
    <tr>
      <td><a href="/news/view_post.php?id=<? echo $row[id]; ?>"><? echo substr($row[subject], 080); ?></a></td>
      <td><? echo substr($row[msg_from], 020); ?></td>
      <td><? echo $row[post_date]; ?></td>
      <td><? echo $row[name]; ?></td>
    </tr>
    <?
    
    $cnt
++;
    
    if (
$cnt == 100) {
      break;
    }
  }
  
  
  
?>

</table>

<? ?>

</body>
</html>