_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 .= ''; } else { $page .= ''; } $page .= "{$i}"; $page .= ''; } $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('?', $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['page']) ? intval($_get['page']) : 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['request_uri']); $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();
