如何为搜索结果添加分页?

时间:2012-12-18 14:42:14

标签: php mysql search pagination

我想在此搜索引擎中添加分页。这就是我的尝试:

//get date
$button = $_GET['submit'];
$search = $_GET['search'];

if (!$button)
   echo "Please fill out the form";
else
{
   if (strlen($search)<=2) 
      echo "The item you searched for was to small";
   else
   {
      echo "You searched for <b>$search</b> <hr size='1'>";

     //connect to database

     mysql_connect('localhost','wnumber','passowrd');
     mysql_select_db('wnumber');


     //explode search term
     $search_exploded = explode(" ",$search);
     foreach($search_exploded as $search_each) 
     {
        $str = mysql_real_escape_string(substr($search_each, 0, 4));
        //construct query
        $x++;
        if ($x==1) $construct .= "keywords LIKE '$str%'";
        else       $construct .= " OR keywords LIKE '$str%'";
     }

     //echo out construct
     $construct = "SELECT * FROM word WHERE $construct order by LENGTH(`keywords`)";
     $run = mysql_query($construct);
     $foundnum = mysql_num_rows($run);

     if ($foundnum==0)
        echo "No results found.";
     else 
     {
        echo "$foundnum results found.<p><hr size='1'>";
        while ($runrows = mysql_fetch_assoc($run)) 
        {

           //get data
           $meaning = $runrows['meaning'];
           $keywords = $runrows['keywords'];

           echo "<b>$keywords</b><br>
           <b>$meaning</b><br>
        }
     }
  }

}

3 个答案:

答案 0 :(得分:1)

对于分页,您应该使用SELECT查询的LIMIT部分。阅读文档:

http://dev.mysql.com/doc/refman/5.0/en/select.html

例如,如果您希望每页有20个项目并想要转到第3页,则应在“$ run = mysql_query($ construct);”之前添加以下行:

$construct = $construct . " LIMIT 60, 20"

在这种情况下,您跳过前60条记录并选择下一条20。

<强>更新

对于HTML标记,您可以使用以下内容:

<div id="paginator">
  <ul>
    <li><a href="page.php?submit=true&search=text_to_search&page=1>1</a></li>
    <li><a href="page.php?submit=true&search=text_to_search&page=2>2</a></li>
    etc.
  </ul>
</div>

然后在开头的$ button = $ _GET ['button']之后添加以下行:

$page = ( !empty( $_GET['page'] ) ) ? ( (int)$_GET['page'] ) : ( 1 );

并将我的行从上方更改为:

$construct = $construct . " LIMIT {($page-1)*20}, 20"

这应该可以解决问题。

答案 1 :(得分:0)

看一下这个例子,这就是你如何用MySQL进行分页。

第一个参数是要开始的行,第二个参数是要显示的结果数

mysql> select * From t1 limit 0,5;
+----------+------------+--------------+---------------------+
| actor_id | first_name | last_name    | last_update         |
+----------+------------+--------------+---------------------+
|        1 | PENELOPE   | GUINESS      | 2006-02-15 04:34:33 |
|        2 | NICK       | WAHLBERG     | 2006-02-15 04:34:33 |
|        3 | ED         | CHASE        | 2006-02-15 04:34:33 |
|        4 | JENNIFER   | DAVIS        | 2006-02-15 04:34:33 |
|        5 | JOHNNY     | LOLLOBRIGIDA | 2006-02-15 04:34:33 |
+----------+------------+--------------+---------------------+
5 rows in set (0.00 sec)

mysql> select * From t1 limit 5,5;
+----------+------------+-----------+---------------------+
| actor_id | first_name | last_name | last_update         |
+----------+------------+-----------+---------------------+
|        6 | BETTE      | NICHOLSON | 2006-02-15 04:34:33 |
|        7 | GRACE      | MOSTEL    | 2006-02-15 04:34:33 |
|        8 | MATTHEW    | JOHANSSON | 2006-02-15 04:34:33 |
|        9 | JOE        | SWANK     | 2006-02-15 04:34:33 |
|       10 | CHRISTIAN  | GABLE     | 2006-02-15 04:34:33 |
+----------+------------+-----------+---------------------+
5 rows in set (0.00 sec)

mysql> select * From t1 limit 10,5;
+----------+------------+-----------+---------------------+
| actor_id | first_name | last_name | last_update         |
+----------+------------+-----------+---------------------+
|       11 | ZERO       | CAGE      | 2006-02-15 04:34:33 |
|       12 | KARL       | BERRY     | 2006-02-15 04:34:33 |
|       13 | UMA        | WOOD      | 2006-02-15 04:34:33 |
|       14 | VIVIEN     | BERGEN    | 2006-02-15 04:34:33 |
|       15 | CUBA       | OLIVIER   | 2006-02-15 04:34:33 |
+----------+------------+-----------+---------------------+
5 rows in set (0.00 sec)

答案 2 :(得分:0)

您可以使用JavaScript库来显示分页。 http://datatables.net

您可以将php结果提供给此库以创建分页。