我有一个搜索引擎,如何添加一段分页代码?
代码有点长,但是下面一大段只是判断关键字AND 和OR的功能。
$numrows就是搜索结果的总数量。
<form method="GET" action="<?php echo $_SERVER['PHP_SELF'];?>" name="searchForm">
          <p> Search For: 
            <input type="text" class="textfield" name="keyword" onBlur="if(this.value==''){this.value='name address'}" onFocus="if(this.value=='name address'){this.value=''}" value="name address"/>
   <input type="submit" name="submit" value="Search!" />
   </p>
        </form>
 // 搜索框
<?php
include("config.php");
 mysql_connect($HostName,$UserName,$MySQLPassword)
 or die("ERROR: Could not connect to database!");
  mysql_select_db($Database) or die("cannot select db"); //连接数据库
 mysql_query("SET NAMES 'utf8'"); 
 $default_sort = 'id';
 $allowed_order = array ('id','title','content','cotelogo'); //允许搜索的数据表
 if (!isset ($_GET['order']) || 
     !in_array ($_GET['order'], $allowed_order)) {
     $order = $default_sort;
 } else {
     $order = $_GET['order'];
 }
 if (isset($_GET['keyword'])) {
         if(!$_GET['keyword']) {
           die('<p>Please enter a search term.</p>');
    }     
 $tables = 'jaws_home'; //数据库名
 $return_fields = 'title content cotelogo'; //返回的数据表名
 $check_fields = 'title content'; //检测的数据表名
  $query_text = $_GET['keyword'];
 $clean_query_text =cleanQuery($query_text);
 $newquery=bq_simple ($return_fields, $tables, $check_fields, $clean_query_text);
 $newquery = $newquery . " ORDER BY $order;"; //获取输入值搜索排列
 $result = mysql_query($newquery) or die(mysql_error());
 $numrows = mysql_num_rows($result);
 if ($numrows == 0) {
     echo "<H1>No data to display!</H1>";
     exit;
 }
 echo  "<p>Your search '$query_text' returned ".$numrows. " results.</p>\n";
//$numrows就是搜索的总数量;
 $row = mysql_fetch_assoc ($result);
 echo "<div class='section'>\n";
 echo "<table border=0>\n";
   echo "<tr>\n";    
     echo "<td width='50%'>title</a></td>\n";
     echo "<td width='15%'>content</td>\n";
     echo "</tr>\n\n"; 
       while ($row = mysql_fetch_assoc ($result)) {
     echo "<tr>\n";    
     echo "<td width='50%'>".$row["title"]."</td>\n";
   echo "<td width='15%'>".$row["content"]."</td>\n";  //得出的结果输出格式     echo "</tr>\n";
 }
 echo "</table>\n";
 echo "</div>\n";
 }
//下面都是功能代码,只是判断AND,OR的关系,以及结果URL形式
function autolink($foo) 
{
$foo = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="\1" target=_blank rel=nofollow>\1</a>', $foo);
if( strpos($foo, "http") === FALSE ){
$foo = eregi_replace('(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="http://\1" target=_blank rel=nofollow >\1</a>', $foo);
}else{
$foo = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '\1<a href="http://\2" target=_blank rel=nofollow >\2</a>', $foo);
}
return $foo; 

 function cleanQuery($string)
 {
   $string = trim($string);
   $string = strip_tags($string);  //删除所有的HTML / JavaScript
   if(get_magic_quotes_gpc())
   {
     $string = stripslashes($string);
   }
   if (phpversion() >= '4.3.0')
   {
     $string = mysql_real_escape_string($string);
   }
   else
   {
     $string = mysql_escape_string($string);
   }
   return $string;
 }
//用于支持“关键字+关键字”语法以及没有关键字的用法
function bq_handle_shorthand($text) {
 $text = preg_replace("/ \+/", " and ", $text);
 $text = preg_replace("/ -/", " not ", $text);
 return $text;
}
//下面用于建立查询时,保持关键字的结合 i.e.... [fish and chips and "chipped ham"] syntax
function bq_explode_respect_quotes($line) {
        $quote_level = 0; 
        $buffer = "";
        for ($a = 0; $a < strlen($line); $a++) {
                if ($line[$a] == "\"") {
                        $quote_level++;
                        if ($quote_level == 2) { $quote_level = 0; }
                }
                else {
                        if ($line[$a] == " " and $quote_level == 0) {
                                $buffer = $buffer . "~~~~";  
                        }
                        else {
                                $buffer = $buffer . $line[$a];
                        }
                }
        }
 $buffer = str_replace("\\", "", $buffer);
        $array = explode("~~~~", $buffer);
        return $array;
}
//下面用于一个关键字来查询数据库里任意字段的关键字
function bq_make_subquery($fields, $word, $mode) {
 if ($mode == "not") {
  $back = " LIKE '%$word%'))";
 }
 else {
  $back = " LIKE '%$word%')";
 }
 if ($mode == "not") {
  $front = "(NOT (";
  $glue = " LIKE '%$word%' OR ";
 }
 else {
  $front = "(";
  $glue = " LIKE '%$word%' OR ";
 }
 $text = str_replace(" ", $glue, $fields);
 $text = $front . $text . $back;
 return $text;
}
//下面生成“WHERE” 的部分查询,对每个给定的搜索字符串的关键词迭代。是否安全的链接(例如, AND)与反复调用输出。
function bq_make_query($fields, $text) {
 $text = strtolower($text);
 $text = bq_handle_shorthand($text);
 $wordarray = bq_explode_respect_quotes($text);
 $buffer = "";
 $output = "";
 for ($i = 0; $i<count($wordarray); $i++) {
  $word = $wordarray[$i];
  if ($word == "and" or $word == "or" or $word == "not" and $i > 0) {
   if ($word == "not") {
    $i++;
    if ($i == 1) {  
     $buffer = bq_make_subquery($fields, $wordarray[$i], "not");
    }
    else {
     $buffer = " AND " . bq_make_subquery($fields, $wordarray[$i], "not");
    }
   }
   else {
    if ($word == "or") {
     $i++;
     if ($i == 1) {
      $buffer = bq_make_subquery($fields, $wordarray[$i], "");
     }
     else {
      $buffer = " OR " . bq_make_subquery($fields, $wordarray[$i], "");
     }
    }
    else {
     if ($word == "and") {
      $i++;
      if ($i == 1) {
       $buffer = bq_make_subquery($fields, $wordarray[$i], "");
      }
      else {
       $buffer = " AND " . bq_make_subquery($fields, $wordarray[$i], "");
      }
     }
    }
   }
  }
  else {
   if ($i == 0) {  
    $buffer = bq_make_subquery($fields, $wordarray[$i], "");
   }
   else {
    $buffer = " and " . bq_make_subquery($fields, $wordarray[$i], "");
   }
  }
  $output = $output . $buffer;
 }
 return $output;
}
//下面生成一个简单的布尔查询。如果你直接使用bq_make_query,需要进一步补充的复杂程度
function bq_simple ($return_fields, $tables, $check_fields, $query_text) {
 $return_fields = str_replace(" ", ", ", $return_fields);
 $tables = str_replace(" ", ", ", $tables);
 $query = "SELECT $return_fields FROM $tables WHERE ";
 $query = $query . bq_make_query($check_fields, $query_text);
 return $query;
}
?>