在一页上有两个与codeigniter的分页

时间:2017-04-24 03:21:20

标签: codeigniter

我需要为我的用户提供很多分页的结果,另一个是我的问题结果。

当我在问题结果的链接3上时,它还会在用户列表分页上切换结果。

  

如果我点击问题分页链接,我怎么能确定它不会影响用户列表的结果。

我试过这个Best way to do multiple pagination on one page in codeigniter不起作用

正如您在下面的图片中看到的,因为我在问题列表第3页上影响了用户列表

enter image description here

<?php

class Dashboard extends MY_Controller {

    public $data = array();

    public function __construct() {
        parent::__construct();
        $this->load->model('user/user_model');
        $this->load->model('forum/question_model');

        $this->load->library('pagination');
    }

    public function index() {       
        $this->data['title'] = 'Dashboard';
        $this->data['is_logged'] = $this->session->userdata('is_logged');

        $config1['base_url'] = base_url('dashboard/');
        $config1['total_rows'] = $this->user_model->total_users();
        $config1['per_page'] = 2;
        $config1['uri_segment'] = 2;
        $config1['num_links'] = 200;
        $config1['use_page_numbers'] = FALSE;
        $config1['prefix'] = 'u_';

        $pagination1 = new CI_Pagination();
        $pagination1->initialize($config1);

        $this->data['pagination1'] = $pagination1->create_links();

        $page_segment1 = explode('_', $this->uri->segment(2));

        $page1 = ($this->uri->segment(2)) ? $page_segment1[1] : 0;

        $this->data['users'] = array();

        $users = $this->user_model->get_users($config1['per_page'], $page1);

        foreach ($users as $user) {
            $this->data['users'][] = array(
                'user_id' => $user['user_id'],
                'username' => $user['username'],
                'status' => ($user['status']) ? 'Enabled' : 'Disabled',
                'warning' => '0' . '%',
                'date' => date('d-m-Y H:i:s A', $user['date_created_on']),
                'href' => site_url('user/profile/') . $user['user_id']
            ); 
        }

        // Questions Pagination & Results   

        $config2['base_url'] = base_url('dashboard/');
        $config2['total_rows'] = $this->question_model->total_questions();
        $config2['per_page'] = 2;
        $config2['uri_segment'] = 2;
        $config2['num_links'] = 200;
        $config2['use_page_numbers'] = FALSE;
        $config2['prefix'] = 'q_';

        $pagination2 = new CI_Pagination();
        $pagination2->initialize($config2);

        $this->data['pagination2'] = $pagination2->create_links();

        $page_segment2 = explode('_', $this->uri->segment(2));

        $page2 = ($this->uri->segment(2)) ? $page_segment2[1] : 0;

        $this->data['questions'] = array();

        $questions = $this->question_model->get_questions($config2['per_page'], $page2);

        foreach ($questions as $question) {
            $this->data['questions'][] = array(
                'user_id' => $question['user_id'],
                'title' => $question['title']
            ); 
        }

        $this->data['navbar'] = $this->load->view('common/navbar', $this->data, TRUE);
        $this->data['header'] = $this->load->view('common/header', $this->data, TRUE);
        $this->data['footer'] = $this->load->view('common/footer', '', TRUE);

        $this->load->view('common/dashboard', $this->data);

    }
}

问题模型

<?php

class Question_model extends CI_Model {

    public function get_questions($limit, $start) {
        $this->db->select('*');
        $this->db->from('questions q');
        $this->db->join('users u', 'u.user_id = q.user_id', 'left');
        $this->db->limit($limit, $start);
        $query = $this->db->get();
        return $query->result_array();
    }

    public function total_questions() {
        return $this->db->count_all("questions");
    }
}

用户模型

<?php

class User_model extends CI_Model {

    public function get_users($limit, $start) {
        $this->db->select('u.status, u.date_created_on, ud.*');
        $this->db->from('users u', 'left');
        $this->db->join('users_userdata ud', 'ud.user_id = u.user_id', 'left');
        $this->db->limit($limit, $start);
        $query = $this->db->get();
        return $query->result_array();
    }

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

}

0 个答案:

没有答案