PHP简易分页类
好久没写php了,写个分页类玩玩,明天开始还是搞java的好,php不太适合。调用:
好久没写php了,写个分页类玩玩,明天开始还是搞java的好,php不太适合。
调用:
调用:
<?php
class Page{
private $_page;
private $_pagesize;
private $_totalpage;
private $_totalcount;
private $_begin;
private $_end;
private $_url;
const size = 5; // 建议设置成奇数
function __construct($page, $totalcount, $url, $pagesize = 5){
if(is_int($page) && is_int($totalcount) && is_int($pagesize)){
$this->_page = $page;
$this->_totalcount = $totalcount;
$this->_url = $url;
$this->_pagesize = $pagesize;
} else {
throw new Exception("构造函数参数不正确~");
}
}
/**
* 分页条依赖于bootstrap,格式如下
* <div class="pagination pagination-right">
* <ul>
* <li><a href="#">«</a></li>
* <li><a href="#">1</a></li>
* <li><a href="#">»</a></li>
* </ul>
* </div>
*/
function page(){
$page = '<div class="pagination pagination-centered">';
$page .= '<ul>';
$page .= "<li><a href=\"{$this->_getUrl()}page=1\">«</a></li>";
$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 <= $e; $i++){
if($i == $this->_page){
$page .= '<li class="disabled">';
} else {
$page .= '<li>';
}
$page .= "<a href=\"{$this->_getUrl()}page={$i}\">{$i}</a>";
$page .= '</li>';
}
$page .= "<li><a href=\"{$this->_getUrl()}page={$this->getTotalPage()}\">»</a></li>";
$page .= "</ul>";
$page .= "</div>";
return $page;
}
function limit(){
return "limit {$this->getBegin()},{$this->_pagesize}";
}
function getBegin(){
if($this->_page > 0 && $this->_page <= $this->getTotalPage()){
return ($this->_page - 1) * $this->_pagesize;
} else {
throw new Exception("当前页码不正确~");
}
}
// function getEnd(){
// if($this->_page > 0 && $this->_page <= $this->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. [代码]调用 跳至 [1] [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();
- 上一篇:爬小说站
- 下一篇:数组混合GBK、UTF-8 互相转换
精彩图集
精彩文章






