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

CodeIgniter 分頁類首頁尾頁不顯示問題解決方案與自訂配置封裝

     編輯:PHP 綜合知識

問題現象

CI Pagination 類別預設:總頁數少於 4 頁時,首頁與尾頁連結不顯示。需求:無論頁數多少,皆顯示首頁/尾頁連結。

根因分析

原始碼 system/libraries/Pagination.php 中:

var $num_links = 2; // 預設顯示當前頁前後各 2 頁

// 首頁顯示條件
if ($this->first_link !== FALSE AND $this->cur_page > ($this->num_links + 1)) { ... }

// 尾頁顯示條件
if ($this->last_link !== FALSE AND $this->cur_page < ($this->total_pages - $this->num_links)) { ... }

當總頁數 ≤ 2 * num_links + 1 (預設 5 頁) 時,條件永不成立,首尾頁隱藏。

解決方案

方案一:修改設定參數(簡單但有限制)

$config['num_links'] = 1; // 最小可設 1,總頁數 ≥ 3 時顯示首尾頁

若需「任何情況下皆顯示」,需修改原始碼。

方案二:修改 Pagination.php 原始碼(徹底解決)

檔案:system/libraries/Pagination.php

// 找到首頁顯示判斷(約第 200 行)
// 原本:
if ($this->first_link !== FALSE AND $this->cur_page > ($this->num_links + 1))

// 修改為:移除頁數條件,僅保留 first_link 設定檢查
if ($this->first_link !== FALSE)

// 同理修改尾頁判斷
if ($this->last_link !== FALSE)

方案三:建立自訂分頁 Library 繼承覆寫(推薦,不動核心檔)

// application/libraries/MY_Pagination.php
class MY_Pagination extends CI_Pagination {
    protected function _build_first_link() {
        if ($this->first_link !== FALSE) {
            return $this->first_tag_open . $this->first_link . $this->first_tag_close;
        }
        return '';
    }

    protected function _build_last_link() {
        if ($this->last_link !== FALSE) {
            return $this->last_tag_open . $this->last_link . $this->last_tag_close;
        }
        return '';
    }
}

分頁配置封裝至基底控制器

避免每個控制器重複撰寫 $config,封裝為父類別方法:

class MY_Controller extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('pagination');
    }

    /**
     * 產生分頁連結 HTML
     * @param string $base_url 基礎 URL
     * @param int $total_rows 總筆數
     * @param int $per_page 每頁筆數
     * @param bool $use_query_string TRUE= ?page=1, FALSE= /page/1
     * @return string 分頁 HTML
     */
    protected function buildPagination($base_url, $total_rows, $per_page, $use_query_string = TRUE) {
        $config['base_url'] = $base_url;
        $config['total_rows'] = $total_rows;
        $config['per_page'] = $per_page;
        $config['page_query_string'] = $use_query_string;
        $config['query_string_segment'] = 'page'; // 參數名

        // 首尾頁永遠顯示
        $config['first_link'] = '首頁';
        $config['first_tag_open'] = '
  • '; $config['first_tag_close'] = '
  • '; $config['last_link'] = '尾頁'; $config['last_tag_open'] = '
  • '; $config['last_tag_close'] = '
  • '; // 樣式配置(Bootstrap 4 範例) $config['full_tag_open'] = '
      '; $config['full_tag_close'] = '
    '; $config['num_tag_open'] = '
  • '; $config['num_tag_close'] = '
  • '; $config['cur_tag_open'] = '
  • '; $config['cur_tag_close'] = '(current)
  • '; $config['next_tag_open'] = '
  • '; $config['next_tag_close'] = '
  • '; $config['prev_tag_open'] = '
  • '; $config['prev_tag_close'] = '
  • '; $config['next_link'] = '下一頁 »'; $config['prev_link'] = '« 上一頁'; $this->pagination->initialize($config); return $this->pagination->create_links(); } }

    控制器使用範例

    class News extends MY_Controller {
        public function index() {
            $page = $this->input->get('page', true) ?: 1;
            $perPage = 15;
            $offset = ($page - 1) * $perPage;
    
            $total = $this->news_model->countAll();
            $list = $this->news_model->getList($offset, $perPage);
    
            $paginationHtml = $this->buildPagination(
                site_url('news/index'),
                $total,
                $perPage,
                TRUE // 使用 ?page= 格式
            );
    
            $this->load->view('news/index', [
                'list' => $list,
                'pagination' => $paginationHtml,
            ]);
        }
    }

    參數名稱衝突解決

    CI 內部使用 per_page 作為查詢字串參數名,可能與分頁元件衝突。解決:

    1. 修改 system/libraries/Pagination.php 中 var $query_string_segment = 'per_page'; 為 'page'。
    2. 或配置 $config['query_string_segment'] = 'page';(CI 3.0+ 支援)。

    總結

    • 首尾頁不顯示源於 num_links 預設邏輯限制。
    • 修改核心檔最徹底但升級麻煩,建議繼承 MY_Pagination 覆寫。
    • 封裝 buildPagination() 至基底控制器,統一風格、減少重複程式碼。
    • 搭配 Bootstrap 樣式配置,快速產出美觀分頁元件。

    掌握 CI 分頁類別的配置與擴充技巧,能靈活應對各種分頁顯示需求。

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