首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

Yii 1.x 分頁組件 (CPagination) 完整使用示例

     編輯:PHP 相關知識

場景

在 Yii 1.x 中,分頁常以組件形式放入 protected/components/Page.php,控制器配合視圖實現列表分頁。

控制器動作

public function actionIndex() {
    $user = User::model();
    $count = $user->count();           // 總記錄數
    $pageSize = 3;                     // 每頁條數
    $page = new Page($count, $pageSize); // 實例化分頁組件

    $sql = "SELECT * FROM {{user}} {$page->limit}";
    $info = $user->findAllBySql($sql); // 查詢當前頁數據
    $showPage = $page->fpage();        // 生成分頁 HTML

    $this->renderPartial('index', [
        'info' => $info,
        'show_page' => $showPage,
    ]);
}

分頁組件 Page.php 核心邏輯

class Page {
    private $total, $listRows, $limit, $uri, $page, $pageNum;
    private $config = ['header'=>'條記錄', 'prev'=>'上一頁', 'next'=>'下一頁', 'first'=>'首頁', 'last'=>'尾頁'];
    private $listNum = 8; // 顯示頁碼數量

    public function __construct($total, $listRows=10, $pa='') {
        $this->total = $total;
        $this->listRows = $listRows;
        $this->uri = $this->getUri($pa);
        $this->page = !empty($_GET['page']) ? (int)$_GET['page'] : 1;
        $this->pageNum = ceil($this->total / $this->listRows);
        $this->limit = $this->setLimit();
    }

    private function setLimit() {
        return "LIMIT " . ($this->page - 1) * $this->listRows . ", {$this->listRows}";
    }

    private function getUri($pa) {
        $url = $_SERVER['REQUEST_URI'] . (strpos($_SERVER['REQUEST_URI'], '?') ? '' : '?') . $pa;
        $parse = parse_url($url);
        if (isset($parse['query'])) {
            parse_str($parse['query'], $params);
            unset($params['page']);
            $url = $parse['path'] . '?' . http_build_query($params);
        }
        return $url;
    }

    public function __get($args) {
        return $args === 'limit' ? $this->limit : null;
    }

    // 首頁、上一頁、頁碼列表、下一頁、尾頁、跳轉輸入框 等私有方法省略...

    public function fpage($display = [0,1,2,3,4,5,6,7,8]) {
        $html = [];
        $html[0] = " 共有<b>{$this->total}</b>{$this->config['header']} ";
        $html[1] = " 每頁顯示<b>" . ($this->end() - $this->start() + 1) . "</b>條,本頁<b>{$this->start()}-{$this->end()}</b>條 ";
        $html[2] = " <b>{$this->page}/{$this->pageNum}</b>頁 ";
        $html[3] = $this->first();
        $html[4] = $this->prev();
        $html[5] = $this->pageList();
        $html[6] = $this->next();
        $html[7] = $this->last();
        $html[8] = $this->goPage();
        $out = '';
        foreach ($display as $i) $out .= $html[$i];
        return $out;
    }
}

視圖渲染

<?php foreach ($info as $row): ?>
  <div><?= $row->username ?></div>
<?php endforeach; ?>
<div class="pagination"><?= $show_page ?></div>

現代替代方案

Yii 1.1.6+ 內建 CPagination + CLinkPager,更標準、可維護性更強:

// 控制器
$criteria = new CDbCriteria();
$count = User::model()->count($criteria);
$pages = new CPagination($count);
$pages->pageSize = 10;
$pages->applyLimit($criteria);
$models = User::model()->findAll($criteria);

// 視圖
$this->widget('CLinkPager', ['pages' => $pages]);

建議新項目直接使用框架內建組件,僅在維護舊代碼時參考自定義分頁類。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved