分页功能,类已经封装好了,可以非常简单的调用
时间:2007-04-17
来源:互联网
*学PHP终于有3天了,现在来试自己是否已经进入PHP草根行列
*其中MYSQL查询语句学习默默同志的
*http://www.phpchina.com/2345/viewspace_3734.html
*/
首先是newpage.php(调试/调用实例)页面,如下:
<?php
include("page.php");
$db = mysql_connect("host","name","pass"); //创建数据库连接
$select = mysql_select_db("db",$db);
$page = new Page();
$page->setAllPageNum(mysql_num_rows(mysql_query("select * from table")));//设置查询结果的页数总数
$page->setPageSize(10);//设置每页显示的数据总数
$page->setNUM(10);//设置显示的页数
$page->setURL("newpage.php");//设置需要跳转URL
$cur = $_REQUEST['curpage'];//从request中得到当前页
$page->setCurpage($cur);//设置当前页
$curnum = $page->getCurPage()*$page->getPageSize();//根据当前页和每页显示的数据数量,求得本页第一条数据号码
$sql = 'select * from table Limit ';
$sql.= $curnum;
$sql.= $page->getPageSize();
$result = mysql_query("sql");
?>
<html>
<head>
<title>
分页功能
</title>
</head>
<body>
<?php
/**
* 输出数据部分略
* 分解出result就可以了
*/
?>
<?php $page->display();?>//显示分页数据
</body>
</html>
/**
*page.php类
*
**/
<?php
class page{
//总页数
var $allpagenum;
//每页信息显示数量
var $pagesize;
//当前页
var $curpage;
//上一页
var $prepage;
//下一页
var $nextpage;
//显示页数数量
var $num;
//$URL
var $url;
public function getAllPageNum()
{
return $this->allpagenum;
}
public function setAllPageNum($allpage)
{
$this->allpagenum = $allpage;
}
public function getPageSize()
{
return $this->pagesize;
}
public function setPageSize($ps)
{
$this->pagesize = $ps;
}
public function getCurPage()
{
if ($this->curpage>0)
return $this->curpage;
else
return 0;
}
public function setCurPage($cur)
{
if ($cur>0)
$this->curpage = $cur;
else
$this->curpage = 0;
}
public function getPrePage()
{
if ($this->curpage-1>=0)
$this->prepage = $this->curpage-1;
else
$this->prepage = 1;
return $this->prepage;
}
private function getNextPage()
{
if ($this->curpage+1<$this->allpagenum)
{
$this->nextpage = $this->curpage+1;
}
else
{
$this->nextpage = $this->curpage;
}
return $this->nextpage;
}
public function getNUM()
{
if ($this->num>0)
return $this->num;
else
return 10;
}
public function setNUM($n)
{
if ($n>0)
$this->num = $n;
else
$this->num = 10;
}
public function setURL($u)
{
if ($u!=null)
$this->url = $u;
else
$this->url = "error.php";
}
public function getURL()
{
if ($this->url != null)
return $this->url;
else
return "error.php";
}
//打印
private function pr($i,$st)
{
$str = '<a href="';
$str.=$this->getURL();
$str.='?curpage=';
$str.=$i;
$str.='">';
if ($i==$this->curpage)
{
$str.='<font color="#ff0000">';
$str.=$st;
$str.='</font>';
}
else
$str.=$st;
$str.=' </a>';
echo $str;
}
//输出页数
public function display()
{
$this->pr($this->getPrePage(),'上一页');
if ($this->allpagenum<=$this->num)
{ //输出所有的页数
for ($i=0;$i<$this->allpagenum;$i++)
{
$this->pr($i,$str);
}
}
else //输出从$curpage-$num/2到$curpage+$num/2的页数
{
if ($this->getCurpage()+$this->num/2>$this->allpagenum)
{ //如果当前页数+定义输出页数>总页数时输出最后10页
$start = 0;//起始位置
if ($this->allpagenum-$this->num>0)
$start = $this->allpagenum-$this->num+1;
for ($i=$start;$i<=$this->allpagenum;$i++)
{
$this->pr($i,$i);
}
}else
{ //输出当前页数-定义输出页数/2到当前页数+定义输出页数/2
$start = 0;
if ($this->getCurPage()-$this->num/2>0)
{
$start = $this->getCurPage()-$this->num/2;
$end = $this->getCurPage()+$this->num/2;
}
else
{
$start = 1;
$end = $this->num+1;
}
for ($i=$start;$i<$end;$i++)
{
$this->pr($i,$i);
}
}
}
$this->pr($this->getNextPage(),'下一页');
}
}
?>
[ 本帖最后由 lan 于 2007-4-16 16:16 编辑 ]
作者: lan 发布时间: 2007-04-16
作者: lan 发布时间: 2007-04-16
作者: netfound 发布时间: 2007-04-16
作者: lan 发布时间: 2007-04-17
作者: lan 发布时间: 2007-04-17
作者: hiler 发布时间: 2007-04-19
作者: h058 发布时间: 2007-04-19
PHP代码:
/**学PHP终于有3天了,现在来试自己是否已经进入PHP草根行列
*其中MYSQL查询语句学习默默同志的
*[url]http://www.phpchina.com/2345/viewspace_3734.html[/url]
*/
首先是newpage.php(调试/调用实例)页面,如下:
<?php
include("page.php");
$db = mysql_connect("host","name","pass"); //创建数据库连接
$select = mysql_select_db("db",$db);
$page = new Page();
$page->setAllPageNum(mysql_num_rows(mysql_query("select * from table")));//设置查询结果的页数总数
$page->setPageSize(10);//设置每页显示的数据总数
$page->setNUM(10);//设置显示的页数
$page->setURL("newpage.php");//设置需要跳转URL
$cur = $_REQUEST['curpage'];//从request中得到当前页
$page->setCurpage($cur);//设置当前页
$curnum = $page->getCurPage()*$page->getPageSize();//根据当前页和每页显示的数据数量,求得本页第一条数据号码
$sql = 'select * from table Limit ';
$sql.= $curnum;
$sql.= $page->getPageSize();
$result = mysql_query("sql");
?>
<html>
<head>
<title>
分页功能
</title>
</head>
<body>
<?php
/**
* 输出数据部分略
* 分解出result就可以了
*/
?>
<?php $page->display();?>//显示分页数据
</body>
</html>
/**
*page.php类
*
**/
<?php
class page{
//总页数
var $allpagenum;
//每页信息显示数量
var $pagesize;
//当前页
var $curpage;
//上一页
var $prepage;
//下一页
var $nextpage;
//显示页数数量
var $num;
//$URL
var $url;
public function getAllPageNum()
{
return $this->allpagenum;
}
public function setAllPageNum($allpage)
{
$this->allpagenum = $allpage;
}
public function getPageSize()
{
return $this->pagesize;
}
public function setPageSize($ps)
{
$this->pagesize = $ps;
}
public function getCurPage()
{
if ($this->curpage>0)
return $this->curpage;
else
return 0;
}
public function setCurPage($cur)
{
if ($cur>0)
$this->curpage = $cur;
else
$this->curpage = 0;
}
public function getPrePage()
{
if ($this->curpage-1>=0)
$this->prepage = $this->curpage-1;
else
$this->prepage = 1;
return $this->prepage;
}
private function getNextPage()
{
if ($this->curpage+1<$this->allpagenum)
{
$this->nextpage = $this->curpage+1;
}
else
{
$this->nextpage = $this->curpage;
}
return $this->nextpage;
}
public function getNUM()
{
if ($this->num>0)
return $this->num;
else
return 10;
}
public function setNUM($n)
{
if ($n>0)
$this->num = $n;
else
$this->num = 10;
}
public function setURL($u)
{
if ($u!=null)
$this->url = $u;
else
$this->url = "error.php";
}
public function getURL()
{
if ($this->url != null)
return $this->url;
else
return "error.php";
}
//打印
private function pr($i,$st)
{
$str = '<a href="';
$str.=$this->getURL();
$str.='?curpage=';
$str.=$i;
$str.='">';
if ($i==$this->curpage)
{
$str.='<font color="#ff0000">';
$str.=$st;
$str.='</font>';
}
else
$str.=$st;
$str.=' </a>';
echo $str;
}
//输出页数
public function display()
{
$this->pr($this->getPrePage(),'上一页');
if ($this->allpagenum<=$this->num)
{ //输出所有的页数
for ($i=0;$i<$this->allpagenum;$i++)
{
$this->pr($i,$str);
}
}
else //输出从$curpage-$num/2到$curpage+$num/2的页数
{
if ($this->getCurpage()+$this->num/2>$this->allpagenum)
{ //如果当前页数+定义输出页数>总页数时输出最后10页
$start = 0;//起始位置
if ($this->allpagenum-$this->num>0)
$start = $this->allpagenum-$this->num+1;
for ($i=$start;$i<=$this->allpagenum;$i++)
{
$this->pr($i,$i);
}
}else
{ //输出当前页数-定义输出页数/2到当前页数+定义输出页数/2
$start = 0;
if ($this->getCurPage()-$this->num/2>0)
{
$start = $this->getCurPage()-$this->num/2;
$end = $this->getCurPage()+$this->num/2;
}
else
{
$start = 1;
$end = $this->num+1;
}
for ($i=$start;$i<$end;$i++)
{
$this->pr($i,$i);
}
}
}
$this->pr($this->getNextPage(),'下一页');
}
}
?>
作者: 17too 发布时间: 2007-04-19
作者: zhoulei 发布时间: 2007-04-22
作者: lan 发布时间: 2007-04-25
作者: scz2011 发布时间: 2007-04-26
作者: scz2011 发布时间: 2007-04-26
还是支持LZ
作者: 千与千寻 发布时间: 2007-04-26
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28