欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2017年05月20日 11:08:26   作者:molaifeng  
這篇文章主要介紹了Yii框架分頁實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了基于Yii框架實(shí)現(xiàn)分頁的原理、步驟與相關(guān)操作技巧,需要的朋友可以參考下

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

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

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

上面是yii自帶的寫法,這里引入tp的分頁類,做個(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)加載的。具體的可見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)該也有吧,目前還沒發(fā)現(xiàn)。在該文件寫入

// 根據(jù)頁碼獲取列表
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中用到分頁的地方調(diào)用該方法

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

封裝了此方法,以后調(diào)用分頁時(shí),只需傳幾個(gè)參數(shù),簡(jiǎn)單又快捷。在page.php頁面上調(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)分頁的查詢效果,代碼如下

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

更多關(guān)于Yii相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結(jié)》、《php優(yōu)秀開發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

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

相關(guān)文章

最新評(píng)論