PHP分页设计

WBOY
发布: 2016-06-20 12:56:58
原创
1118 人浏览过

一、分页类设计Page.php

class Page {

public $pageSize=6;//每页显示记录数

public $res;  //记录集

public $rowCount;//记录总数数

public $pageNow=1;  //当前页

public $pageCount=1;  //总页数

public $navigate;   //导航

public $url;    //地址

public $firstRow;  //每页显示的第一条记录

public $rollPage;  //分栏每页显示页数

public $startNum;   //【1】

public $endNum;


public function setNav(){

$navigate="";

 //本导航的起始页

if($this->startNum>1){

$jump=$this->startNum-$this->rollPage;

//$nav=$this->url."/pageNow/$jump";

$navigate.= "  ";

}

for($start=$this->startNum;$startendNum;$start++)

{

$navigate.="[{$start}]";

}

if($this->endNumpageCount)

{

$jump=$this->startNum+$this->rollPage;

$navigate.= ">>  ";

}

$navigate.="第".$this->pageNow."页/共".$this->pageCount."页";

$this->navigate=$navigate;

}


public function setPage($url,$pageNow,$rowCount,$pageSize=6,$rollPage=4){


$this->url=$url;

$this->pageNow=$pageNow;

$this->rowCount=$rowCount;

$this->pageSize=$pageSize;

$this->rollPage=$rollPage;

$this->pageCount=ceil($this->rowCount/($this->pageSize+0.0));

$this->firstRow=($this->pageNow-1)*$this->pageSize;


$this->startNum=floor(($this->pageNow-1)/$this->rollPage)*$this->rollPage+1;

$this->endNum=$this->startNum+$this->rollPage-1;

if($this->endNum>$this->pageCount)

{

$this->endNum=$this->pageCount;

}

if($rowCount==0){

$this->pageNow=1;

$this->rollPage=1;

$this->firstRow=1;

$this->pageCount=1;

}

$this->setNav();  //字符串存储导航

}

}


?>



二、SqlHelper.class.php定义分页显示方法

public function excute_dql_page($sql1,$sql2,&$page){

//数据表信息分页

//sql1数据,sql2求行数

$result=mysql_query($sql1,$this->conn)or die(mysql_errno());

$arr=array();

while($row=mysql_fetch_assoc($result)){

$arr[]=$row;

}

mysql_free_result($result);

$result=mysql_query($sql2,$this->conn) or die(mysql_errno());

if($row=mysql_fetch_row($result)){

$page->pageCount=ceil($row[0]/$page->pageSize);

$page->rowCount=$row[0];

}

mysql_free_result($result);

//实现导航条

                $page->res_array=$arr;   //数组存储记录集

$fenyePage->setNav();

        return $arr;

}

三、使用分页(userList.php)

header("content-type:text/html;charset=utf-8");

require_once 'SQLHelper.class.php';

require_once 'UserService.class.php';



echo"用户信息表
";

echo"

";


//创建fenyePage并设置属性值



$page=new Page();

$page->url="userList.php";

$page->pageNow=1;

        

if(isset($_GET['pageNow'])){

$page->pageNow=$_GET['pageNow'];

}

//创建Service负责产生一个记录集(分页后的)

        $page->setPage($url,$pageNow);

$userService=new UserService();

$userService->getUserListByPage($fenyePage);


 

  foreach ($page->res_array as $row){

  $id=$row['id'];

  echo"

";

  //print_r($row['id']);

 

  }

  echo"

id name 删除 修改
{$row['id']} {$row['name']} 删除 修改
";

 

  echo $fenyePage->navigate;

 

?>

 

到第

 



相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!