php教程通用分页类代码
*/
class dividepage //分页类
{
private $page ; //当前页
private $total ; //总的记录数
private $pagesize; //每页的记录数
private $prepage ; //前一页
private $nextpage; //后一页
private $lastpage ; //最后一页
private $url; //url
private $start; //当页显示记录数开始条数
private $endp; //当页显示记录数结束条数
private $first; //查询开始数
//构造函数
public function __construct($total,$pagesize,$url)
{
$this->total = intval($total);
$this->pagesize = intval($pagesize);
$this->url = $url;
$this->init();
}
//初始化分页信息
public function init()
{
$parse_url = parse_url($this->url);
//print_r($parse_url);
$url_query = $parse_url['query'];
if(empty($url_query))
{
$this->page = 1;
$this->url = $this->url.'?page=';
}
else
{
preg_match("/(^|&)page=(.*)(&|$)/ui",$url_query,$temp);
$this->page = intval($temp[2]);
$this->url=preg_replace("/page=(.*)/","page=",$this->url);
}
$this->lastpage = ceil($this->total/$this->pagesize) ? ceil($this->total/$this->pagesize) :1 ;
//最后一页的页码,总数/每页数向上取整,如果总数为0,则最后一页为第一页.
$this->page = min($this->lastpage,$this->page)>0 ? min($this->lastpage,$this->page) :1 ;
//当前页大于最后一页,则取最后一页为当前页,不大于0,则取1为当前页.
$this->first = ($this->page - 1)*$this->pagesize ; //计算sql查询语句起始位置
$this->prepage = $this->page-1 ; //前一页
$this->nextpage = $this->page+1 ; //后一页
$this->start = $this->total?$this->first +1:0; //当页记录开始编号
$this->endp = min($this->total,$this->page*$this->pagesize);//当页记录结束编号
}
public function getpageinfo() //取得当前页基本信息,如:显示第 1-5 条记录,共 9 条记录。
{
return '<span class="pageinfostyle">显示第<span class = "numberstyle">'.$this->start.'-'.$this->endp.'</span>条记录,共<span class = "numberstyle">'.$this->total.'</span>条记录。</span>';
}
public function getpagenav()//取得分页导航如:首页 上一页 下一页 尾页
{
$nav = "";
if($this->lastpage == 1)
{
return $nav;
break;
}
$nav = '<a href ="'.$this->url.'1" class = "navpagestyle" >首页</a>';
if($this->prepage)
{
$nav.= '<a href = "'.$this->url.$this->prepage.'" class = "navpagestyle">上一页</a>';
}
else
{
$nav.= '<a class = "navpagestyle">上一页</a>';
}
if($this->nextpage <=$this->lastpage)
{
$nav.= '<a href = "'.$this->url.$this->nextpage.'" class = "navpagestyle">下一页</a> ';
}
else
{
$nav.= '<a class = "navpagestyle">下一页</a>';
}
$nav.= '<a href = "'.$this->url.$this->lastpage.'" class = "navpagestyle" >尾页</a>';
return $nav;
}
public function getjumpnav() //下拉跳转分页
{
$nav = '<span class = "pageinfostyle">到第<select name= "jumpnav" size="1" class = "jumpnav" onchange ='window.location = "'.$this->url.'"+this.value'>'."n";
for($i=1 ; $i<=$this->lastpage ; $i++)
{
if($i==$this->page)
{
$nav.= '<option value ="'.$i.'" selected>'.$i.'</option>'."n";
}
else
{
$nav.='<option value = "'.$i.'">'.$i.'</option>'."n";
}
}
$nav.='</select>页,共<span class= "numberstyle">'.$this->lastpage.'</span>页</span>';
return $nav;
}
public function getlimit()
{
return $this->first.",".$this->pagesize;
}
public function getallnav() //获取导航
{
return $this->getpageinfo().$this->getpagenav().$this->getjumpnav();
}
}
//使用实例:
$sql1 = mysql教程_query("select * from sun_message");
$total=mysql_num_rows($sql1);//查询到的数据的总条数
$pagesize = 5;//每页显示的记录条数
$url = $_server['request_uri'];//请求的uri
$pageclass = new dividepage($total, $pagesize,$url); //创建分页类,(类能自动初始化)
$limit =$pageclass->getlimit();//取得当前页要显示的记录开始序号和每页显示条数,如:0, 5(显示从0开始的5条记录)
echo $dividepageclass->getallpagenav();//显示所有分页导航条,如:显示第11-13条记录,共13条记录。首页 上一页 下一页 尾页 到第 1 页,共 3 页
$sql2 = "select * from sun_message order by mid desc limit {$limit}"; //分页内容输出
$result=mysql_query($sql);
while($row=mysql_fetch_array($result))
{
echo "<hr><b>".$row[title]." | ".$row[author];
}