Yii框架分页实现方法详解
(编辑:jimmy 日期: 2025/10/29 浏览:3 次 )
本文实例讲述了Yii框架分页实现方法。分享给大家供大家参考,具体如下:
下家公司用的框架是yii,这几天看了下相关教程,今儿把分页教程写下,最后把tp的分页也给整合进了yii,因为个人觉得yii分页没有tp用的顺手。
首页,在models目录里创建个Auth.php的模型文件,里面写入
class Auth extends CActiveRecord {
public static function model($className = __CLASS__) {
return parent::model($className);
}
public function tableName() {
return '{{auth}}';
}
}
接着在controllers目录里创建IndexController.php的控制文件,里面写入
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']) "a_id,a_nickname";
$criteria->condition='';
$criteria->limit = 10;
$criteria->offset = $p == 0 "htmlcode">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<div class="blogList">
<ul>
<"page">
<"htmlcode">
$this->varPage = C('VAR_PAGE') "htmlcode">
$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']) "htmlcode">
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>
<"page">
<"htmlcode">
function getListByPage($model, $select = '*', $condition = '', $limit = 10, $order = '', $p = '', $ajax = 0) {
if (!$model) {
return array();;
}
// 初始化参数
$_GET['p'] = isset($_GET['p']) "_blank" href="https://www.jb51.net/Special/386.htm">Yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。
上一篇:Yii框架实现图片上传的方法详解
下一篇:thinkPHP显示不出验证码的原因与解决方法分析