`
mengdejun
  • 浏览: 405269 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

php通用分页类

    博客分类:
  • Php
PHP 
阅读更多
<?php
interface ILink
{
	public function parse($page,$param);
}
?>

 

<?php
require'ILink.php';
class LinkAdapter implements ILink
{
	/**
	 * @param unknown_type $page
	 * @param unknown_type $param
	 */
	public function parse($page, $param) 
	{
		
		$temp="共{$page->getAllPage()}页,第{$page->getCurrentPage()}页&nbsp;";
		$links=$this->getLinkString($param);
		if($page->hasPrevious())$temp.="<a href='$links=".($page->getCurrentPage()-1)."'>上一页</a>&nbsp;";else{$temp.="上一页&nbsp;";}
		for($i=$page->getCurrentPage();$i<=$page->getAllPage()&&$i<=$page->getPerRecords();$i++)
		{
			$temp.="<a href='$links=$i'>{$i}</a>&nbsp;";
		}
		if($page->hasNext())$temp.="<a href='$links=".($page->getCurrentPage()+1)."'>下一页</a>&nbsp;";else{$temp.="下一页&nbsp;";}
		return $temp;
	}
	public function getLinkString($param)
	{
		$str="";
		$attr=$_GET;
		unset($attr[$param]);
		if($attr)
		{
			foreach($attr as $key=>$val)
			{
				if($str=="")
				{
					$str.="?$key=$val";
				}
				else
				{
					$str.="&$key=$val";
				}
			}
			$str.="&$param";
		}
		else
		{
			$str.="?$param";
		}
		return $str;
	}
}
?>

  

<?php
class Page 
{
	private $allPage;#总页数
	private $allRecords;#总记录数
	private $perRecords;#单页记录数
	private $currentPage=1;#当前页面
	/**
	 * @return the $allPage
	 */
	public function getAllPage()
	{
		return $this->allPage;
	}
	/**
	 * @return the $allRecords
	 */
	public function getAllRecords() 
	{
		return $this->allRecords;
	}

	/**
	 * @return the $perRecords
	 */
	public function getPerRecords() {
		return $this->perRecords;
	}

	/**
	 * @return the $currentPage
	 */
	public function getCurrentPage() 
	{
		return $this->currentPage;
	}

	/**
	 * @param $allPage the $allPage to set
	 */
	public function setAllPage($allPage) 
	{
		$this->allPage = ($allPage%$this->perRecords == 0)?($allPage/$this->perRecords):($allPage/$this->perRecords+1);
		$this->allPage=intval($this->allPage);
	}

	/**
	 * @param $allRecords the $allRecords to set
	 */
	public function setAllRecords($allRecords) 
	{
		$this->allRecords = $allRecords;
	}

	/**
	 * @param $perRecords the $perRecords to set
	 */
	public function setPerRecords($perRecords) {
		$this->perRecords = $perRecords;
	}

	/**
	 * @param $currentPage the $currentPage to set
	 */
	public function setCurrentPage($currentPage) 
	{
		if ($currentPage < 1)
			$this->currentPage = 1;
		else if ($currentPage > $this->allPage)
			$this->currentPage =$this->allPage;
		else
			$this->currentPage=$currentPage;
	}
	public function hasNext() 
	{
		return $this->currentPage<$this->allPage;
	}
	public function hasPrevious() 
	{
		return $this->currentPage>1;
	}
	public function getEndIndex() 
	{
		return ((($this->currentPage-1)*$this->perRecords)+$this->perRecords)>$this->allRecords?((($this->currentPage-1)*$this->perRecords)+$this->perRecords)-$this->allRecords:$this->perRecords;
	}
	public function getStartIndex() 
	{
		return ($this->currentPage-1)*$this->perRecords;
	}
}
?>

 

<?php
require'Page.php';
require'LinkAdapter.php';
class Pager 
{
	private $list=array();
	private $page;#分页对象
	private $param;#页面请求参数
	public function __construct($list)
	{
		$this->list=$list;
		$this->page=new Page();
	}
	/**
	 * 
	 * @param unknown_type $rows 显示的数据量
	 * @param unknown_type $current 当前页
	 */
	public function init($rows=5,$current)
	{
		$this->page->setAllRecords(count($this->list));
		$this->page->setPerRecords($rows);
		$this->page->setAllPage(count($this->list));
		$this->page->setCurrentPage($current);
		
		$this->list=array_slice($this->list,$this->page->getStartIndex(),$this->page->getEndIndex());
	}
	/**
	 * 获取分页变量
	 */
	public function getVar()
	{
		return $this->list;
	}
	/**
	 * @return the $param
	 */
	public function getParam() 
	{
		return $this->param;
	}
	/**
	 * @param $param the $param to set
	 */
	public function setParam($param) {
		$this->param = $param;
	}
	/**
	 * 加载插件信息,获取生成的链接,装饰器模式
	 * @param unknown_type $link
	 */
	public function getLink($link=null)
	{
		if(!empty($link)||!(($link instanceof ILink)))$link=new LinkAdapter();
		return $link->parse($this->page,$this->param);
	}
}
?>

 

<?php
	include'lib/Pager.php';
	$target=array();
	for($i=0;$i<=100;$i++){$target[]=$i;}
	$page=new Pager($target);
	$page->setParam("page");
	$page->init(30,$_REQUEST['page']);
	$list=$page->getVar();
	foreach($list as $val):
		echo $val.'<br/>';
	endforeach;
	echo $page->getLink();
?>

 下载

分享到:
评论

相关推荐

    PHP通用分页类 v1.0-源码.zip

    PHP通用分页类是Web开发中常用的一种工具,它能够帮助开发者在大量数据查询结果上实现高效的分页显示。PHP的分页类通常包括参数处理、数据库查询优化、URL生成等核心功能,使得用户可以方便地浏览数据集的各个部分,...

    PHP通用分页类详解

    ### PHP通用分页类详解 #### 一、概述 在Web开发中,为了提升用户体验以及减轻服务器负担,分页功能的应用十分广泛。本篇将详细解析一个PHP通用分页类的实现逻辑及其应用场景。 #### 二、核心概念 1. **`$total`...

    一个很好的分页类php分页类

    标题中的"一个很好的分页类php分页类"暗示我们这里有一个专门用于实现分页功能的PHP类。这个类可能包含了一系列方法,用于计算总页数、生成页码链接以及处理用户请求的特定页面。 `page.class.php`可能是这个分页类...

    PHP通用分页类 v1.0

    写了个php的通用分页类使用方法很简单在实例化对象时只需要给他传个"每页显示记录数"和"总记录数"就可以了例:include_once("pages_class.php");连接db省略.....得到总记录数在此省略........

    PHP通用分页类page.php[仿google分页]

    PHP通用分页类Page.php是一个模仿Google分页样式的PHP类,它主要用于处理数据库查询或其他数据集的分页显示。这个类的设计目标是能够轻松地适应各种程序需求,只需要提供记录总数和每页显示的记录数即可。下面我们将...

    超强php分页打包 通用分页 万能分页 ajax分页 google分页

    本压缩包“超强php分页打包 通用分页 万能分页 ajax分页 google分页”提供了一系列的分页解决方案,包括了基本的PHP分页、通用的分页实现、以及结合AJAX技术的动态分页,旨在满足各种项目需求。下面将详细介绍这些...

    php分页类(通用)

    一个通用的PHP分页类可以帮助开发者快速实现这一功能,提高用户体验。下面我们将详细探讨PHP分页类的设计原理、实现方法以及`page.class.php`这个文件可能包含的关键知识点。 1. 分页类设计基础 - **属性**:分页...

    PHP通用分页程序

    **PHP通用分页程序** 在Web开发中,数据量大的时候,为了提高用户体验,通常会使用分页技术来展示信息。PHP是一种广泛使用的服务器端脚本语言,特别适合于Web开发,因此,PHP通用分页程序是开发人员常用的一种工具...

    PHP分页显示的方法分析【附PHP通用分页类】

    7. **通用分页类**: 提供了一个名为`Page`的类,用于创建分页对象和生成分页链接。类中包含了总条数、显示页数、当前页、每页显示条数等属性,以及生成页码数组和样式的方法。 这个实例中,`Page`类的调用方式...

    PHP通用分页类源代码

    写了个php的通用分页类 使用方法很简单 在实例化对象时只需要给他传个每页显示记录数和总记录数就可以了 例: include_once(pages_class.php); 连接db省略..... 得到总记录数在此省略........

    php实现数据分页的通用方法类.zip

    本压缩包文件“php实现数据分页的通用方法类.zip”提供了一个PHP类库,帮助开发者轻松实现这个功能。 首先,我们要理解什么是数据分页。数据分页是将数据库查询结果按一定的数量(如每页10条)进行分割,然后根据...

    php仿谷歌分页类.zip

    PHP通用分页类(仿Google样式)。本代码是用于分页用的,只需提供记录总数与每页显示数两个参数,无需指定URL,链接由程序生成。方便用于检索结果分页,表单采用GET方法提交,可保证在诸如查询之,删除之类的操作时,...

    小贤PHP通用分页程序 v1.0.rar

    我在一些PHP论坛上经常看到不少PHP初学者对于PHP分页很头痛,所以我就抽一点时间将我所写的网站中的 分页功能给分离出来给广大PHP初学者做为参考或者使用。 程序的优点:可用于伪静态、静态和动态分页用,...

    BluePage通用分页类助开发者提高开发效率1

    ### BluePage通用分页类详解 #### 一、引言 在现代Web开发中,分页功能是一项不可或缺的技术。无论是数据库查询结果的分页展示,还是长篇文章的分页阅读,都需要一个高效、灵活的分页解决方案。BluePage正是为了...

    通用PHP分页组件

    4. 轻松集成:由于其通用性,该组件可以无缝集成到任何PHP项目中,无论是基于MVC框架还是简单的PHP脚本,只需简单引入并调用相应的函数或类,即可实现分页功能。 5. 性能优化:一个好的分页组件还会考虑到性能,...

    php无敌分页 php多种分页类,php分页大全

    "php无敌分页 php多种分页类,php分页大全"这个主题涵盖了一系列用于实现高效、灵活和美观分页的方法和类库。下面我们将深入探讨PHP分页的相关知识点。 1. 基本分页概念: 分页是将大量的数据分为多个部分,每次只...

    PHP通用的数据库分页类

    本文将深入探讨如何使用PHP实现一个通用的数据库分页类,以适应不同的数据库查询需求。 首先,我们需要了解数据库分页的基本概念。分页是将大量结果集分为若干个较小的部分(页面),每次只加载一部分数据到用户...

Global site tag (gtag.js) - Google Analytics