本文实例讲述了Yii框架分页实现方法。分享给大家供大家参考,具体如下:
下家公司用的框架是yii,这几天看了下相关教程,今儿把分页教程写下,最后把tp的分页也给整合进了yii,因为个人觉得yii分页没有tp用的顺手。
首页,在models目录里创建个Auth.php的模型文件,里面写入
1
2
3
4
5
6
7
8
|
class Auth extends CActiveRecord {
public static function model( $className = __CLASS__ ) {
return parent::model( $className );
}
public function tableName() {
return '{{auth}}' ;
}
}
|
接着在controllers目录里创建IndexController.php的控制文件,里面写入
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
class IndexController extends Controller {
public function actionList() {
$criteria = new CDbCriteria();
$criteria ->order = 'a_id desc' ;
$count = Auth::model()-> count ( $criteria );
$page = new CPagination( $count );
$page ->pageSize = 10;
$page ->applyLimit( $criteria );
$auth = Auth::model()->findAll( $criteria );
$this ->renderPartial( 'auth' , array ( 'page' => $page , 'list' => $auth ));
}
public function actionList1() {
$p = isset( $_GET [ 'page' ]) ? $_GET [ 'page' ] : 0;
$criteria = new CDbCriteria();
$criteria ->select = "a_id,a_nickname" ;
$criteria ->condition= '' ;
$criteria ->limit = 10;
$criteria ->offset = $p == 0 ? 0 : (( $p -1)*10);
$criteria ->order = 'a_id desc' ;
$auth = Auth::model()->findAll( $criteria );
$count = Auth::model()-> count ( $criteria );
$page = new CPagination( $count );
$page ->pageSize = 10;
$page ->applyLimit( $criteria );
$this ->renderPartial( 'auth' , array ( 'page' => $page , 'list' => $auth ));
}
}
|
其中actionList和actionList1是$criteria的两种写法
最后在views目录里添加index目录,并在index目录内添加auth.php文件,里面写入
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<meta http-equiv= "Content-Type" content= "text/html; charset=utf-8" />
<div class = "blogList" >
<ul>
<?php foreach ( $list as $key => $value ){ ?>
<li>
<a><?php echo $value [ 'a_nickname' ];?></a>
</li>
<?php } ?>
</ul>
</div>
<div id= "page" >
<?php
$this ->widget( 'CLinkPager' , array (
'firstPageLabel' => '首页' ,
'lastPageLabel' => '末页' ,
'prevPageLabel' => '上一页' ,
'nextPageLabel' => '下一页' ,
'pages' => $page ,
'maxButtonCount' =>13,
)
);
?>
</div>
|
上面是yii自带的写法,这里引入tp的分页类,做个简单的改动,步骤如下
首先,把tp的AjaxPage.class.php和Page.class.php移动到yii的项目目录下的 protected/components下面,并且把文件名称分布改为AjaxPage.php和Page.php,分别进入两个文件,把里面的C方法去掉,也就是这一句
1
|
$this ->varPage = C( 'VAR_PAGE' ) ? C( 'VAR_PAGE' ) : 'p' ;
|
改为
1
|
$this ->varPage = 'p' ;
|
改完之后,这个两个文件是不需要引入的,因为yii在启动时会自动加载的。具体的可见protected/config.php/main.php的配置中的
1
2
3
4
5
|
// autoloading model and component classes
'import' => array (
'application.models.*' ,
'application.components.*' ,
),
|
其次,在protected/config.php/目录里新建一个common.php文件,这个文件就放些项目的公共函数,熟悉tp的朋友应该知道tp也有公共函数文件,很好用,这里借鉴下,yii应该也有吧,目前还没发现。在该文件写入
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
// 根据页码获取列表
function getListByPage( $model , $select = '*' , $condition = '' , $limit = 10, $order = '' , $p = '' , $ajax = 0) {
// 初始化参数
$_GET [ 'p' ] = isset( $_GET [ 'p' ]) ? intval ( $_GET [ 'p' ]) : 1;
$limit = intval ( $limit ) > 0 ? intval ( $limit ) : 10;
if ( $p ) {
$_GET [ 'p' ] = intval ( $p ) ? intval ( $p ) : 1;
}
$criteria = new CDbCriteria();
$count = $model -> count ( $criteria );
if ( $ajax ) {
$Page = new AjaxPage( $count , $limit );
} else {
$Page = new Page( $count , $limit );
}
$result [ 'page' ] = trim( $Page ->show());
$criteria ->select = $select ;
$criteria ->condition = $condition ;
$criteria ->limit = $Page ->listRows;
$criteria ->offset = $Page ->firstRow;
$criteria ->order = $order ;
$list = $model ->findAll( $criteria );
$result [ 'list' ] = $list ;
return $result ;
}
|
这个文件可就要引入了,不然加载不了,可以在项目的入口文件index.php里自行引入,代码如下
1
|
require_once (dirname( $config ) . '/common.php' );
|
最后在indexController.php中用到分页的地方调用该方法
1
2
3
4
5
|
public function actionPage() {
$model = Auth::model();
$info = getListByPage( $model );
$this ->renderPartial( 'page' , array ( 'info' => $info ));
}
|
封装了此方法,以后调用分页时,只需传几个参数,简单又快捷。在page.php页面上调用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<meta http-equiv= "Content-Type" content= "text/html; charset=utf-8" />
<div class = "blogList" >
<ul>
<?php foreach ( $info [ 'list' ] as $key => $value ){ ?>
<li>
<a><?php echo $value [ 'a_nickname' ];?></a>
</li>
<?php } ?>
</ul>
</div>
<div id= "page" >
<?php
echo $info [ 'page' ];
?>
</div>
|
同时利用findAll也可以实现分页的查询效果,代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
function getListByPage( $model , $select = '*' , $condition = '' , $limit = 10, $order = '' , $p = '' , $ajax = 0) {
if (! $model ) {
return array ();;
}
// 初始化参数
$_GET [ 'p' ] = isset( $_GET [ 'p' ]) ? intval ( $_GET [ 'p' ]) : 1;
$limit = intval ( $limit ) > 0 ? intval ( $limit ) : 10;
if ( $p ) {
$_GET [ 'p' ] = intval ( $p ) ? intval ( $p ) : 1;
}
$count = $model -> count ();
if ( $ajax ) {
$Page = new AjaxPage( $count , $limit );
} else {
$Page = new Page( $count , $limit );
}
$result [ 'page' ] = trim( $Page ->show());
$result [ 'list' ] = $model ->findAll( array (
'select' => $select ,
'condition' => $condition ,
'order' => $order ,
'limit' => $Page ->listRows,
'offset' => $Page ->firstRow,
));
return $result ;
}
|
总结:
经历过ci、tp两个框架后,再看yii进度快很多。掌握某个框架,个人认为无非就是掌握mvc的使用规则,在model层调用数据库方法得到数据,controller层调用model层数据并进行逻辑处理,再传给view层,同时了解框架的模板操作,表单操作,分页操作,文件上传操作,cookie和session操作,url调用,这些掌握了,在经过项目的磨合,就差不多了,理解了常用操作之后,再去看看源码,对比并总结框架间的区别和共性,从而升华自己的技术,以后常用开发就不在话下,拿可观的薪水也是如此。
希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。