标题: PHP分页,看到有些虎友需要
时间: 2014-02-08
select count(1) from table
select * from table limit $limit offset $limit*(1-1)
select * from table limit $limit offset $limit*(2-1)
select * from table limit $limit offset $limit*(3-1)
<?php
$total = select count(1) from table; //$total是你查出的总记录数
$limit = 10; //$limit是每页显示的条数
$LastPage = ceil($total/$limit); //$LastPage为尾页,也就是总页数函数ceil就是向上取整数,如:ceil(3.3) == 4 ceil(3.7) == 4
$pageno = $_GET['pageno'] == null ? 1 : $_GET['pageno']; //$pageno为当前的页数,默认为第1页
if($pageno > $LastPage) {
$pageno = $LastPage;
}
if($pageno < 1) {
$pageno = 1;
}
$messageArray = select * from table limit $limit offset $limit*($pageno-1); //这就是当前页数的数据
?>
<html>
<head><title>分页</title></head>
<body>
<?php
if($messageArray) {
foreach($messageArray as $message) {
//循环写出数据
}
} else {
echo '没有记录';
}
?>
<a href="message.php?pageno=1">首页</a>
<a href="message.php?pageno=<?php echo $pageno-1; ?>">上一页</a>
<a href="message.php?pageno=<?php echo $pageno+1; ?>">下一页</a>
<a href="message.php?pageno=<?php echo $LastPage; ?>">尾页</a>
</body>
</html>
<?php
class Page {
private $pageno;
private $total;
private $limit;
public function __construct() { }
/**
* 设置当前页数
* @param unknown $pageno
*/
public function setPageno($pageno) {
$this->pageno = $pageno;
}
/**
* 设置总记录数
* @param unknown $total
*/
public function setTotal($total) {
$this->total = $total;
}
/**
* 设置limit
* @param unknown $limit
*/
public function setLimit($limit) {
$this->limit = $limit;
}
/**
* 获取当前页码
*/
public function getPageno() {
return $this->pageno;
}
/**
* 获取总记录数
*/
public function getTotal() {
return $this->total;
}
/**
* 获取总页数
* @return number
*/
public function getPageNum() {
return ceil($this->total/$this->limit);
}
/**
* 获取第一页
* @return number
*/
public function getFirstPage() {
return 1;
}
/**
* 获取最后一页
*/
public function getLastPage() {
return $this->getPageNum();
}
/**
* 获取上一页
* @return number
*/
public function getPrePage() {
if($this->getPageno() > 1) {
return $this->getPageno() - 1;
} else {
return 1;
}
}
/**
* 获取下一页
* @return number
*/
public function getNextPage() {
if($this->getPageno() < $this->getPageNum()) {
return $this->getPageno() + 1;
} else {
return $this->getPageNum();
}
}
public function getLimit() {
return $this->limit;
}
public function getOffset() {
return $this->getLimit()*($this->getPageno()-1);
}
}
?>
$sql = "select * from table limit ".$page->getLimit()." offset ".$page->getOffset();
『回复列表(17|隐藏机器人聊天)』