【问题标题】:Codeigniter pagination not working when using get_where instead of get使用 get_where 而不是 get 时,Codeigniter 分页不起作用
【发布时间】:2023-03-17 22:18:01
【问题描述】:

我正在使用 CodeIgniter 3 和 Twitter Bootstrap 开发一个注册和登录应用程序。

应用程序有 2 个表:userscustomers。登录后,用户可以添加他们的客户

客户显示在一个分页表中,如下图所示:

我很容易在数据库中显示所有客户,而不管添加他们的用户是什么:

控制器

class Home extends CI_Controller { 
   public function index() {
        $this->load->model('Customer');
        $this->load->library('pagination');
        $config = [
            'base_url' => base_url("/home/"),
            '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' => '«',
            '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) {
            $_GET[$config['query_string_segment']] = 1;
        }
        $uid = $this->session->userdata('user_id');
        $limit = $config['per_page'];
        $offset = ($this->input->get($config['query_string_segment']) - 1) * $limit;
        $this->pagination->initialize($config);
        $customers = $this->Customer->getCustomers($limit, $offset);
        if ($this->agent->is_mobile()) {
            $this->load->view('mobile/home', ['records'=>$customers]);
        } else {
            $this->load->view('home', ['records'=>$customers]);
        }
    }
}

模型

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

但我需要能够向每个登录用户显示他的客户。所以,在控制器中,我有:

  1. 添加了登录用户ID$uid = $this-&gt;session-&gt;userdata('user_id');
  2. $customers = $this-&gt;Customer-&gt;getCustomers($limit, $offset); 替换为$customers = $this-&gt;Customer-&gt;getCustomers($uid, $limit, $offset);

在模型中我已将getCustomers() 方法更改为:

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

在这种情况下,分页显示的项目比它应该显示的要多:只有一页,但有到第 2 页的链接,女巫没有项目。

我的错误在哪里?

【问题讨论】:

  • 停止在图片中显示姓名、电子邮件、电话等个人数据!或发布这些是测试数据

标签: php mysql twitter-bootstrap codeigniter pagination


【解决方案1】:

您计算的结果数量与获得所有客户时的结果数量相同。所以分页类仍然认为你有相同数量的条目。 即

'total_rows' =>    $this->Customer->get_num_rows(),

但是在使用 get_where 时,您会减少该计数。因此,您需要更改计算“total_rows”的方式以适合。

【讨论】:

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