php通用分页种

php中文网
发布: 2016-06-13 11:03:08
原创
995人浏览过

php通用分页类

<?phpinterface ILink{	public function parse($page,$param);}?>
登录后复制

?

启科网络PHP商城系统
启科网络PHP商城系统

启科网络商城系统由启科网络技术开发团队完全自主开发,使用国内最流行高效的PHP程序语言,并用小巧的MySql作为数据库服务器,并且使用Smarty引擎来分离网站程序与前端设计代码,让建立的网站可以自由制作个性化的页面。 系统使用标签作为数据调用格式,网站前台开发人员只要简单学习系统标签功能和使用方法,将标签设置在制作的HTML模板中进行对网站数据、内容、信息等的调用,即可建设出美观、个性的网站。

启科网络PHP商城系统 0
查看详情 启科网络PHP商城系统
<?phprequire'ILink.php';class LinkAdapter implements ILink{	/**	 * @param unknown_type $page	 * @param unknown_type $param	 */	public function parse($page, $param) 	{				$temp="共{$page->getAllPage()}页,第{$page->getCurrentPage()}页 ";		$links=$this->getLinkString($param);		if($page->hasPrevious())$temp.="<a href='$links=".($page->getCurrentPage()-1)."'>上一页</a> ";else{$temp.="上一页 ";}		for($i=$page->getCurrentPage();$i<=$page->getAllPage()&&$i<=$page->getPerRecords();$i++)		{			$temp.="<a href='$links=$i'>{$i}</a> ";		}		if($page->hasNext())$temp.="<a href='$links=".($page->getCurrentPage()+1)."'>下一页</a> ";else{$temp.="下一页 ";}		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;	}}?>
登录后复制

??

<?phpclass 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;	}}?>
登录后复制

?

<?phprequire'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速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号