PHP通用分页类

大多数程序都会用到分页这一功能,所以为了以后方便花了近一个小时封装了一个通用的PHP分页类。

<?php
	require_once "../PublicService/WebHelper.class.php";
?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试</title>
<style type="text/css">
#diva a{ width:10px;height:10px; padding:10px;}

</style>
</head>
<body>
<div>
<a href="/php1/UI/empMain.php?loginid=<?php echo $name ?>">返回首页</a>
</div>
<table border="1" bordercolor="green" >
<tr>
<td>ID号</td>
<td>登录名</td>
<td>姓名</td>
<td>邮箱</td>
<td>操作</td>
</tr>
<?php 
		//创建分页类的对象实例(获得总记录数的SQL语句,生成的连接地址,每页显示行数,翻页索引数)
		$fenYe=new FenYe("select count(id) from users","/php1/UI/emplist.php",10,10);
		//定制显示(这里可以设置超链接的文字)
		$fenYe->setPageup_txt("上翻页*");
		$fenYe->setNextpage_txt("下翻页*");
		$fenYe->setFirstpage_txt("第一页*");
		$fenYe->setEndpage_txt("最后一页*");
		//传入分页SQL语句(getStrat()为占位符,内部生成替换的索引)
		$fenYe->fenye("select * from users limit {$fenYe->getStart()},{$fenYe->getEnd()}");
		//获得分页数据(关联数组)
		$arr=$fenYe->getData();
		for($i=0;$i<count($arr);$i++){
			echo "<tr><td>{$arr[$i]["id"]}</td><td>{$arr[$i]["loginid"]}</td><td>{$arr[$i]["name"]}</td><td>{$arr[$i]["email"]}</td><td><a herf='#'>其他操作</a>||<a href='#'>其他操作</a></td>";
		}
?>
</table>
<br/>
<span>
<?php  
echo $fenYe->getFristpage(); //第一页
?>
<<<
<?php 
	//上翻页   分页连接1,2,3,4   下翻页
	echo $fenYe->getPageup()."<div id='diva'>".$fenYe->getNavigation_bar()."</div>".$fenYe->getNextpage();
?>
>>>
<?php 
//最后一页
 echo $fenYe->getEndpage()
 
 ?>
<?php 
	echo "{当前:{$fenYe->getPagenow()},共:{$fenYe->getPagecount()}页}";
?>
</span>
</body>
</html>

一下是分页源代码:

class FenYe{
			private $pagecount;//总页数
			private $pagesize=10;//每页显示的记录数
			private $pagenow=1;//当前页
			private $pageindexcount;//总索引数
			private $pageindex=1;//当前索引页
			private $pageindexsize=10;//每页显示索引数
			private $navigation_bar;//导航条
			private $data;//数据
			private $sqlhelper;//数据库连接
			private $pagenowid="pagenow";//当前页url参数名
			private $pageindexid="pageindex";//当前索引页url参数名
			private $data_sql;//查询数据的SQL语句
			private $start="@start";//SQL语句limit占位符
			private $end="@end";//SQL语句limit占位符
			private $url;//连接的地址
			private $pageup;//上翻页连接
			private $nextpage;//下翻页连接
			private $fristpage;//第一页
			private $endpage;//最后一页
			
			private $pageup_txt="上翻页";//上翻页显示文本
			private $nextpage_txt="下翻页";//下翻页显示文本
			private $firstpage_txt="第一页";//第一页显示文本
			private $endpage_txt="最后一页";//最后一页显示文本
			
			/**
	 * @return the $pageup_txt
	 */
	public function getPageup_txt() {
		return $this->pageup_txt;
	}

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

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

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

			/**
	 * @param field_type $pageup_txt
	 */
	public function setPageup_txt($pageup_txt) {
		$this->pageup_txt = $pageup_txt;
	}

			/**
	 * @param field_type $nextpage_txt
	 */
	public function setNextpage_txt($nextpage_txt) {
		$this->nextpage_txt = $nextpage_txt;
	}

			/**
	 * @param field_type $firstpage_txt
	 */
	public function setFirstpage_txt($firstpage_txt) {
		$this->firstpage_txt = $firstpage_txt;
	}

			/**
	 * @param field_type $endpage_txt
	 */
	public function setEndpage_txt($endpage_txt) {
		$this->endpage_txt = $endpage_txt;
	}

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

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

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

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

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

			/**
	 * @param field_type $url
	 */
	public function setUrl($url) {
		$this->url = $url;
	}

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

			/**
	 * @param field_type $data_sql
	 */
	public function setData_sql($data_sql) {
		$this->data_sql = $data_sql;
	}

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

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

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

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

			/**
	 * @param string $pagenowid
	 */
	public function setPagenowid($pagenowid) {
		$this->pagenowid = $pagenowid;
	}

			/**
	 * @param string $pageindexid
	 */
	public function setPageindexid($pageindexid) {
		$this->pageindexid = $pageindexid;
	}

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

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

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

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

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

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

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

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

			/**
	 * @param field_type $pagecount
	 */
	public function setPagecount($pagecount) {
		$this->pagecount = $pagecount;
	}

			/**
	 * @param field_type $pagesize
	 */
	public function setPagesize($pagesize) {
		$this->pagesize = $pagesize;
	}

			/**
	 * @param field_type $pagenow
	 */
	public function setPagenow($pagenow) {
		$this->pagenow = $pagenow;
	}

			/**
	 * @param field_type $pageindexcount
	 */
	public function setPageindexcount($pageindexcount) {
		$this->pageindexcount = $pageindexcount;
	}

			/**
	 * @param field_type $pageindex
	 */
	public function setPageindex($pageindex) {
		$this->pageindex = $pageindex;
	}

			/**
	 * @param field_type $pageindexsize
	 */
	public function setPageindexsize($pageindexsize) {
		$this->pageindexsize = $pageindexsize;
	}

			/**
	 * @param field_type $navigation_bar
	 */
	public function setNavigation_bar($navigation_bar) {
		$this->navigation_bar = $navigation_bar;
	}

			/**
	 * @param field_type $data
	 */
	public function setData($data) {
		$this->data = $data;
	}

	public function __construct($sql,$url,$pagesize,$pageindexsize){
		$this->pagesize=$pagesize;
		$this->pageindexsize=$pageindexsize;
		$this->sqlhelper=new SqlHelper();
		$this->url=$url;
		$pagecount=$this->sqlhelper->Execute_DQL_Array($sql);
		//总页数
		$this->pagecount=ceil($pagecount[0][0]/$this->pagesize);
		//总索引数
		$this->pageindexcount=ceil($this->pagecount/$this->pageindexsize);
	}
	
	public function fenye($data_sql){
		$this->data_sql=$data_sql;
		//当前显示的页数
		if(!empty($_GET[$this->pagenowid])){
			$this->pagenow=$_GET[$this->pagenowid];
		}
		//当前显示索引
		if(!empty($_GET[$this->pageindexid])){
			$this->pageindex=$_GET[$this->pageindexid];
		}
		
		//生成查询语句
		$this->data_sql=str_replace($this->start, ($this->pagenow-1)*$this->pagesize, $this->data_sql);
		$this->data_sql=str_replace($this->end, $this->pagesize, $this->data_sql);
		
		//获得查询数据
		$this->data=$this->sqlhelper->Execute_DQL($this->data_sql);
		
		$index=$this->pageindexcount<=1?2:$this->pageindex+$this->pageindexsize;
		
		//生成导航条
		for($j=$this->pageindex;$j<$index;$j++){
			$this->navigation_bar.="<a href='{$this->url}?{$this->pagenowid}=$j&{$this->pageindexid}={$this->pageindex}'>$j</a>";
		}
		//生成上翻页与下翻页
		$pagetemp=$this->pageindex;
		if($this->pageindex>1){
			$this->pageindex=$this->pageindex-$this->pageindexsize;
		}
		if($pagetemp<$this->pageindexcount){
			$pagetemp=$pagetemp+$this->pageindexsize;
		}
		$this->pageup="<a href='{$this->url}?$this->pageindexid={$this->pageindex}'>{$this->pageup_txt}</a>";
		$this->nextpage="<a href='{$this->url}?$this->pageindexid={$pagetemp}'>{$this->nextpage_txt}</a>";
		//生成第一页与最后一页
		$this->fristpage="<a href='{$this->url}?{$this->pagenowid}=1'>{$this->firstpage_txt}</a>";
		$this->endpage="<a href='{$this->url}?{$this->pagenowid}={$this->pagecount}'>{$this->endpage_txt}</a>";
	}
			
			
			
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,关于PHP分页中的page通用分页函数split_page()的使用以及实现原理,我可以给您一些解释和建议。 首先,page通常是指当前页码,也就是用户当前所在的页数。在分页中,我们通常需要知道当前页码以及每一页所显示的数据条数,然后根据这两个参数计算出数据的起始位置,从而进行数据的分页显示。通用分页函数split_page()则是一个可以适用于不同情境下的分页函数,它可以根据传入的参数自动计算出数据的起始位置和页码等信息,并将分页链接和页码显示等工作封装起来,方便我们在不同的页面中调用和使用。 在使用分页函数时,我们需要传入的参数通常包括数据总条数、每页显示的条数、当前页码以及分页链接等信息。分页函数则会根据这些参数计算出数据的起始位置,并生成相应的分页链接和页码显示等内容。其中,分页链接通常包括上一页、下一页、首页和末页等链接,以及一些页码链接,方便用户进行点击选择。 至于具体的实现原理,分页函数通常会通过计算总页数和当前页的位置,然后根据当前页所在的位置生成相应的分页链接和页码显示。在生成分页链接时,我们可以使用PHP中的链接生成函数,将参数传入链接中,从而生成相应的链接地址和参数。在生成页码显示时,我们通常需要根据不同的情境进行不同的处理,比如在当前页码前后加上一些省略号或者直接显示完整的页码等。 总之,分页函数是一个比较常用的功能,它可以帮助我们在不同的页面中方便地进行数据的分页显示,从而提高用户体验和数据展示效果。如果您需要在自己的项目中使用分页函数,建议可以先了解一下PHP中的分页函数实现原理和使用方法,然后根据自己的需求进行相应的定制和调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值