系统需求:PHP4.3.* /PHP5,PEAR::Pager
PEAR::Pager有两种分页显示模式,一是Jumping,二是Sliding。两者有什么区别,我们来看两个例子:
<?php
require_once ‘Pager/Pager.php’;
$params = array(
‘mode’ => ‘Jumping’,
‘perPage’ => 3,
‘delta’ => 5,
‘itemData’ => array(‘a’,’b’,’c’,’d’,’e’,’z’,’ty’,’xc’,’fg’,’fg’,’jk’,’hj’,’ty’,’xc’,’e’,’z’,’ty’,’xc’,’fg’,’fg’,’jk’,’hj’,’ty’,’xc’)
);
echo “
当前分页模式:”.$params[‘mode’];
echo “
每页显示数据条数:”.$params[‘perPage’];
echo “
显示页数:”.$params[‘delta’];
echo “
详细数据数组:”;
print_r($params[‘itemData’]);
$pager = & Pager::factory($params);
$data = $pager->getPageData();
$links = $pager->getLinks();
echo “
最后效果:”;
echo $links[‘all’];
echo $pager->linkTags;
echo ‘
当前页的数据: ‘ ;
echo “
";
print_r($data);
echo "
“;
echo “其它类方法得到的数据:
“;
echo ‘getCurrentPageID()…: ‘;
var_dump($pager->getCurrentPageID());
echo “
“;
echo ‘getNextPageID()……: ‘;
var_dump($pager->getNextPageID());
echo “
“;
echo ‘getPreviousPageID()..: ‘;
var_dump($pager->getPreviousPageID());
echo “
“;
echo ‘numItems()………..: ‘;
var_dump($pager->numItems());
echo “
“;
echo ‘numPages()………..: ‘;
var_dump($pager->numPages());
echo “
“;
echo ‘isFirstPage()……..: ‘;
var_dump($pager->isFirstPage());
echo “
“;
echo ‘isLastPage()………: ‘;
var_dump($pager->isLastPage());
echo “
“;
echo ‘isLastPageComplete().: ‘;
var_dump($pager->isLastPageComplete());
echo “
“;
echo ‘$pager->range……..: ‘;
var_dump($pager->range); echo “
“;
?>
运行结果如下图:
Sliding.php
<?php
require_once ‘Pager/Pager.php’;
$month = ‘september’;
$params = array(
‘mode’ => ‘Sliding’,
‘append’ => false,
‘urlVar’ => ‘num’,
‘path’ => ‘http://localhost/’ . $month,
‘fileName’ => ‘art%d.html’, //%d将被替换成当前页的数字
‘itemData’ => array(‘a’,’b’,’c’,’d’,’e’,’z’,’ty’,’xc’,’fg’,’fg’,’jk’,’hj’,’ty’,’xc’,’e’,’z’,’ty’,’xc’,’fg’,’fg’,’jk’,’hj’,’ty’,’xc’),
‘perPage’ => 3
);
echo “
当前分页模式:”.$params[‘mode’];
echo “
每页显示数据条数:”.$params[‘perPage’];
echo “
链接指向路径:”.$params[‘path’];
echo “
链接指向文件名:”.$params[‘fileName’];
echo “
详细数据数组:”;
print_r($params[‘itemData’]);
$pager = & Pager::factory($params);
$data = $pager->getPageData();
echo “
最后效果:”;
echo $pager->links;
echo ‘
当前页的数据: ‘ ;
echo “
";
echo 'Data for current page: '; print_r($data);
echo "
“;
echo “其它类方法得到的数据:
“;
echo ‘getCurrentPageID()…: ‘;
var_dump($pager->getCurrentPageID());
echo “
“;
echo ‘getNextPageID()……: ‘;
var_dump($pager->getNextPageID());
echo “
“;
echo ‘getPreviousPageID()..: ‘;
var_dump($pager->getPreviousPageID());
echo “
“;
echo ‘numItems()………..: ‘;
var_dump($pager->numItems());
echo “
“;
echo ‘numPages()………..: ‘;
var_dump($pager->numPages());
echo “
“;
echo ‘isFirstPage()……..: ‘;
var_dump($pager->isFirstPage());
echo “
“;
echo ‘isLastPage()………: ‘;
var_dump($pager->isLastPage());
echo “
“;
echo ‘isLastPageComplete().: ‘;
var_dump($pager->isLastPageComplete());
echo “
“;
echo ‘$pager->range……..: ‘;
var_dump($pager->range); echo “
“;
?>
运行结果如下图:
运行以上代码,可以发现,Jumping是跳跃式前进,一次翻几页,而Sliding是逐页地前进。
在例子中可以看到PEAR::Pager提供了很多类方法,可以返回我们所需要的数据,如当前页ID,下一页ID,当前页上数据条数等等。
下面将所有类方法的作用列出,供参考:
Pager::Pager() – 构造函数 参数为二维数组 详见上面的示例
Pager::factory() – 建立一个Pager对象
Pager::getCurrentPageID() 返回当前页的ID
Pager::getLinks() 返回某个页所对应的链接 参数为页面的ID 参数为空则返回当前页的链接
Pager::getNextPageID() 返回下一页的ID
Pager::getOffsetByPageId() 返回记录范围
例如当前页为1,每页显示10条,则返回(1,10)
如当前页为2,每页显示8条,则返回(8,16)
Pager::getPageData() 以数组形式返回当前页数据
Pager::getPageIdByOffset() 根据记录范围返回页面ID
本方法仅当分页模式为Jumping时有效
Pager::getPageRangeByPageId() 返回某个页面所在的记录范围
例如模式为Jumping时,如果pageId=3 , delta=10,则返回(1,10),如果pageId=3 , delta=10,则仍然返回(1,10),因为1,3都在10的范围之内。如果pageId =14,则返回(10,20),
因为14落于10,20之间。
Pager::getPreviousPageID() 得到前一页的ID
Pager::getperpageselectbox() 返回一个XHTML的Select标签字符串
可用于直接跳转到某个页面
Pager::isFirstPage() 判断当前页是否第一页
Pager::isLastPage() 判断当前页是否最后第一页
Pager::isLastPageComplete() 判断是否到达最后一页
Pager::numItems() 返回总记录数
Pager::numPages() 返回总页数
其它功能及用法请参看PEAR::Pager文档: