|
发表于 2004-9-4 12:21:59
|
显示全部楼层
在 MySQL 中用 LIMIT 来实现的啊。
SELECT * FROM post DESC LIMIT 0, 30; # 取前面 30 条记录
SELECT * FROM post LIMIT 31, 60; # 取 31 - 60 条记录
我是写了一个分页类的,实现的关键就是上面的查询啦。
[php]
<?php
// -----------------------------------------------------------------------
// Simple DB paging class.
// -----------------------------------------------------------------------
// Copyright (c) 2004 Bank Of China.
// Rik <riksken@163.net>
// -----------------------------------------------------------------------
// $db_paging.php,v 1.5 2003/08/23 15:37:36 Rik Exp $
// -----------------------------------------------------------------------
class DBPaging
{
var $dbQueryStr;
var $numPerPage;
var $recordsTotal;
var $pagesTotal;
var $currentPage;
var $pageVar;
var $pageNav;
/**
* @param string query string
* @param int number per page
* @param string page variable name
* @desc Constructor for this class.
*/
function DBPaging($queryStr, $numPerPage, $pageVar)
{
global $db; // db class is required, note that this is a customized class.
$this->dbQueryStr = $queryStr;
$this->numPerPage = $numPerPage;
$this->pageVar = $pageVar;
$this->currentPage = 1;
$this->recordsTotal = $db->numRows($db->query($queryStr));
$this->pagesTotal = ceil($this->recordsTotal / $this->numPerPage);
}
/**
* @return string query string
* @param int page number
* @desc Turn to the specified page and return the query string.
*/
function turnPage($pageNum = 1)
{
if ($pageNum < 1 or $pageNum > $this->pagesTotal)
{
$pageNum = 1;
}
$this->currentPage = $pageNum;
return $this->dbQueryStr." LIMIT " . ($pageNum - 1) * $this->numPerPage . "," . $this->numPerPage;
}
/**
* @return string html code of the navigator
* @desc Build a navigator.
*/
function makePageNav()
{
global $_GET, $PHP_SELF;
// build navigator when $this->pagesTotal > 1
if ($this->pagesTotal <= 1)
{
return "";
}
// extract url codes and form "hidden" codes
$inputCodes = "";
$urlCodes = "";
foreach ($_GET as $key => $value)
{
if ($key != $this->pageVar)
{
$inputCodes .= "<input type=\"hidden\" name=\"$key\" value=\"$value\" />\n";
if ($urlCodes != '')
{
$urlCodes .= '&';
}
$urlCodes .= $key . '=' . $value;
}
}
if ($urlCodes != "")
{
$urlCodes = $PHP_SELF . "?" . $urlCodes . "&";
}
else
{
$urlCodes = $PHP_SELF . "?";
}
// start to build page jumping form
$this->pageNav = "<form method=\"get\" action=\"$PHP_SELF\" style=\"margin:0\">\n";
if ($this->currentPage > 1)
{
$this->pageNav .= '<b><a href="' . $urlCodes . $this->pageVar . '=1">第一页</a></b> ';
}
if ($this->currentPage <= 1)
{
$this->pageNav .= "<a href=\"" . $urlCodes . $this->pageVar . "=" . ($this->currentPage + 1) . "\">下一页<strong>»</strong></a>\n";
}
elseif($this->currentPage == $this->pagesTotal)
{
$this->pageNav .= "<a href=\"" . $urlCodes . $this->pageVar . "=" . ($this->currentPage - 1) . "\"><strong>«</strong>上一页</a>\n";
}
else
{
$this->pageNav .= "<a href=\"" . $urlCodes . $this->pageVar . "=" . ($this->currentPage - 1) . "\"><strong>«</strong>上一页</a> <a href=\"" . $urlCodes . $this->pageVar . "=" . ($this->currentPage + 1) . "\">下一页<strong>»</strong></a>\n";
}
if ($this->currentPage < $this->pagesTotal)
{
$this->pageNav .= ' <b><a href="' . $urlCodes . $this->pageVar . '=' . $this->pagesTotal . '">最后页</a></b>';
}
$this->pageNav .= " 共" . $this->recordsTotal . "条信息 " . $this->currentPage . "/" . $this->pagesTotal . " 页 <input type=\"text\" name=\"" . $this->pageVar . "\" size=\"1\"> 页</span>" . $inputCodes . " <input type=\"submit\" value=\"跳转\" /></form>\n";
return $this->pageNav;
}
}
?>
[/php] |
|