如何通过分页显示连续编号的数据

时间:2019-05-16 08:27:24

标签: codeigniter pagination codeigniter-3

我正在尝试在codeigniter中以分页方式显示有序列表中的数据。该编号在第一页的第一页中一直工作到极限为止都很好,但是在下一页中,该页倒数从1开始倒数,而不是从上一页继续。

这是控制器代码:

   public function index()
   {   
    // init params
    $params = array();
    $limit_per_page = 2;
    $start_index = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
    $total_records = $this->model_pesan->get_total();
    $data['setting'] = $this->setting_model->get_file();

    if ($total_records > 0) 
    {
        // get current page records
        $params["results"] = $this->model_pesan->get_current_page_records($limit_per_page, $start_index);
        $params["ruangan"] = $this->ruangan_model->get_file();

        $config['base_url'] = base_url() . 'home/index';
        $config['total_rows'] = $total_records;
        $config['per_page'] = $limit_per_page;
        $config["uri_segment"] = 3;

        $config['full_tag_open']    = '<nav><ul class="pagination justify-content-center">';
        $config['full_tag_close']   = '</ul></nav>';

        $config['num_tag_open']     = '<li class="page-item"><span class="page-link">';
        $config['num_tag_close']    = '</span></li>';

        $config['cur_tag_open']     = '<li class="page-item active"><span class="page-link">';
        $config['cur_tag_close']    = '<span class="sr-only">(current)</span></span></li>';

        $config['next_tag_open']    = '<li class="page-item"><span class="page-link">';
        $config['next_tagl_close']  = '<span aria-hidden="true">&raquo;</span></span></li>';

        $config['prev_tag_open']    = '<li class="page-item"><span class="page-link">';
        $config['prev_tagl_close']  = '</span></li>';

        $config['first_tag_open']   = '<li class="page-item"><span class="page-link">';
        $config['first_tagl_close'] = '</span></li>';

        $config['last_tag_open']    = '<li class="page-item"><span class="page-link">';
        $config['last_tagl_close']  = '</span></li>';


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

        // build paging links
        $params["links"] = $this->pagination->create_links();
    }

    $this->load->view('template/header_user', $data);
    $this->load->view('user/home', $params);
    $this->load->view('template/footer_user', $data);
}

这是模型代码:

public function get_current_page_records($limit, $start) 
    {   
        $query = $this->db->query('set @row_number = 0');
        $this->db->select('(@row_number:=@row_number + 1) as num ,reservasi.id_reservasi,DATE_FORMAT(reservasi.tanggal, "%d-%m-%Y") as tanggal, reservasi.acara, reservasi.waktu_mulai, reservasi.waktu_selesai, reservasi.keterangan, ruangan.nama_ruangan, user.nama, reservasi.organisasi, reservasi.notelp');
        $this->db->from('reservasi');
        $this->db->join('user', 'reservasi.id_user = user.id');
        $this->db->join('ruangan', 'reservasi.id_ruangan = ruangan.id_ruangan');
        $this->db->limit($limit, $start);
        $query = $this->db->get();

        if ($query->num_rows() > 0) 
        {
            foreach ($query->result() as $row) 
            {
                $data[] = $row;
            }

            return $data;
        }
        return false;
    }

    public function get_total() 
    {
        return $this->db->count_all_results('reservasi');
    }

输出应为= 第一页 : 1个 2 3 4

然后在下一页中: 5 6 7 8

1 个答案:

答案 0 :(得分:0)

尝试在模型上将row_number设置为$start+1而不是0,这样就不会在每个请求的页面上都从0开始:

$query = $this->db->query('set @row_number = '.$start+1);