+ -
当前位置:首页 → 问答吧 → 分页功能,类已经封装好了,可以非常简单的调用

分页功能,类已经封装好了,可以非常简单的调用

时间: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

PHP不用Bean的吗?刚开始学PHP不懂,请教!!!我写了一个Bean和方法pr(),方法display().我觉得发下已经封装好的类,你根本就不用写里面的实现,只要看这个类和方法的作用与功能.

作者: lan   发布时间: 2007-04-17

如果才能快速进入草根行列,我以前是做Java的.现在项目需要要转型.请各位前辈指导指导

作者: lan   发布时间: 2007-04-17

:) 支持一下.

作者: hiler   发布时间: 2007-04-19

zhichi

作者: h058   发布时间: 2007-04-19

复制PHP内容到剪贴板
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

呵呵,看标题是不是以为在说你啊,因为我也是刚刚接触,刚刚才配置了环境。所以根本看不懂。学了3天就能写出这么长的代码来,也挺列害的了。

作者: zhoulei   发布时间: 2007-04-22

我觉得一个程序员写的代码长并不代表他有能力,相反用最简单的代码来完善更强大的功能,这才叫牛比.写的长的只是垃圾

作者: lan   发布时间: 2007-04-25

收藏备用

作者: scz2011   发布时间: 2007-04-26

收藏备用

作者: scz2011   发布时间: 2007-04-26

感觉默默那个短点看的没那么累:)
还是支持LZ

作者: 千与千寻   发布时间: 2007-04-26