gpt4 book ai didi

php - 使用 get_where 而不是 get 时 Codeigniter 分页不起作用

转载 作者:行者123 更新时间:2023-11-29 10:25:30 24 4
gpt4 key购买 nike

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

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

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

enter image description here

我可以轻松地显示数据库中的所有客户,无论添加它们的用户是谁:

Controller

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();
}
}

但我需要能够仅向每个登录用户显示他的客户。。因此,在 Controller 中,我有:

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

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

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

在这种情况下,分页显示的项目比应有的多:只有一页,但有指向第 2 页的链接,但没有任何项目。

enter image description here

我的错误在哪里?

最佳答案

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

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

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

关于php - 使用 get_where 而不是 get 时 Codeigniter 分页不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48372173/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com