溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何使用Yii框架實現分頁

發布時間:2021-04-13 16:44:47 來源:億速云 閱讀:186 作者:Leah 欄目:開發技術

本篇文章給大家分享的是有關如何使用Yii框架實現分頁,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

首頁,在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']) ? $_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文件,里面寫入

<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方法去掉,也就是這一句

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

改為

$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;
}

以上就是如何使用Yii框架實現分頁,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女