【问题标题】:Codeigniter 3 pagination bug: the second page shows records 3 to 13 instead of 11 to 20Codeigniter 3 分页错误:第二页显示记录 3 到 13 而不是 11 到 20
【发布时间】:2018-01-26 06:56:21
【问题描述】:

我正在制作一个 Codeigniter 3 应用程序,其中包含一个我已分页的表格(大约 100 行),每页 10 行。

在我的家庭控制器中:

public function index() {
    $this->load->model('Customer');
    $this->load->library('pagination');
    $config = [
        'base_url' => base_url("index.php"),
        'per_page' => 10,
        'total_rows' => $this->Customer->get_num_rows(),
        'uri_segment' => 3,
        'first_tag_open' =>  '<li>',
        'first_tag_close' => '</li>',
        'last_tag_open' =>  '<li>',
        'last_tag_close' => '</li>',
        'full_tag_open' =>  '<ul class="pagination">',
        'full_tag_close' => '</ul>',
        'next_tag_open' =>  '<li>',
        'next_tag_close' => '</li>',
        'prev_tag_open' =>  '<li>',
        'prev_tag_close' => '</li>',
        'num_tag_open' =>   '<li>',
        'num_tag_close' =>  '</li>',
        'cur_tag_open' =>   '<li class="active"><a>',
        'cur_tag_close' =>  '</a></li>',
    ];
    $this->pagination->initialize($config);
        $customers = $this->Customer->getCustomers($config['per_page'], $this->input->get('page'));
    $this->load->view('home', ['records'=>$customers]);
}

在我的模型文件中:

class Customer extends CI_Model {
 public function __construct() {
    $this->load->database();
 }

 public function getCustomers($limit, $offset) {
    $this->db->limit($limit, $offset);
    $query = $this->db->get('customers');
    return $query->result();
 }
}

观点:

<div class="pagination-container text-center">
    <?php echo $this->pagination->create_links(); ?>
</div> 

第一页看起来不错(我收到了社区的帮助。谢谢!):

问题是第二页显示记录 3 到 13 而不是 11 到 20:

【问题讨论】:

  • 我已经编辑了我的答案,希望它对你有用

标签: php mysql codeigniter pagination


【解决方案1】:

在 Home 控制器中,在进行了使分页工作的更改之后,我现在有了:

public function index() {
    $this->load->model('Customer');
    $this->load->library('pagination');
    $config = [
        'base_url' => base_url("index.php"),
        'page_query_string' => TRUE,
        'query_string_segment' => 'page',
        'display_pages' => TRUE,
        'use_page_numbers' => TRUE,
        'per_page' => 10,
        'total_rows' => $this->Customer->get_num_rows(),
        'uri_segment' => 3,
        'first_link' => '&laquo;',
        'first_tag_open' =>  '<li>',
        'first_tag_close' => '</li>',
        'last_link' => '&raquo;',
        'last_tag_open' =>  '<li>',
        'last_tag_close' => '</li>',
        'full_tag_open' =>  '<ul class="pagination">',
        'full_tag_close' => '</ul>',
        'next_link' =>  '&rsaquo;',
        'next_tag_open' =>  '<li>',
        'next_tag_close' => '</li>',
        'prev_link' => '&lsaquo;',
        'prev_tag_open' =>  '<li>',
        'prev_tag_close' => '</li>',
        'num_tag_open' =>   '<li>',
        'num_tag_close' =>  '</li>',
        'cur_tag_open' =>   '<li class="active"><a>',
        'cur_tag_close' =>  '</a></li>'
    ];
    if (!isset($_GET[$config['query_string_segment']])) {
        $_GET[$config['query_string_segment']] = 1;
    }
    $limit = $config['per_page'];
    $offset = ($this->input->get($config['query_string_segment']) - 1) * $limit;
    $this->pagination->initialize($config);
    $customers = $this->Customer->getCustomers($limit, $offset);
    $this->load->view('home', ['records'=>$customers]);
}

效果很好,希望对很多人有帮助。但我想知道我是否可以简化,用更少的代码编写......

【讨论】:

    【解决方案2】:

    使用您当前的模型方法

    public function getCustomers($limit, $offset) {
        $this->db->limit($limit, $offset);
        $query = $this->db->get('customers');
        return $query->result();
    }
    

    你传入的是 $limit,你传入的是 10。 您的 $offset 似乎是您所看到的结果所示的页码。

    例如,如果您的 uri 段值为 2(对于第 2 页),您实际上将方法中的限制设置为

    $this->db->limit(10, 2);
    

    这将显示记录 3 到 13。

    您可以通过使用以下 var_dump 语句作为调试代码来检查值来验证这一点。

    public function getCustomers($limit, $offset) {
        $this->db->limit($limit, $offset);
        var_dump($limit, $offset);
        $query = $this->db->get('customers');
        return $query->result();
    }
    

    如果您无法直接在页面上看到这些内容,您将在 Web 浏览器中对 HTML 执行查看源代码时找到这些内容的输出。

    所以你需要做的就是设置$offset来反映条目数*页码。

    所以第1页将是$offset = $limit * ($page - 1) = 10 * 0 = 0的记录,限制为10将获得记录1到10。

    第2页应该是$offset = $limit * ($page - 1) = 10 * 1 = 10的记录将给出记录11-20

    因此这导致您需要将方法参数中的 $offset 重命名为 $page 并计算所需的 $offset

    public function getCustomers($limit, $page) {
        $offset = 0;
        if($page > 0) {
          $offset = $limit * ($page - 1); 
        }
        $this->db->limit($limit, $offset);
        $query = $this->db->get('customers');
        return $query->result();
    }
    

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      Codeigniter 使用类似 array() 的数组而不使用 []

      还可以使用单独的函数来计算下表中的所有结果

      public function index() {
      
          $this->load->model('customer_model');
          $this->load->library('pagination');
      
      
          $config['base_url'] = base_url("controllername");
          $config['total_rows'] = $this->customer_model->count_total();
          $config['per_page'] = 10;
          $config['uri_segment'] = 2;
          $config['num_links'] = 10;
      
          // Bootstrap
          $config['full_tag_open'] = '<div class="pagination"><ul>';
          $config['full_tag_close'] = '</ul></div><!--pagination-->';
          $config['first_link'] = '&laquo; First';
          $config['first_tag_open'] = '<li class="prev page">';
          $config['first_tag_close'] = '</li>';
          $config['last_link'] = 'Last &raquo;';
          $config['last_tag_open'] = '<li class="next page">';
          $config['last_tag_close'] = '</li>';
          $config['next_link'] = 'Next &rarr;';
          $config['next_tag_open'] = '<li class="next page">';
          $config['next_tag_close'] = '</li>';
          $config['prev_link'] = '&larr; Previous';
          $config['prev_tag_open'] = '<li class="prev page">';
          $config['prev_tag_close'] = '</li>';
          $config['cur_tag_open'] = '<li class="active"><a href="">';
          $config['cur_tag_close'] = '</a></li>';
          $config['num_tag_open'] = '<li class="page">';
          $config['num_tag_close'] = '</li>';
          /*
      
            Change your uri segment number to what you want make sure though you change it in the array above also uri_segment.
      
          */
          $page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
      
          $this->pagination->initialize($config);
      
          $data['paglinks'] = $this->pagination->create_links();
      
          $data['records'] = $this->customer_model->getCustomers($config['per_page'], $page);
      
          $this->load->view('home', $data);
      
      
      }
      

      http://www.codeigniter.com/user_guide/general/views.html#creating-loops

      http://www.codeigniter.com/user_guide/libraries/uri.html

      http://www.codeigniter.com/user_guide/general/routing.html#examples

      您可能需要设置路线

      $route['controllername/(:any)'] = 'controllername/index/$1';
      

      如果有另一个同名文件不会导致错误,我会将模型重命名为 customer_model 这样的名称

      class Customer extends CI_Model {
      
          public function __construct() {
             parent::__construct();  
          }
      
          public function getCustomers($limit, $offset) {
              $this->db->limit($limit, $offset);
              $query = $this->db->get('customers');
              return $query->result();
          }
      
          public function count_total()
          {
              return $this->db->count_all_results('customers');
          }
      
      }
      

      更好地自动加载数据库

      $autoload['libraries'] = array('database');
      

      【讨论】:

        【解决方案4】:

        偏移量是预览限制(不好解释)。示例:

        SELECT * FROM customers LIMIT 0, 10;
        

        偏移量为 0 且限制为 10,第二页:

        SELECT * FROM customers LIMIT 10, 10;
        

        偏移量是 10 和限制 10,第三页:

        SELECT * FROM customers LIMIT 20, 10;
        

        Offset 为 20,limit 为 10 等。使用 codeigniter 的 QueryBuilder:

        $this->db->limit( $limit, $offset );
        

        URL 参数是一个偏移量。

        然后对于分页库的实例,您可以使用 site_url() 函数。 base_url 是静态或资产位置,site_url() 是导航页面。

        检查 config.php 文件 $config['index_page'] 上的变量,如果您使用重写 url 删除“index.php”,则此变量为空,否则您的值为“index.php”。

        如果您使用段参数而不是变量,则需要添加路由条目。例如:

        $route['customers/(:num)'] = 'Customers/index/$1';
        

        或使用正则表达式限制器:

        $route['^customers/(:num)$'] = 'Customers/index/$1';
        

        记住,url 参数 (:num) 是一个偏移量。

        试试这个。

        【讨论】:

          猜你喜欢
          • 2018-10-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多