动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
  
   PHP → 阅读文章

 一个分页导航类

作者来源: 
阅读 数 345 人次 , 2006-3-29 4:08:00 


<?php
// +----------------------------------------------------------------------+
// | php version 4 |
// +----------------------------------------------------------------------+
// | copyright (c) 1997-2002 the php group |
// +----------------------------------------------------------------------+
// | this source file is subject to version 2.02 of the php license, |
// | that is bundled with this package in the file license, and is |
// | available at through the world-wide-web at  |
// | http://www.php.net/license/2_02.txt.  |
// | if you did not receive a copy of the php license and are unable to  |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately.  |
// +----------------------------------------------------------------------+
// | authors: richard heyes <richard@phpguru.org>  |
// +----------------------------------------------------------------------+

/**
* pager class
*
* handles paging a set of data. for usage see the example.php provided.
*
*/

class pager {

/**
* current page
* @var integer
*/
var $_currentpage;

/**
* items per page
* @var integer
*/
var $_perpage;

/**
* total number of pages
* @var integer
*/
var $_totalpages;

/**
* item data. numerically indexed array...
* @var array
*/
var $_itemdata;

/**
* total number of items in the data
* @var integer
*/
var $_totalitems;

/**
* page data generated by this class
* @var array
*/
var $_pagedata;

/**
* constructor
*
* sets up the object and calculates the total number of items.
*
* @param $params an associative array of parameters this can contain:
* currentpage  current page number (optional)
* perpage  items per page (optional)
* itemdata data to page
*/
function pager($params = array())
{
global $http_get_vars;

$this->_currentpage = max((int)@$http_get_vars['pageid'], 1);
$this->_perpage  = 8;
$this->_itemdata = array();

foreach ($params as $name => $value) {
$this->{'_' . $name} = $value;
}

$this->_totalitems = count($this->_itemdata);
}

/**
* returns an array of current pages data
*
* @param $pageid desired page id (optional)
* @return array page data
*/
function getpagedata($pageid = null)
{
if (isset($pageid)) {
if (!empty($this->_pagedata[$pageid])) {
return $this->_pagedata[$pageid];
} else {
return false;
}
}

if (!isset($this->_pagedata)) {
$this->_generatepagedata();
}

return $this->getpagedata($this->_currentpage);
}

/**
* returns pageid for given offset
*
* @param $index offset to get pageid for
* @return int pageid for given offset
*/
function getpageidbyoffset($index)
{
if (!isset($this->_pagedata)) {
$this->_generatepagedata();
}

if (($index % $this->_perpage) > 0) {
$pageid = ceil((float)$index / (float)$this->_perpage);
} else {
$pageid = $index / $this->_perpage;
}

return $pageid;
}

/**
* returns offsets for given pageid. eg, if you
* pass it pageid one and your perpage limit is 10
* it will return you 1 and 10. pageid of 2 would
* give you 11 and 20.
*
* @params pageid pageid to get offsets for
* @return array first and last offsets
*/
function getoffsetbypageid($pageid = null)
{
$pageid = isset($pageid) ? $pageid : $this->_currentpage;
if (!isset($this->_pagedata)) {
$this->_generatepagedata();
}

if (isset($this->_pagedata[$pageid])) {
return array(($this->_perpage * ($pageid - 1)) + 1, min($this->_totalitems, $this->_perpage * $pageid));
} else {
return array(0,0);
}
}

/**
* returns back/next and page links
*
* @param $back_html html to put inside the back link
* @param $next_html html to put inside the next link
* @return array back/pages/next links
*/
function getlinks($back_html = '<< back', $next_html = 'next >>')
{
$url  = $this->_getlinksurl();
$back = $this->_getbacklink($url, $back_html);
$pages = $this->_getpagelinks($url);
$next = $this->_getnextlink($url, $next_html);

return array($back, $pages, $next, 'back' => $back, 'pages' => $pages, 'next' => $next);
}

/**
* returns number of pages
*
* @return int number of pages
*/
function numpages()
{
return $this->_totalpages;
}

/**
* returns whether current page is first page
*
* @return bool first page or not
*/
function isfirstpage()
{
return ($this->_currentpage == 1);
}

/**
* returns whether current page is last page
*
* @return bool last page or not
*/
function islastpage()
{
return ($this->_currentpage == $this->_totalpages);
}

/**
* returns whether last page is complete
*
* @return bool last age complete or not
*/
function islastpagecomplete()
{
return !($this->_totalitems % $this->_perpage);
}

/**
* calculates all page data
*/
function _generatepagedata()
{
$this->_totalitems = count($this->_itemdata);
$this->_totalpages = ceil((float)$this->_totalitems / (float)$this->_perpage);
$i = 1;
if (!empty($this->_itemdata)) {
foreach ($this->_itemdata as $value) {
$this->_pagedata[$i][] = $value;
if (count($this->_pagedata[$i]) >= $this->_perpage) {
$i++;
}
}
} else {
$this->_pagedata = array();
}
}

/**
* returns the correct link for the back/pages/next links
*
* @return string url
*/
function _getlinksurl()
{
global $http_server_vars;

// sort out query string to prevent messy urls
$querystring = array();
if (!empty($http_server_vars['query_string'])) {
$qs = explode('&', $http_server_vars['query_string']);
for ($i = 0, $cnt = count($qs); $i < $cnt; $i++) {
list($name, $value) = explode('=', $qs[$i]);
if ($name != 'pageid') {
$qs[$name] = $value;
}
unset($qs[$i]);
}
}
if(is_array($qs)){
foreach ($qs as $name => $value) {
$querystring[] = $name . '=' . $value;
}
}
return $http_server_vars['script_name'] . '?' . implode('&', $querystring) . (!empty($querystring) ? '&' : '') . 'pageid=';
}

/**
* returns back link
*
* @param $url url to use in the link
* @param 上一篇      目录      下一篇 html to use as the link
* @return string the link
*/
function _getbacklink($url, 上一篇      目录      下一篇 = '<< back')
{
// back link
if ($this->_currentpage > 1) {
$back = '<a href="' . $url . ($this->_currentpage - 1) . '">' . 上一篇      目录      下一篇 . '</a>';
} else {
$back = '';
}

return $back;
}

/**
* returns pages link
*
* @param $url url to use in the link
* @return string links
*/
function _getpagelinks($url)
{
// create the range
$params['itemdata'] = range(1, max(1, $this->_totalpages));
$pager =& new pager($params);
上一篇      目录      下一篇s = $pager->getpagedata($pager->getpageidbyoffset($this->_currentpage));

for ($i=0; $i<count(上一篇      目录      下一篇s); $i++) {
if (上一篇      目录      下一篇s[$i] != $this->_currentpage) {
上一篇      目录      下一篇s[$i] = '<a href="' . $this->_getlinksurl() . 上一篇      目录      下一篇s[$i] . '">' . 上一篇      目录      下一篇s[$i] . '</a>';
}
}

return implode(' ', 上一篇      目录      下一篇s);
}

/**
* returns next link
*
* @param $url url to use in the link
* @param 上一篇      目录      下一篇 html to use as the link
* @return string the link
*/
function _getnextlink($url, 上一篇      目录      下一篇 = 'next >>')
{
if ($this->_currentpage < $this->_totalpages) {
$next = '<a href="' . $url . ($this->_currentpage + 1) . '">' . 上一篇      目录      下一篇 . '</a>';
} else {
$next = '';
}

return $next;
}
}

?>

 本文Tags分页  
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:一个分页导航类示例
· 下一篇:实战PHP/GTK
· PEAR简介:用PEAR来写你的下一个php程序
· MySQL用于PHP的库, 对数据库进行操作
· 用PHP制作饼图调查表
· DBA函数库
· 如何用PHP做到即时简繁切换


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077