php之分页种代码

WBOY
Release: 2016-06-13 12:59:52
Original
858 people have browsed it

php之分页类代码

/*
思路
1.把地址栏的URL获取
2.分析URL中的query部分--就是?后面传参数的部分
3.query部分分析成数组
4.把数组中的page单元,+1,-1,形成2个新的数组
5.再把新数组拼接成query部分,合成上一页,下一页连接地址

*/
//分页类
class Page {
	public $total;	//全部条数,从数据库取出
	public $prePage = 10; //每页的条数

	protected $curr= 1;	//默认当前页码

	public function __construct($total,$prePage='') {
		$this->total = $total; //把总条目信息放在total属性
		if ($prePage > 0) {
			$this->prePage = $prePage;	//把每页数量放在perPage属性
		}

		//计算当前页码
		if (isset($_GET['page']) && ($_GET['page'] + 0) > 0) {
			$this->curr = $_GET['page'] + 0;
		}
	}

	//主体函数
	public function showPage() {
		if ($this->total <=0) {
			return ''; //如果总条目<=0 直接返回空字符串
		}

		$cnt = ceil($this->total / $this->prePage); //算总页数,进一取整
	
		//根据当前页,算上一页,下一页
		/*
		分析url,有几种情况?
		xx.php
		xx.php?id=5
		xx.php?page=3
		xx.php?id=5&page=3
		*/
		
		//最终生成的URL里边必然有page=N
		$url = $_SERVER['REQUEST_URI'];
		$parse = parse_url($url); //把URL分析结果放在数组里
		//print_r($parse);
		//保证参数里边有page
		if (!isset($parse['query'])) {
			$parse['query'] = 'page=' .$this->curr;
		}
		//把query字符串分析成数组,再次确保有page选项
		parse_str($parse['query'],$parms);

		if (!array_key_exists('page', $parms)) {
			$parms['page'] = $this->curr;
		}

		//上边四种情况都测试一遍,page参数都能生成
		//print_r($parms);

		//判断除了page之外,还有没有其他参数

		if (count($parms) == 1) {
			$url = $parse['path'] . '?';
		} else {
			unset($parms['page']);
			$url = $parse['path'] . '?' . http_build_query($parms) . '&';
		}

		//echo $url
		$prev = $this->curr - 1;
		$next = $this->curr + 1;

		//首页
		$indexLink = '<a href="' . $url .'page=' . 1 . '">首页</a>';

		//上一页
		if ($prev < 1) {
			$prevLink = '';
		}else {
			$prevLink = '<a href="' . $url .'page=' . $prev . '">上一页</a>';
		}

		//下一页
		if ($next > $cnt) {
			$nextLink = '';
		}else {
			$nextLink = '<a href="' . $url .'page=' . $next . '">下一页</a>';
		}
		//尾页
		$lastLink = '<a href="' . $url .'page=' . $cnt . '">尾页</a>';

		//echo $indexLink.'  '.$prevLink.'  '.$nextLink .'  '.$lastLink;
		//上一页,1 2 3 4 5 下一页

		$start = $this->curr - (5-1)/2; //计算左侧开始的页码
		$end = $this->curr + (5-1)/2;	//计算右侧开始的页码
		
		//如果左侧的页面,已经小于1,则把小于1 的部分补到右侧
		if ($start < 1) {
			$end += (1 - $start);
			$start = 1; //修改start = 1
		
			if ($end > $cnt) {
				$end  = $cnt;
			}
		}

		//把右侧超出的部分,补到左边
		if ($end > $cnt) {
			$start -= ($end - $cnt);
			$end = $cnt;

			if ($start < 1) {
				$start = 1;
			}
		}
		

		//循环出页码数
		$pageStr = '';
		for ($i=$start; $i <= $end ; $i++) { 
				
			if ($i == $this->curr) {
				$pageStr .= $i;
				continue;
			}

			$pageStr .= '<a href="' . $url . 'page=' . $i . '">' . $i . '</a>';
		}
		return $indexLink.$prevLink.$pageStr.$nextLink.$lastLink;
	}



}


$page = new Page(30,3);
echo $page->showPage();
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template