改为
$this->varPage = 'p' ;
改完之后,这个两个文件是不需要引入的,因为yii在启动时会自动加载的。具体的可见protected/config.php/main.php的配置中的
// autoloading model and component classes
'import'=>array(
'application.models.*',
'application.components.*',
),
其次,在protected/config.php/目录里新建一个common.php文件,这个文件就放些项目的公共函数,熟悉tp的朋友应该知道tp也有公共函数文件,很好用,这里借鉴下,yii应该也有吧,目前还没发现。在该文件写入
// 根据页码获取列表
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里自行引入,代码如下
require_once(dirname($config) . '/common.php');
最后在indexController.php中用到分页的地方调用该方法
public function actionPage() {
$model = Auth::model();
$info = getListByPage($model);
$this->renderPartial('page', array('info' => $info));
}
封装了此方法,以后调用分页时,只需传几个参数,简单又快捷。在page.php页面上调用
<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也可以实现分页的查询效果,代码如下
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;
}







