php通用分页种

Jun 13, 2016 pm 01:03 PM
function gt page param this

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-&gt;getCurrentPage()}页 ";
		$links=$this-&gt;getLinkString($param);
		if($page-&gt;hasPrevious())$temp.="<a href="%24links=%22.(%24page-&gt;getCurrentPage()-1).%22">上一页</a> ";else{$temp.="上一页 ";}
		for($i=$page-&gt;getCurrentPage();$igetAllPage()&amp;&amp;$igetPerRecords();$i++)
		{
			$temp.="<a href="%24links=%24i">{$i}</a> ";
		}
		if($page-&gt;hasNext())$temp.="<a href="%24links=%22.(%24page-&gt;getCurrentPage()+1).%22">下一页</a> ";else{$temp.="下一页 ";}
		return $temp;
	}
	public function getLinkString($param)
	{
		$str="";
		$attr=$_GET;
		unset($attr[$param]);
		if($attr)
		{
			foreach($attr as $key=&gt;$val)
			{
				if($str=="")
				{
					$str.="?$key=$val";
				}
				else
				{
					$str.="&amp;$key=$val";
				}
			}
			$str.="&amp;$param";
		}
		else
		{
			$str.="?$param";
		}
		return $str;
	}
}
?&gt;
로그인 후 복사

??

<?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-&gt;allRecords;
	}

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

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

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

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

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

	/**
	 * @param $currentPage the $currentPage to set
	 */
	public function setCurrentPage($currentPage) 
	{
		if ($currentPage currentPage = 1;
		else if ($currentPage &gt; $this-&gt;allPage)
			$this-&gt;currentPage =$this-&gt;allPage;
		else
			$this-&gt;currentPage=$currentPage;
	}
	public function hasNext() 
	{
		return $this-&gt;currentPageallPage;
	}
	public function hasPrevious() 
	{
		return $this-&gt;currentPage&gt;1;
	}
	public function getEndIndex() 
	{
		return ((($this-&gt;currentPage-1)*$this-&gt;perRecords)+$this-&gt;perRecords)&gt;$this-&gt;allRecords?((($this-&gt;currentPage-1)*$this-&gt;perRecords)+$this-&gt;perRecords)-$this-&gt;allRecords:$this-&gt;perRecords;
	}
	public function getStartIndex() 
	{
		return ($this-&gt;currentPage-1)*$this-&gt;perRecords;
	}
}
?&gt;
로그인 후 복사

?

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

?

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

?下载

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

뜨거운 기사 태그

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

화웨이 GT3 Pro와 GT4의 차이점은 무엇입니까? 화웨이 GT3 Pro와 GT4의 차이점은 무엇입니까? Dec 29, 2023 pm 02:27 PM

화웨이 GT3 Pro와 GT4의 차이점은 무엇입니까?

기능은 무슨 뜻인가요? 기능은 무슨 뜻인가요? Aug 04, 2023 am 10:33 AM

기능은 무슨 뜻인가요?

수정: Windows 11에서 캡처 도구가 작동하지 않음 수정: Windows 11에서 캡처 도구가 작동하지 않음 Aug 24, 2023 am 09:48 AM

수정: Windows 11에서 캡처 도구가 작동하지 않음

iPhone에서 App Store 오류에 연결할 수 없는 문제를 해결하는 방법 iPhone에서 App Store 오류에 연결할 수 없는 문제를 해결하는 방법 Jul 29, 2023 am 08:22 AM

iPhone에서 App Store 오류에 연결할 수 없는 문제를 해결하는 방법

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决

Python에서 'enumerate()' 함수의 목적은 무엇입니까? Python에서 'enumerate()' 함수의 목적은 무엇입니까? Sep 01, 2023 am 11:29 AM

Python에서 'enumerate()' 함수의 목적은 무엇입니까?

Vue 프로젝트에서 데이터 페이징 및 디스플레이 최적화를 구현하는 방법 Vue 프로젝트에서 데이터 페이징 및 디스플레이 최적화를 구현하는 방법 Oct 15, 2023 am 09:27 AM

Vue 프로젝트에서 데이터 페이징 및 디스플레이 최적화를 구현하는 방법

MySQL.proc 테이블의 역할과 기능에 대한 자세한 설명 MySQL.proc 테이블의 역할과 기능에 대한 자세한 설명 Mar 16, 2024 am 09:03 AM

MySQL.proc 테이블의 역할과 기능에 대한 자세한 설명

See all articles