Home > php教程 > PHP源码 > body text

PHP简易分页类

PHP中文网
Release: 2016-05-25 17:03:33
Original
1010 people have browsed it

跳至

_page = $page;
				$this->_totalcount = $totalcount;
				$this->_url = $url;
				$this->_pagesize = $pagesize;	
			} else {
				throw new Exception("构造函数参数不正确~");
			}
		}

		/**
		 * 分页条依赖于bootstrap,格式如下
		 *
  		*		
		*«
		*			1
		*			»
  		*		
		*
		*/
		function page(){
			$page = '';
			$page .= '';
			$page .= "«";
			$b = $this->_page - floor(self::size / 2);
			$e = $this->_page + floor(self::size / 2);
			if($b < 1){
				$b = 1;
				$e = ($this->getTotalPage() < self::size) ? $this->getTotalPage() : self::size;
			}
			if($e > $this->getTotalPage()){
				$b = ($this->getTotalPage() > self::size) ? ($this->getTotalPage() - self::size) : 1;
				$e = $this->getTotalPage();
			}
			for($i = $b; $i _page){
					$page .= &#39;&#39;;
				} else {
					$page .= &#39;&#39;;
				}
				$page .= "{$i}";
				$page .= &#39;&#39;;
			}
			$page .= "»";
			$page .= "";
			$page .= "";
			return $page;
		}

		function limit(){
			return  "limit {$this->getBegin()},{$this->_pagesize}";
		}

		function getBegin(){
			if($this->_page > 0 && $this->_page getTotalPage()){
				return ($this->_page - 1) * $this->_pagesize;
			} else {
				throw new Exception("当前页码不正确~");
			}
		}

		// function getEnd(){
		// 	if($this->_page > 0 && $this->_page getTotalPage()){
		// 		return ($this->_page == $this->getTotalPage()) ? ($this->_totalcount - $this->getBegin()) : ($this->_page * $this->_pagesize);
		// 	} else {
		// 		throw new Exception("当前页码不正确~");
		// 	}
		// }

		function getTotalPage(){
			if($this->_totalcount > 0){
				return ceil($this->_totalcount / $this->_pagesize) ;
			} else {
				throw new Exception("总记录数不正确~");
			}
		}

		private function _getUrl(){
			$url = $this->_url;
			$arr = explode(&#39;?&#39;, $this->_url);
			if(count($arr) > 1){
				$url = $url . "&";
			} else {
				$url = $url . "?";
			}
			$url = preg_replace("/&?page=[0-9]+/", "", $url);
			return str_replace("?&", "?", $url);
		}

	}
Copy after login


2. [代码]调用

$currentpage = isset($_GET[&#39;page&#39;]) ? intval($_GET[&#39;page&#39;]) : 1;
		$mysqli = new mysqli("localhost", "root", "hjj", "php");

		if(mysqli_connect_errno()){
			echo "数据库连接失败";
			exit();
		}

		$mysqli->set_charset("utf8");

		$sql = "select id from message";
		$result = $mysqli->query($sql);

		$page = new Page($currentpage, $result->num_rows, $_SERVER[&#39;REQUEST_URI&#39;]);

		$result = $mysqli->query("select * from message {$page->limit()}", MYSQLI_USE_RESULT);
		$arr = array();
		while($row = $result->fetch_assoc()){
			$arr[] = $row;
		}

		$smarty->assign("list", $arr);
		$smarty->assign("pagehtml", $page->page());
		$smarty->assign("page", $currentpage);
		$smarty->display("index.tpl");
		$mysqli->close();
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template