|
本帖最后由 skeay 于 2011-3-3 15:00 编辑
<?php
class page
{
public $page;
public $num;
public $result;
public $tot;
public $total;
public $pagenum;
public $offset;
public $info;
public $it;
public $show;
public $style;
public $property;
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_conn()
# {
# $this->conn=mysql_connect("localhost","root","root");
# mysql_select_db("test");
# }
public function mysql_total() //计算出表中记录的总数
{
//$this->mysql_conn();
$this->result=mysql_query("select COUNT(*) from ".$this->table);
$this->tot=mysql_fetch_array($this->result);
$this->total=$this->tot['0'];
$this->pagenum=ceil($this->total/$this->num);
//echo "mysql_total".$this->pagenum;
}
public function mysql_show($prop) //全部显示出哪些字段
{
$this->mysql_total();
$this->property=$prop;
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->it=mysql_fetch_array($this->info))
{
echo $this->it[$this->property]."<br/>";
}
$this->mysql_page();
}
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(2);
$test->mysql_show("name");
?> |
|