国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 語(yǔ)言 > PHP > 正文

Yii框架分頁(yè)實(shí)現(xiàn)方法詳解

2024-05-04 23:57:21
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了Yii框架分頁(yè)實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:

下家公司用的框架是yii,這幾天看了下相關(guān)教程,今兒把分頁(yè)教程寫下,最后把tp的分頁(yè)也給整合進(jìn)了yii,因?yàn)閭€(gè)人覺(jué)得yii分頁(yè)沒(méi)有tp用的順手。

首頁(yè),在models目錄里創(chuàng)建個(gè)Auth.php的模型文件,里面寫入

class Auth extends CActiveRecord {  public static function model($className = __CLASS__) {    return parent::model($className);  }  public function tableName() {    return '{{auth}}';  }}

接著在controllers目錄里創(chuàng)建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']) ? $_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目錄內(nèi)添加auth.php文件,里面寫入

<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'=>'首頁(yè)',    'lastPageLabel'=>'末頁(yè)',    'prevPageLabel'=>'上一頁(yè)',    'nextPageLabel'=>'下一頁(yè)',    'pages'=>$page,    'maxButtonCount'=>13,    )  );?></div>

上面是yii自帶的寫法,這里引入tp的分頁(yè)類,做個(gè)簡(jiǎn)單的改動(dòng),步驟如下

首先,把tp的AjaxPage.class.php和Page.class.php移動(dòng)到y(tǒng)ii的項(xiàng)目目錄下的 protected/components下面,并且把文件名稱分布改為AjaxPage.php和Page.php,分別進(jìn)入兩個(gè)文件,把里面的C方法去掉,也就是這一句

$this->varPage = C('VAR_PAGE') ? C('VAR_PAGE') : 'p' ;

改為

$this->varPage = 'p' ;

改完之后,這個(gè)兩個(gè)文件是不需要引入的,因?yàn)閥ii在啟動(dòng)時(shí)會(huì)自動(dòng)加載的。具體的可見(jiàn)protected/config.php/main.php的配置中的

// autoloading model and component classes  'import'=>array(    'application.models.*',    'application.components.*',  ),

其次,在protected/config.php/目錄里新建一個(gè)common.php文件,這個(gè)文件就放些項(xiàng)目的公共函數(shù),熟悉tp的朋友應(yīng)該知道tp也有公共函數(shù)文件,很好用,這里借鑒下,yii應(yīng)該也有吧,目前還沒(méi)發(fā)現(xiàn)。在該文件寫入

// 根據(jù)頁(yè)碼獲取列表function getListByPage($model, $select = '*', $condition = '', $limit = 10, $order = '', $p = '', $ajax = 0) {  // 初始化參數(shù)  $_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;}

這個(gè)文件可就要引入了,不然加載不了,可以在項(xiàng)目的入口文件index.php里自行引入,代碼如下

require_once(dirname($config) . '/common.php');

最后在indexController.php中用到分頁(yè)的地方調(diào)用該方法

public function actionPage() {    $model = Auth::model();    $info = getListByPage($model);    $this->renderPartial('page', array('info' => $info));}

封裝了此方法,以后調(diào)用分頁(yè)時(shí),只需傳幾個(gè)參數(shù),簡(jiǎn)單又快捷。在page.php頁(yè)面上調(diào)用

<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>

同時(shí)利用findAll也可以實(shí)現(xiàn)分頁(yè)的查詢效果,代碼如下

function getListByPage($model, $select = '*', $condition = '', $limit = 10, $order = '', $p = '', $ajax = 0) {  if (!$model) {    return array();;  }  // 初始化參數(shù)  $_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;}

總結(jié):

經(jīng)歷過(guò)ci、tp兩個(gè)框架后,再看yii進(jìn)度快很多。掌握某個(gè)框架,個(gè)人認(rèn)為無(wú)非就是掌握mvc的使用規(guī)則,在model層調(diào)用數(shù)據(jù)庫(kù)方法得到數(shù)據(jù),controller層調(diào)用model層數(shù)據(jù)并進(jìn)行邏輯處理,再傳給view層,同時(shí)了解框架的模板操作,表單操作,分頁(yè)操作,文件上傳操作,cookie和session操作,url調(diào)用,這些掌握了,在經(jīng)過(guò)項(xiàng)目的磨合,就差不多了,理解了常用操作之后,再去看看源碼,對(duì)比并總結(jié)框架間的區(qū)別和共性,從而升華自己的技術(shù),以后常用開(kāi)發(fā)就不在話下,拿可觀的薪水也是如此。

希望本文所述對(duì)大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到PHP教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 正宁县| 南丰县| 营山县| 永福县| 兴山县| 台湾省| 青浦区| 潜江市| 惠来县| 项城市| 平顺县| 香河县| 阳江市| 章丘市| 闸北区| 古交市| 久治县| 长泰县| 渭源县| 新乡县| 澜沧| 江西省| 乡城县| 寻乌县| 辉县市| 博爱县| 东阳市| 颍上县| 武山县| 巫溪县| 林周县| 申扎县| 剑阁县| 云梦县| 临武县| 成安县| 金门县| 永城市| 巍山| 安新县| 新民市|