首頁 > php教程 > PHP源码 > PHP简易分页类

PHP简易分页类

PHP中文网
發布: 2016-05-25 17:03:33
原創
1043 人瀏覽過

跳至

_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);
		}

	}
登入後複製


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();
登入後複製


相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板