【问题标题】:Codeigniter pagination error (offset)Codeigniter 分页错误(偏移)
【发布时间】:2013-09-17 06:16:13
【问题描述】:

对,第一次使用分页,但不能让它在第一页之后工作(404 错误)。这一切似乎都围绕着 $offset 变量,好像我将偏移量的变量更改为一个整数(例如 15),它从第 15 行开始在第一页上显示结果 - 但是 $this->uri->segment( 2)变量只是不工作。一切都显示正确,但第二页无法加载(正如我所说,404 错误)。

控制器:

<?php
class Brands extends CI_Controller
{
                public function index()
                {
                                $this->load->library('pagination');
                                $config['base_url']   = base_url() . '/brands/';
                                $config['total_rows'] = $this->db->count_all('seixweb');
                                $config['per_page']   = 15;
                                $limit                = $config['per_page'];
                                $offset               = $this->uri->segment(2);
                                $this->pagination->initialize($config);
                                $this->load->model('Default_model');
                                ... Etc.
                }
}

型号:

<?php
class Default_model extends CI_Model
{
                function get_list($limit, $offset)
                {
                                $query = $this->db->get('tbl', $limit, $offset);
                                return $query->result_array();
                }
}

网址(首页):

http://www.mydomain.com/brands

网址(第二页):

http://www.mydomain.com/brands/15

我做错了什么?

虽然和报错无关,查看:

        <!-- Products -->
        <div class="products">
            <div class="cl">&nbsp;</div>

            <p><?=$this->pagination->create_links();?></p>

            <div class="cl">&nbsp;</div>
        </div><!-- End Products -->

【问题讨论】:

  • 基本 url 应该包含方法名称,因此对于您的示例 base_url('brands/index')

标签: php codeigniter pagination offset


【解决方案1】:

在从模型中获取记录时,将您的限制和分段值发送到模型中,您将获得所有必需的记录

【讨论】:

    【解决方案2】:
    $config['base_url']   = base_url() . '/brands/';
    

    去掉最后一个斜线

    $config['base_url']   = base_url() . '/brands';
    

    添加规则routes.php

    $route['brands/(:num)'] = "brands";
    

    【讨论】:

      【解决方案3】:

      设置OFFSET的默认参数

      $config['per_page'] = $this->uri->segment(3, 15);
      

      第二个参数定义默认值为15 if $this-&gt;uri-&gt;segment(3) 不存在。

      【讨论】:

        【解决方案4】:

        那是因为您的链接可能不正确。

        codeigniter 的默认值是www.example.com/controller/action,如果你什么都没做,你的链接调用的是控制器品牌中名为 15 的函数。

        您需要将链接设置为@987654321@ 或告诉codeigniter 您希望它通过$config['uri_segment'] = 2; 查找第二个uri 段在您的情况下不推荐。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-04-28
          • 2011-05-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多