如何为搜索引擎添加分页?

时间:2016-02-20 22:19:19

标签: php search pagination

我创建了一个包含多个字段的PHP搜索引擎,每件事情都很好而且工作超级但我需要分页,我想问一下,如何为我的搜索引擎表单创建分页以及如何在一个中显示所有记录点击。

这是我的代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>search</title>
<link rel="stylesheet" type="text/css"href="http://www.sayahonline.com/a/my.css">
<link href="http://www.sayahonline.com/templates/sayah/favicon.ico" rel="shortcut icon">


</head>
<body>


<center>
<form name="search" method="post" action="<?=$PHP_SELF?>">
 <Select NAME="field">
 <Option VALUE="idno">ID</option>
 <Option VALUE="name">Name</option>
 <Option VALUE="fname">Father Name</option>
 <Option VALUE="province">Province</option>
 </Select>
 <input placeholder="Search" type="text" name="find" /> 
 <input type="hidden" name="searching" value="yes" />
 <input type="submit" name="search" value="Search" />
 </form>


<table border="1" width="">
    <thead>
        <tr>
            <th><center>ID</center></th>
            <th><center>Name</center></th>
            <th><center>Father Name</center></th>            
            <th><center>G/F Name</center></th>
            <th><center>School</center></th>
            <th><center>Province</center></th>
            <th><center>Grade</center></th>
            <th><center>Result</center></th>
        </tr>
    </thead>
    <tbody id="tbl">
    <p></p>
<? 
//get information
$field = @$_POST['field'] ;
$find = @$_POST['find'] ;
$searching = @$_POST['searching'] ;

 //once submitted  
 if ($searching =="yes") 
 { 
 echo "<p></p>"; 


 //blank search returns all results


 mysql_connect("localhost","User","Password") or die(mysql_error()); 
 mysql_select_db("Name") or die(mysql_error()); 
 mysql_query('SET NAMES utf8');

 //filter search term 
 $find = strtoupper($find); 
 $find = strip_tags($find); 
 $find = trim ($find); 

 //search database 

$getquery = mysql_query("SELECT * FROM Table WHERE upper($field) LIKE'%$find%' LIMIT $start, $per_page"); 



//display the results 
while($result = mysql_fetch_assoc($getquery))
{
$idno = $result ['idno'];
$name = $result ['name'];
$fname = $result ['fname'];
$gfname = $result ['gfname'];
$school = $result ['school'];
$province = $result ['province'];
$grade = $result ['grade'];
$result = $result ['result'];


echo "
        <tr>
            <td><center>$idno</center></td>
            <td>$name</td>      
            <td>$fname</td>
            <td>$gfname</td>
            <td>$school</td>
            <td>$province</td>
            <td><center>$grade</center></td>
            <td>$result</td>
        </tr>
</center> 
";

}


 //number of results or error 
 $anymatches=mysql_num_rows($getquery); 
 if ($anymatches == 0) 
 { 
 echo "Sorry..."; 
 } 

 //the search term 
 echo "Result: <b>$find</b> | Total: <b>$anymatches</b> <hr size='1'>"; 
 } 




 ?>


</body>
</html>

2 个答案:

答案 0 :(得分:0)

您需要做的是修改搜索查询。 1)搜索查询条件。 2)限制查询说,50左右。 3)将查询限制存储在会话中。 4)创建一个&#34;接下来的50&#34;和之前的50&#34;纽扣。 5)单击它们时再次运行查询并使用会话限制作为查询中的起始指针。 6)转到第2步。

希望有所帮助。

答案 1 :(得分:0)

From the MySQL documentation

SELECT * FROM tbl LIMIT 5,10;  # Retrieve rows 6-15 

只需根据当前页面从数据库中检索数据。