搜索
首页php教程php手册PHP自适应分页代码,可以灵活定制CSS样式和分页链接效果!

原创! 无 ?php/** * 可以灵活定制的分页类 * * 可以定制的选项包括,分页链接显示效果,当前页码链接按钮的样式,URL中获取分页值的名字,可以随意带自己的参数 * * 使用方法: * 1、初始化类的时候需要传入参数,类型为数组。 * array( * (必填)'totalRows'

原创!
<?php
/**
 * 可以灵活定制的分页类
 * 
 * 可以定制的选项包括,分页链接显示效果,当前页码链接按钮的样式,URL中获取分页值的名字,可以随意带自己的参数
 * 
 * 使用方法:
 * 1、初始化类的时候需要传入参数,类型为数组。
 * array(
 * 	(必填)'totalRows'=>'100', 需要显示的数据的总条数;
 * 	(必填)'pageSize'=>'2', 每页需要显示的代码数量;
 * 	(必填)'currentPage'=>$_GET['p'], 当前页码,默认可以通过$_GET['p']获取,其中名字p可以定制
 * 	(必填)'baseUrl'=>'/welcome?id=3',你当前页面的链接地址,比如为http://www.xxx.com/test.php(或者/test.php),如果后面带有参数则可以为http://www.xxx.com/test?id=8
 * 	(选填,默认为3)'offset'=>'3', 当前页码的左右偏移量,比如当前页码为5,则在5的左右各显示几个数字链接,默认为3个,则效果为2,3,4,5,6,7,8
 * 	(选填,默认为p)'pageString'=>'p',通过$_GET['p']获取当前页码时候的名字,默认为p
 * 	(选填,默认为here)'className'=>'here',当前页码链接按钮的样式,默认样式名为here,所以你可以这样写css样式.here{background:#FF4500;} )
 * 
 * 2、可以使用的方法。
 *  A、初始化类后,需要调用pagination([$style = '1'][,$output=TRUE])方法产生分页链接
 *  关于参数的说明:
 *  @param $style (默认为 1,可不填写) :获取链接全部组件,即 首页+上一页+数字链接+下一页+尾页
 *  @param $style == 2 :仅获取数字链接
 *  @param $style == 3 :仅获取上一页+下一页
 *  @param $style == 4 :仅获取上一页+数字链接+下一页,(不包含首尾页)
 *  
 *  @param $output (默认为TRUE),返回分页链接
 *  @param $output 为FALSE时,直接输出分页链接
 *  
 *  B、getCurrentPage()获取当前页码,经过真伪判断后的,防止用户自行输入错误,比如http://www.xxx.com/test?p=-100;此时通过此方法获取当前页码为1
 *  
 *  C、pageAmount()获取总的页码数量
 *  
 * @author 星空幻颖
 * @link http://blog.sina.com.cn/yanyinghq
 *
 */
class Page
{
	private $pageSize; //您的网站每一页显示的列表条数
	private $totalRows; //通过数据库查询返回的总的记录条数
	private $url; //基准URL
	private $pageAmount; //页码的总数
	private $currentPage; //当前的页码
	private $offset = 4; //页码偏移量
	private $pageString = 'p'; //页码在URL中的名字
	private $classHere = 'class="here"'; //当前页链接的class样式类名,默认为here
	
	//初始化当前页码,记录总条数,每页多少条记录
	public function __construct($param)
	{
		$this->pageSize = $param['pageSize'];
		$this->totalRows = $param['totalRows'];
		$this->url = $param['baseUrl'];
		$this->offset = !empty($param['offset'])?$param['offset']:$this->offset;
		$this->pageString =  !empty($param['pageString'])?$param['pageString']:$this->pageString;
		$this->classHere = !empty($param['className'])?$param['className']:$this->classHere;
		$this->currentPage = (int)$param['currentPage'];
	}
	
	/**
	 * 创建分页链接
	 * 
	 * @param $style 默认为 1 :获取链接全部组件
	 * @param $style == 2 :仅获取数字链接
	 * @param $style == 3 :仅获取上一页,下一页
	 * @param $style == 4 :仅获取上一页、下一页、数字链接,不包含首尾页
	 * 
	 * @param $output 为TRUE时,返回分页链接
	 * @param $output 为FALSE时,直接输出分页链接
	 * 
	 */
	public function pagination($style = '1',$output=TRUE)
	{
		$this->baseUrl();
		$this->pageAmount();
		$this->currentPage();
			
		//获取全部组件
		if($style == '1')
		{
			$page = $this->indexPage().$this->prevPage().$this->pageNumber().$this->nextPage().$this->endPage();
		}
		else if($style == '2')
		{
			//获取纯数字链接
			$page = $this->pageNumber();
		}
		else if($style == '3')
		{
			//只获取上一页下一页
			$page = $this->prevPage().$this->nextPage();
		}
		else if($style =='4')
		{
			//上一页、下一页、数字链接
			$page = $this->prevPage().$this->pageNumber().$this->nextPage();
		}
		
		if($output)
		{
			return $page;
		}
		else
		{
			echo $page;
		}
	}
	
	/**
	 * 获取当前页码
	 * 
	 * @return 当前页码,经过真伪判断的
	 */
	public function getCurrentPage()
	{
		$this->pageAmount();
		$this->currentPage();
		return $this->currentPage;
	}
	
	/**
	 * 计算出所有的页数
	 * 
	 * 可以类外面直接调用此方法返回页码总数
	 * 
	 * @return 页码的总数
	 */
	public function pageAmount()
	{
		$this->pageAmount = ceil( $this->totalRows / $this->pageSize);
		if($this->pageAmount <= 0)
		{
			$this->pageAmount = '1';
		}
		return $this->pageAmount;
	}
	
	/**
	 * 判断基准链接是否携带参数
	 * 
	 * 基准链接为用户提交当前页码链接
	 * 
	 * 如果携带参数,则在链接之后加&p=
	 * 
	 * 如果不携带参数,则直接加?p=
	 */
	private function baseUrl()
	{
		if(preg_match('/\?/', $this->url))
		{
			$this->url = $this->url.'&'.$this->pageString.'=';
		}
		else
		{
			$this->url = $this->url.'?'.$this->pageString.'=';
		}
	}
	
	/**
	 * 验证当前页码的真伪性
	 * 
	 * 如果当前页码小于1或者没有,则默认当前页码为1
	 * 
	 * 如果当前页码大于页码总数,则默认当前页码为页码总数
	 * 
	 */
	private function currentPage()
	{
		if($this->currentPage < 1 || !$this->currentPage)
		{
			$this->currentPage = 1;
		}
		else if(($this->currentPage > $this->pageAmount))
		{
			$this->currentPage = $this->pageAmount;
		}
	}
	
	/**
	 * 首页链接
	 */ 
	private function indexPage()
	{
		if($this->currentPage == 1) return;
		return '<a href="'.$this->url.'1">首页</a>';
	}
	
	/**
	 * 尾页链接
	 */
	private function endPage()
	{
		if($this->currentPage == $this->pageAmount) return;
		return '<a href="'.$this->url.$this->pageAmount.'">尾页</a>';
	}
	
	/**
	 * 上一页
	 */
	private function prevPage()
	{
		if($this->currentPage == 1) return;
		return '<a href="'.$this->url.( $this->currentPage - 1 ).'">上一页</a>';
	}
	
	/**
	 * 下一页
	 */
	private function nextPage()
	{
		if($this->currentPage == $this->pageAmount) return;
		return '<a href="'.$this->url.( $this->currentPage + 1 ).'">下一页</a>';
	}
	
	/**
	 * 中间页码的链接
	 * 
	 */
	private function pageNumber()
	{
		$left ="";
		$right = "";
		
		//如果总记录的条数“大于”所有链接的数量时候
		if($this->pageAmount > ($this->offset * 2 + 1))
		{
			//当前页码距离首页的距离
			$leftNum = $this->currentPage - 1;
			
			//当前页码距离尾页的距离
			$rightNum = $this->pageAmount - $this->currentPage;
			
			//当当前页码距离首页距离不足偏移量offset时候,在右边补齐缺少的小方块
			if( $leftNum < $this->offset)
			{
				//左边的链接
				for($i = $leftNum; $i >= 1 ; $i--)
				{
					$left .= '<a href="'.$this->url.( $this->currentPage - $i ).'">'.( $this->currentPage - $i ).'</a>';
				}
				
				//右边的链接
				for($j = 1; $j <= ($this->offset * 2 - $leftNum); $j++)
				{
					$right .= '<a href="'.$this->url.( $this->currentPage + $j ).'">'.( $this->currentPage + $j ).'</a>';
				}
			}
			else if($rightNum < $this->offset)
			{
				//左边的链接
				for($i = ($this->offset * 2 - $rightNum); $i >= 1 ; $i--)
				{
					$left .= '<a href="'.$this->url.( $this->currentPage - $i ).'">'.( $this->currentPage - $i ).'</a>';
				}
				
				//右边的链接
				for($j = 1; $j <= $rightNum; $j++)
				{
					$right .= '<a href="'.$this->url.( $this->currentPage + $j ).'">'.( $this->currentPage + $j ).'</a>';
				}
			}
			else
			{
				//当前链接左边的链接
				for($i = $this->offset; $i >= 1 ; $i--)
				{
					$left .= '<a href="'.$this->url.( $this->currentPage - $i ).'">'.( $this->currentPage - $i ).'</a>'; 
				}
				
				//当前链接右边的链接
				for($j = 1; $j <= $this->offset; $j++)
				{
					$right .= '<a href="'.$this->url.( $this->currentPage + $j ).'">'.( $this->currentPage + $j ).'</a>';
				}
			}

			return $left.'<a href="'.$this->url.$this->currentPage.'" class="here">'.$this->currentPage.'</a>'.$right;
		}
		else
		{
			$allLink='';
			//当页码总数小于需要显示的链接数量时候,则全部显示出来
			for($j = 1; $j <= $this->pageAmount; $j++)
			{
				 $allLink.='<a href="'.$this->url.$j.'" '.($j == $this->currentPage?$this->classHere:'').'>'.$j.'</a>';
			}
			return $allLink;
		}
	}

}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
li{width:100%; overflow:hidden; margin-top:20px; list-style:none;}
a{display:block; height:30px; min-width:30px; text-align:center; font-size:14px; border:1px solid #d6d6d6; float:left; margin-left:10px; padding:3px 5px; line-height:30px; text-decoration:none; color:#666;}
a:hover,a.here{background:#FF4500; border-color:#FF4500; color:#FFF;}

</style>
</head>

<body>

<?php
require_once('/page.php');

$param = array('totalRows'=>'100','pageSize'=>'2','currentPage'=>@$_GET['p'],'baseUrl'=>'/page_index.php?id=3');


$page1 = new Page($param);
$page2 = new Page($param);
$page3 = new Page($param);
$page4 = new Page($param);
$page5 = new Page($param);

echo '总记录数:100';
echo '<hr />';
echo '每页记录2条<hr/ >';
echo '当前页码:'.$page1->getCurrentPage().'<hr />';
echo '共计'.$page1->pageAmount().'页<hr />';
echo '<li>'.$page1->pagination().'</li>';
echo '<li>'.$page2->pagination('1').'</li>'; //默认为1,所以和不填写效果一样
echo '<li>'.$page3->pagination('2').'</li>';
echo '<li>'.$page4->pagination('3').'</li>';
echo '<li>'.$page5->pagination('4').'</li>';
?>
</body>
</html>
PHP自适应分页代码,可以灵活定制CSS样式和分页链接效果!
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

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.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

禅工作室 13.0.1

禅工作室 13.0.1

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