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

PHP简易分页类

PHP中文网
Lepaskan: 2016-05-25 17:03:33
asal
1034 orang telah melayarinya

跳至

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

	}
Salin selepas log masuk


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();
Salin selepas log masuk


Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan