首页 > 代码库 > PHP面向对象(分页)
PHP面向对象(分页)
<?php
class page
{
var $table;
var $pagesize;
var $totalsize;
var $totalpage;
var $first;
var $page;
var $provepage;
var $nextpage;
function __construct($table,$pagesize)
{
$conn=mysql_connect("localhost","root","123456");
mysql_select_db("baonier");
$this->table=$table;
$this->pagesize=$pagesize;
$sql="select * from ".$this->table."";
$query=mysql_query($sql);
$num=mysql_num_rows($query);
$this->page=isset($_GET[‘page‘])?$_GET[‘page‘]:1;
$this->totalsize=$num;
$this->totalpage=ceil(($this->totalsize)/($this->pagesize));
$this->first=($this->pagesize)*($this->page-1);
$this->provepage=$this->page-1;
$this->nextpage=$this->page+1;
}
function get_rows()
{
$sqls="select * from ".$this->table." limit ".$this->first.",".$this->pagesize."";
$querys=mysql_query($sqls);
$dates=Array();
while($rows=mysql_fetch_array($querys))
{
$dates[]=$rows;
}
//print_r($dates);exit;
return $dates;
}
function get_page()
{
if($this->provepage<1){
$this->provepage=1;
}
if($this->nextpage>$this->totalpage){
$this->nextpage=$this->totalpage;
}
$div="<a href=http://www.mamicode.com/"?page=1\">首页</a>
<a href=http://www.mamicode.com/"?page=$this->provepage\">上一页</a>
<a href=http://www.mamicode.com/"?page=$this->nextpage\">下一页</a>
<a href=http://www.mamicode.com/"?page=$this->totalpage\">尾页</a>";
return $div;
}
}
?>
<style type="text/css">
.td{ text-align:center;padding-left:20px;padding-right:20px;}
</style>
<html>
<body>
<table style="background-color:#fcc">
<tr>
<td class="td">编号</td>
<td class="td">标题</td>
<td class="td">作者</td>
<td class="td">时间</td>
<td class="td">内容</td>
</tr>
<?php
include "page.php";
$page=new page("news",5);
$arr=$page->get_rows();
foreach ($arr as $rows)
{
?>
<tr style="background-color:#ffc">
<td class="td"><?php echo $rows[‘nid‘] ?></td>
<td class="td"><?php echo $rows[‘title‘] ?></td>
<td class="td"><?php echo $rows[‘author‘] ?></td>
<td class="td"><?php echo $rows[‘publishtime‘] ?></td>
<td class="td"><?php echo $rows[‘content‘] ?></td>
</tr>
<?php
}
?>
</table>
<table>
<tr>
<td style="width:250px;"></td>
<td><?php echo $page->get_page();?></td>
<td style="width:100px;"></td>
</tr>
</table>
</body>
</html>
PHP面向对象(分页)