首页 » 网站推广 » php分页pager技巧_OOP理解应用之分页类

php分页pager技巧_OOP理解应用之分页类

访客 2024-12-11 0

扫一扫用手机浏览

文章目录 [+]

前面实现的分页可能每次都须要重新设计和编写,不符合面向工具的特色,按照面向工具的设计理念来封装分页类可以极大的供应事情效率。

二、实现效果

php分页pager技巧_OOP理解应用之分页类

三、紧张源码

php分页pager技巧_OOP理解应用之分页类
(图片来自网络侵删)

<?php

/

Describe:分页类完身分歧的数据库不同的数据表分页功能,支持联表

/

include_once('config.inc.php');

class Pager{

private $pageIndex;//当前页数

private $pageSize;//每页显示的记录数

private $count;//总记录数

private $pageCount;//总页数

private $condition;//查询条件

private $order;//排序

private $mysql;//数据操作类工具

private $tableName;//须要分页的表

//当前页数属性

function setPageIndex($value){

$this->pageIndex=$value;

}

function getPageIndex(){

return $this->pageIndex;

}

//每页显示记录数

function setPageSize($value){

$this->pageSize=$value;

//改变每页显示的记录数会影响总页数

$this->getPageCount();

}

function getPageSize(){

return $this->pageSize;

}

//总记录数

function getCount(){

$this->InnerGetCount();//调用自己的内部函数

return $this->count;

}

//总页数

function getPageCount(){

$this->InnerGetCount();

$this->pageCount=ceil($this->count/$this->pageSize);

if($this->pageCount==0)//没有符合的记录条数默认为1页

$this->pageCount=1;

return $this->pageCount;

}

//查询条件

function setCondition($value){

$this->condition=$value;

//设置条件会影响总记录数和总页数

$this->getPageCount();

}

//排序

function setOrder($value){

$this->order=$value;

}

//数据库操作类工具

function getMysql(){

return $this->mysql;

}

//布局函数初始化配置,$table须要分页的表

function __construct($tableName){

$this->tableName=$tableName;

$this->pageIndex=1;//默认为第1页

$this->pageSize=1;//每页显示记录数,默认为10条

$this->count=0;

$this->pageCount=1;

$this->condition='';

$this->order='';

//数据库操作类工具

if(DBTYPE=='mysqli'){

include_once('MysqliHelper.php');

$this->mysql=new MysqliHelper(DB);//无法通过魔术函数自动装载

}

elseif(DBTYPE=='mysql'){

include_once('MysqlHelper.php');

$this->mysql=new MysqlHelper(DB);//无法通过魔术函数自动装载

}

}

//获取分页记录

function GetRecord(){

$sql='select from '.$this->tableName.' where 1=1 '.$this->condition.' '.$this->order.' limit '.($this->pageIndex-1)$this->pageSize.','.$this->pageSize;

return $this->mysql->Execute($sql);

}

//获取总记录数

private function InnerGetCount(){

$sql=\"大众select count() from \"大众.$this->tableName.\公众 where 1=1 \公众.$this->condition;

$result=$this->mysql->Execute($sql,2);

$this->count=$result[0][0];

}

}

?>

四、测试

<form action=\"大众index.php\"大众 method=\公众post\公众>

按:标题或内容包含<input name=\"大众key\"大众 id=\"大众key\"大众 value=\"大众<?php echo $key;?>\"大众 /><input type=\"大众submit\公众 value=\公众 搜 索 \"大众 />

</form>

<p><a href=\"大众index.php?or=1\公众>序号</a> 标题</p>

<?php

include_once('mysql/Pager.php');//引入

$pager=new Pager('(select ttid,ttname from tt) as temp');

//翻页

if($_GET['pi'])

$pager->setPageIndex($_GET['pi']);

$pager->setPageSize(1);

//搜索

if($_REQUEST['key']){

$key=$_REQUEST['key'];

$pager->setCondition(\"大众and (ttname like '%$key%' or ttc like '%$key%')\公众);

}

//排序

if($_GET['or']){

$or=$_GET['or'];

$pager->setOrder(\"大众order by ttid desc\"大众);

}

$arr=$pager->GetRecord();

if(count($arr)){

foreach($arr as $row){

echo \"大众<p>$row[ttid] $row[ttname]</p>\"大众;

}

}

if($pager->getPageIndex()==1)//首页

echo '首页&nbsp;&nbsp;上一页&nbsp;&nbsp;';

else

echo \"大众<a href='index.php?pi=1'>首页</a>&nbsp;&nbsp;<a href='index.php?pi=\"大众.($pager->getPageIndex()-1).\"大众'>上一页</a>&nbsp;&nbsp;\"大众;

//提示信息

echo \"大众当前:\公众.$pager->getPageIndex().\"大众页/共\"大众.$pager->getPageCount().\"大众页 每页显示<input style='width:30px;' value='\"大众.$pager->getPageSize().\公众'>条/共\"大众.$pager->getCount().\"大众条记录&nbsp;&nbsp;\公众;

if($pager->getPageIndex()==$pager->getPageCount())//尾页

echo '下一页&nbsp;&nbsp;尾页';

else

echo \"大众<a href='index.php?pi=\"大众.($pager->getPageIndex()+1).\"大众&key=$key'>下一页</a>&nbsp;&nbsp;<a href='index.php?pi=\公众.$pager->getPageCount().\"大众'>尾页</a>&nbsp;&nbsp;\"大众;

?>

五、类访问润色符

和类成员访问润色符一样,类有一些访问润色符有着分外的浸染,比如static、abstract等。

<?php

class test{

private $tt;

//protected外部工具无法访问,本类和子类可以访问

public function setTT($value){

$this->tt=$value;

}

public function getTT(){

return $this->tt;

}

function tf(){

echo $this->tt;

}

}

$test=new test();

$test->setTT('aaa');

echo $test->getTT();

echo '<br>';

(new test())->setTT('bbb');//实例化时不用工具保存必须要加小括号

echo (new test())->getTT();//再次实例化默认值为空

?>

有的时候,我们系统类的成员赋值往后不系统实例化工具来进行访问,而且别的页面也可以访问这个值,可以在类成员中加上static(静态)关键字。

<?php

class test{

static function tf(){

echo 'bbb';

}

}

test::tf();

?>

把稳:静态函数访问的变量必须是静态的,反之静态成员不一定保存在静态类中。

再看下面的代码

<?php

class test{

static public $tt='aaa';

function tf(){

self::$tt='bbb';//类的静态成员内部访问只能利用self::

echo self::$tt;

}

}

echo test::$tt;//类名::成员

(new test())->tf();

?>

跨页面保存数据,静态类成员访问时不能实例化直接类名::$变量。

Index.php代码

<?php

class test{

static public $tt='aaa';

function tf(){

self::$tt='bbb';//类的静态成员内部访问只能利用self::

echo self::$tt;

}

}

?>

直接获取,index1.php代码

<?php

include_once('index.php');

echo test::$tt;

?>

标签:

相关文章

phparrayhashmap技巧_Java路径36Java的HashMap

特点HashMap底层是由哈希表构造组成的,实在便是“数组+链表”的组合体,数组是HashMap的主体构造,链表则紧张是为理解决哈...

网站推广 2024-12-13 阅读0 评论0