PHPページングの原理はどうなっていますか
9371 ワード
1、
, web 。 web , , , , , 。 , php 。
2、
, , :
($PageSize)?
($CurrentPageID)?
, 。
, : ($PreviousPageID)、 ($NextPageID)、 ($numPages) , 。
mysql , ,sql :select * from table limit offset, rows。 sql , 。
10 :select * from table limit 0,10
11 20 :select * from table limit 10,10
21 30 :select * from table limit 20,10
……
sql $PageSize=10 sql , :
select * from table limit ($CurrentPageID - 1) * $PageSize, $PageSize
sql 。 , , sql php 。 。
3、
, , , , 。
<?php
//
$link = mysql_connect("localhost", "mysql_user", "mysql_password")
or die("Could not connect: " . mysql_error());
//
if( isset($_GET['page']) ){
$page = intval( $_GET['page'] );
}
else{
$page = 1;
}
//
$PageSize = 10;
//
$sql = "select count(*) as amount from table";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$amount = $row['amount'];
//
if( $amount ){
if( $amount < $page_size ){ $page_count = 1; } // $PageSize,
if( $amount % $page_size ){ //
$page_count = (int)($amount / $page_size) + 1; // ,
}else{
$page_count = $amount / $page_size; // ,
}
}
else{
$page_count = 0;
}
//
$page_string = '';
if( $page == 1 ){
$page_string .= ' | |';
}
else{
$page_string .= '<a href="/?page=1>"; </a>|<a href="/?page='."($page-1).'> </a>|';
}
if( ($page == $page_count) || ($page_count == 0) ){
$page_string .= ' | ';
}
else{
$page_string .= '<a href="/?page='."($page+1).'> </a>|<a href="/?page='."$page_count.'> </a>';
}
// ,
if( $amount ){
$sql = "select * from table order by id desc limit ". ($page-1)*$page_size .", $page_size";
$result = mysql_query($sql);
while ( $row = mysql_fetch_row($result) ){
$rowset[] = $row;
}
}else{
$rowset = array();
}
// , , foreach
?>
4、OO
pear db
<?php
// FileName: Pager.class.php
// , ,
Class Pager
{
var $PageSize; //
var $CurrentPageID; //
var $NextPageID; //
var $PreviousPageID; //
var $numPages; //
var $numItems; //
var $isFirstPage; //
var $isLastPage; //
var $sql; //sql
function Pager($option)
{
global $db;
$this->_setOptions($option);
//
if ( !isset($this->numItems) )
{
$res = $db->query($this->sql);
$this->numItems = $res->numRows();
}
//
if ( $this->numItems > 0 )
{
if ( $this->numItems < $this->PageSize ){ $this->numPages = 1; }
if ( $this->numItems % $this->PageSize )
{
$this->numPages= (int)($this->numItems / $this->PageSize) + 1;
}
else
{
$this->numPages = $this->numItems / $this->PageSize;
}
}
else
{
$this->numPages = 0;
}
switch ( $this->CurrentPageID )
{
case $this->numPages == 1:
$this->isFirstPage = true;
$this->isLastPage = true;
break;
case 1:
$this->isFirstPage = true;
$this->isLastPage = false;
break;
case $this->numPages:
$this->isFirstPage = false;
$this->isLastPage = true;
break;
default:
$this->isFirstPage = false;
$this->isLastPage = false;
}
if ( $this->numPages > 1 )
{
if ( !$this->isLastPage ) { $this->NextPageID = $this->CurrentPageID + 1; }
if ( !$this->isFirstPage ) { $this->PreviousPageID = $this->CurrentPageID - 1; }
}
return true;
}
/***
*
*
* , ,
* , getPageData
* getPageData
*
***/
function getDataLink()
{
if ( $this->numItems )
{
global $db;
$PageID = $this->CurrentPageID;
$from = ($PageID - 1)*$this->PageSize;
$count = $this->PageSize;
$link = $db->limitQuery($this->sql, $from, $count); // Pear DB::limitQuery
return $link;
}
else
{
return false;
}
}
/***
*
*
*
***/
function getPageData()
{
if ( $this->numItems )
{
if ( $res = $this->getDataLink() )
{
if ( $res->numRows() )
{
while ( $row = $res->fetchRow() )
{
$result[] = $row;
}
}
else
{
$result = array();
}
return $result;
}
else
{
return false;
}
}
else
{
return false;
}
}
function _setOptions($option)
{
$allow_options = array(
'PageSize',
'CurrentPageID',
'sql',
'numItems'
);
foreach ( $option as $key => $value )
{
if ( in_array($key, $allow_options) && ($value != null) )
{
$this->$key = $value;
}
}
return true;
}
}
?>
<?php
// FileName: test_pager.php
// , pear db
require "Pager.class.php";
if ( isset($_GET['page']) )
{
$page = (int)$_GET['page'];
}
else
{
$page = 1;
}
$sql = "select * from table order by id";
$pager_option = array(
"sql" => $sql,
"PageSize" => 10,
"CurrentPageID" => $page
);
if ( isset($_GET['numItems']) )
{
$pager_option['numItems'] = (int)$_GET['numItems'];
}
$pager = @new Pager($pager_option);
$data = $pager->getPageData();
if ( $pager->isFirstPage )
{
$turnover = " | |";
}
else
{
$turnover = "<a href='?page=1&numItems=".$pager->numItems."'> </a>|<a href="/?page=".$pager->PreviousPageID."&numItems=".$pager->numItems."'> </a>|";
}
if ( $pager->isLastPage )
{
$turnover .= " | ";
}
else
{
$turnover .= "<a href="/?page=".$pager->NextPageID."&numItems=".$pager->numItems."'> </a>|<a href="/?page=".$pager->numPages."&numItems=".$pager->numItems."'> </a>";
}
?>
:
, , 。 , , Pager , :
<?php
Class MemberPager extends Pager
{
function showMemberList()
{
global $db;
$data = $this->getPageData();
//
// ......
}
}
///
if ( isset($_GET['page']) )
{
$page = (int)$_GET['page'];
}
else
{
$page = 1;
}
$sql = "select * from members order by id";
$pager_option = array(
"sql" => $sql,
"PageSize" => 10,
"CurrentPageID" => $page
);
if ( isset($_GET['numItems']) )
{
$pager_option['numItems'] = (int)$_GET['numItems'];
}
$pager = @new MemberPager($pager_option);
$pager->showMemberList();
?>
, 。
mysql: select * from table limit offset, rows
pgsql: select * from table limit m offset n
......
pear db limitQuery 。
ok, , 。