搜索
首页后端开发php教程 php通用分页种
php通用分页种 Jun 13, 2016 pm 01:03 PM
functiongtpageparamthis

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()}页 ";
		$links=$this->getLinkString($param);
		if($page->hasPrevious())$temp.="<a href="%24links=%22.(%24page->getCurrentPage()-1).%22">上一页</a> ";else{$temp.="上一页 ";}
		for($i=$page->getCurrentPage();$igetAllPage()&&$igetPerRecords();$i++)
		{
			$temp.="<a href="%24links=%24i">{$i}</a> ";
		}
		if($page->hasNext())$temp.="<a href="%24links=%22.(%24page->getCurrentPage()+1).%22">下一页</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;
	}
}
?>

??

<?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 currentPage = 1;
		else if ($currentPage > $this->allPage)
			$this->currentPage =$this->allPage;
		else
			$this->currentPage=$currentPage;
	}
	public function hasNext() 
	{
		return $this->currentPageallPage;
	}
	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();
?>

?下载

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
华为GT3 Pro和GT4的差异是什么?华为GT3 Pro和GT4的差异是什么?Dec 29, 2023 pm 02:27 PM

许多用户在选择智能手表的时候都会选择的华为的品牌,其中华为GT3pro和GT4都是非常热门的选择,不少用户都很好奇华为GT3pro和GT4有什么区别,下面就就给大家介绍一下二者。华为GT3pro和GT4有什么区别一、外观GT4:46mm和41mm,材质是玻璃表镜+不锈钢机身+高分纤维后壳。GT3pro:46.6mm和42.9mm,材质是蓝宝石玻璃表镜+钛金属机身/陶瓷机身+陶瓷后壳二、健康GT4:采用最新的华为Truseen5.5+算法,结果会更加的精准。GT3pro:多了ECG心电图和血管及安

function是什么意思function是什么意思Aug 04, 2023 am 10:33 AM

function是函数的意思,是一段具有特定功能的可重复使用的代码块,是程序的基本组成单元之一,可以接受输入参数,执行特定的操作,并返回结果,其目的是封装一段可重复使用的代码,提高代码的可重用性和可维护性。

修复:截图工具在 Windows 11 中不起作用修复:截图工具在 Windows 11 中不起作用Aug 24, 2023 am 09:48 AM

为什么截图工具在Windows11上不起作用了解问题的根本原因有助于找到正确的解决方案。以下是截图工具可能无法正常工作的主要原因:对焦助手已打开:这可以防止截图工具打开。应用程序损坏:如果截图工具在启动时崩溃,则可能已损坏。过时的图形驱动程序:不兼容的驱动程序可能会干扰截图工具。来自其他应用程序的干扰:其他正在运行的应用程序可能与截图工具冲突。证书已过期:升级过程中的错误可能会导致此issu简单的解决方案这些适合大多数用户,不需要任何特殊的技术知识。1.更新窗口和Microsoft应用商店应用程

如何修复无法连接到iPhone上的App Store错误如何修复无法连接到iPhone上的App Store错误Jul 29, 2023 am 08:22 AM

第1部分:初始故障排除步骤检查苹果的系统状态:在深入研究复杂的解决方案之前,让我们从基础知识开始。问题可能不在于您的设备;苹果的服务器可能会关闭。访问Apple的系统状态页面,查看AppStore是否正常工作。如果有问题,您所能做的就是等待Apple修复它。检查您的互联网连接:确保您拥有稳定的互联网连接,因为“无法连接到AppStore”问题有时可归因于连接不良。尝试在Wi-Fi和移动数据之间切换或重置网络设置(“常规”>“重置”>“重置网络设置”>设置)。更新您的iOS版本:

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

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code<form name="myform"

MySQL.proc表的作用和功能详解MySQL.proc表的作用和功能详解Mar 16, 2024 am 09:03 AM

MySQL.proc表的作用和功能详解MySQL是一种流行的关系型数据库管理系统,开发者在使用MySQL时常常会涉及到存储过程(StoredProcedure)的创建和管理。而MySQL.proc表则是一个非常重要的系统表,它存储了数据库中所有的存储过程的相关信息,包括存储过程的名称、定义、参数等。在本文中,我们将详细解释MySQL.proc表的作用和功能

"enumerate()"函数在Python中的用途是什么?"enumerate()"函数在Python中的用途是什么?Sep 01, 2023 am 11:29 AM

在本文中,我们将了解enumerate()函数以及Python中“enumerate()”函数的用途。什么是enumerate()函数?Python的enumerate()函数接受数据集合作为参数并返回一个枚举对象。枚举对象以键值对的形式返回。key是每个item对应的索引,value是items。语法enumerate(iterable,start)参数iterable-传入的数据集合可以作为枚举对象返回,称为iterablestart-顾名思义,枚举对象的起始索引由start定义。如果我们忽

Vue项目中如何实现数据的分页和显示优化Vue项目中如何实现数据的分页和显示优化Oct 15, 2023 am 09:27 AM

Vue项目中实现数据的分页和显示优化在Vue项目中,当页面需要展示大量数据时,通常需要进行数据的分页和显示优化以提高用户体验,本文将介绍如何使用Vue实现数据的分页和显示优化,并提供具体的代码示例。一、数据分页数据分页是指将大量数据按照一定的规则分割成多页,并在页面上进行分页显示。Vue项目中可以使用如下步骤来实现数据分页:定义数据源首先,定义一个包含所有数

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前By尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),