PHP清晰好用的分页类
<?php
class page
{
public $page; //当前页
public $num; //每页显示记录数
public $result; //
public $tot;
public $total;
public $pagenum;
public $offset; //kaishi
public $info;
public $final_res;
public $show;
public $fields;
public $data;
public function set_page() //设置第几页
{
$this->page=isset($_GET['page'])?intval($_GET['page']):1;
}
public function set_table($tab) //设置哪张表
{
$this->table=$tab;
$this->set_page();
}
public function set_num($n) //每页显示多少条记录
{
$this->num=$n;
}
public function mysql_total() //计算出表中记录的总数
{
//$this->mysql_conn();
$this->tot=mysql_fetch_array(mysql_query("select COUNT(*) from ".$this->table));
$this->total=$this->tot['0'];
$this->pagenum=ceil($this->total/$this->num);
//echo "mysql_total".$this->pagenum;
}
public function mysql_show($fields) //全部显示出哪些字段
{
$this->mysql_total();
$this->fields=$fields;
if($this->page>$this->pagenum || $this->page == 0){
echo "Error : Can Not Found The page.";
exit;
}
$this->offset=($this->page-1)*$this->num;
$this->info=mysql_query("select * from ".$this->table." limit ".$this->offset.",".$this->num);
while($this->final_res=mysql_fetch_array($this->info))
{
$this->data[]=$this->final_res; //表里的
// $this->data[]=$this->final_res[$this->fields]; //表里的
}
}
public function mysql_page() //页码
{
//global $search;
for($i=1;$i<=$this->pagenum;$i++){
if($i==1)
{echo " ";}
$this->show=($i!=$this->page)?"<a href='".$_SERVER['PHP_SELF']."?page=".$i."'>$i</a>":"<b>$i</b>";
echo $this->show." ";
}
}
}
?>
<?php
$conn=mysql_connect('localhost','root','root');
$db=mysql_select_db('test',$conn);
$test = new page();
$test->set_table("page"); //表名
$test->set_num(3);
$test->mysql_show("name,id"); //要显示的字段
//print_r($test->data);
echo '<br />';
foreach ($test->data as $value)
echo $value['name'].'---'.$value['id'].'<br />';
$test->mysql_page();
?>