- 觉的分页经常用到,所以就写出来,方便开发时查阅。
- <?php
class Page_class
{
public $host="localhost";
public $root="root";
public $pass="";
public $dbname;
public $tbname;
public $page_size = "10";
public $total_pages;
public $total_rows;
function Page_class($page_size,$tbname,$dbname)
{
$this->page_size = $page_size;
$this->tbname = $tbname;
$this->dbname = $dbname;
$this->connect();
$this->total_rows();
}
function connect()
{
$link = mysql_connect($this->host,$this->root,$this->pass)or die(mysql_error());
mysql_query("set names utf8");
mysql_select_db($this->dbname,$link);
}
function total_rows()
{
$sql = "select * from ".$this->tbname."";
$res = mysql_query($sql)or die("<br>SQL:".mysql_error());
$i = 0;
while($row = mysql_fetch_array($res))
{
$i++;
}
return $this->total_rows = $i;
}
function Divide_page($page_id)
{
if($page_id =='')
{
$page_id = 1;
}
$start = ($page_id-1)*$this->page_size;
$sql = "select * from ".$this->tbname." limit $start,$this->page_size";
$res = mysql_query($sql)or die(mysql_error());
$i = 0;
while($row = mysql_fetch_row($res))
{
$value[$i] = $row;
$i++;
}
return $value;
}
function Display_link_page($page_id)
{
$this->total_pages = ceil($this->total_rows/$this->page_size);
$key = 1;
echo "<table width = '779' align = 'center'>
<div align = 'center'>";
echo "<br><br>";
$this->Up_page($page_id);
while($key<$this->total_pages)
{
echo "<a href=browse_tb.php?id=".$key.">[$key] </a>";
$key++;
}
$this->Down_page($page_id);
echo "</div></table>";
//return $this->total_pages;
}
function Up_page($page_id)
{
$page = $page_id;
if($page =='')
{
$page = 1;
}
if($page>1)
{
$page = $page-1;
echo "<a href=browse_tb.php?id=".$page.">上一页 </a>";
}
}
function Down_page($page_id)
{
$page = $page_id;
if($page =='')
{
$page = 1;
}
if($page < $this->total_pages)
{
$page = $page+1;
echo "<a href=browse_tb.php?id=".$page.">下一页</a>";
}
}
}
?>
php分页类
最新推荐文章于 2021-04-11 13:10:43 发布