Codeigniter在分页中生成额外的页面链接

时间:2018-04-05 09:27:35

标签: php codeigniter pagination codeigniter-3

我有一个分页用户列表,其中总共有2个记录,分页为2.这意味着我只能在分页中获得1个链接。但我得到2个链接,其中第二个链接(即2)不应该呈现。

以下是我的控制器和型号代码:

class Users extends CI_Controller {

    function __construct()
    {
        parent::__construct();

        $this->load->model('users_model');
        $this->load->model('roles_model');
        $this->load->model('branches_model');
    }

    public function index()
    {
        $this->data['page_title'] = 'Users';
        $config["base_url"] = base_url() . "admin/users";
        $config["total_rows"] = $this->users_model->record_count();
        $config["per_page"] = 2;
        $config["uri_segment"] = 3;

        $this->pagination->initialize($config);

        $page = ($this->uri->segment($config["uri_segment"])) ? $this->uri->segment($config["uri_segment"]) : 0;

        $this->data['users'] = $this->users_model->get_users(NULL,$config["per_page"], $page);
        $this->data["links"] = $this->pagination->create_links();
        $this->render('admin/list_users_view');
    }
}
class Users_model extends CI_Model {

    public function __construct() {
        parent::__construct();
        $this->load->database();
    }

    public function record_count() {
        return $this->db->count_all("users");
    }

    public function get_users($UserId = FALSE, $limit = FALSE, $start = FALSE) {

        $this->db->select('users.*,roles.RoleName,branches.BranchName');
        $this->db->from('users');
        $this->db->join('roles', 'users.RoleId = roles.RoleId', 'inner');
        $this->db->join('branches', 'users.BranchId = branches.BranchId', 'inner');
        $this->db->where('users.BranchId',2);


        $filtered_count = $this->db->count_all_results('', false);
        $this->db->limit($limit, $start);
        $query = $this->db->get()->result();

        $data_array = array('total_records' => $filtered_count, 'records' => $query );
        return $data_array;
    }
}

pagination

1 个答案:

答案 0 :(得分:0)

尝试重新排序控制器上的变量并使用模型中返回的total_records

public function index()
{
    $this->data['page_title'] = 'Users';
    $config["base_url"] = base_url() . "admin/users";
    $config["per_page"] = 2;
    $config["uri_segment"] = 3;

    $page = ($this->uri->segment($config["uri_segment"])) ? $this->uri->segment($config["uri_segment"]) : 0;

    $this->data['users'] = $this->users_model->get_users(NULL,$config["per_page"], $page);

    $config["total_rows"] = $this->data['users']['total_records'];

    $this->pagination->initialize($config);

    $this->data["links"] = $this->pagination->create_links();
    $this->render('admin/list_users_view');
}