欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP 翻页 实例代码

程序员文章站 2022-06-03 13:26:35
复制代码 代码如下:
复制代码 代码如下:

<?php
class page
{
private $pagesize;//每一页多少行
private $absolutepage;//当前页
private $pagecount;//总页码数
private $totalnum;//总行数
private $prepage;//上一页
private $nextpage; //下一页
private $sqlstr;
private $baseurl; //不带get参数的url get传输网址
function __construct($pagesize,$absolutepage,$baseurl,$sqlstr)
{
$this->absolutepage = $absolutepage;
$this->pagesize = $pagesize;
$this->baseurl = $baseurl;
$this->sqlstr = $sqlstr;
}
//显示主体内容
public function listinfo()
{
$result = dbhelper::executecommand($this->sqlstr);
// $result = mysql_query($this->sqlstr,$conn);
$this->totalnum = mysql_num_rows($result);//取处结果集的所有行数
$this->pagecount = (int)(($this->totalnum -1)/$this->pagesize)+1; //计算出一共有多少页
if($this->absolutepage == "" || !is_numeric($this->absolutepage))//如果是页面第一次加载那么absolutepage的值一定是空的这时我们认为用户是第一次访问就给他看第一页(当然你如果想给他看第5页也可以)
{
$this->absolutepage = 1;//给他看第一页
}
if($this->absolutepage > $this->pagecount)//如果请求的下一页的页码超过了总页数我们就显示最后一页
{
$this->absolutepage = $this->pagecount;//显示最后一页
}
if ($this->totalnum > 1 && $this->absolutepage > 1) //如果满足显示上一页的条件我们就让上一页的页码变量的值等于当前页(absolutepage)减一
{
$this->prepage = $this->absolutepage -1;
}
if($this->absolutepage >= 1 && $this->absolutepage < $this->pagecount)//在当前页至少是1并且不大于总页数时给下一页变量赋值为当前页加1
{
$this->nextpage = $this->absolutepage + 1;
}
if (mysql_data_seek($result,($this->absolutepage -1) * $this->pagesize))//决定从结果集的哪个位置开始检索数据//决定从结果集的哪个位置开始检索数据 mysql_data_seek指向下一行
{
include("goodstemplate.php");
for ($i = 0; $i < $this->pagesize; $i++)
{
if(($info = mysql_fetch_array($result)))
{
$name = $info['goodsname'];
$tupian = $info['goodsphopo'];
$id = $info['goodsid'];
$price = $info['goodsprice'];
$url = 'detail.php?id='.$id;
$items = str_replace(array('{name}','{tupian}','{url}','{price}','{id}'),array($name,$tupian,$url,$price,$id),$goodscontent);//str_replace替换两个数组 第二个替换第一个数组,$goodscontent参数是要替换的对象
$cishu++;
echo $items;
if ($cishu % 4 == 0)
{
?>
</tr><tr>
<?php
}
}
}
}
}
//分页动作函数
public function topage()
{
include('topagetemplate.php');
if($this->totalnum > 1 && $this->absolutepage > 1)
{
$this->prepage = $this->absolutepage - 1;
}
if($this->absolutepage >= 1 && $this->absolutepage < $this->pagecount)
{
$this->nextpage = $this->absolutepage + 1;
}
$preurl = $this->baseurl."?absolutepage=$this->prepage";
$nexturl = $this->baseurl."?absolutepage=$this->nextpage";
$items = str_replace(array('{totalnum}','{absolutepage}','{pagecount}','{preurl}','{nexturl}'),array($this->totalnum,$this->absolutepage,$this->pagecount,$preurl,$nexturl),$topage);
echo $items;
}
}
?>