在模型中使用查询的Codeigniter分页

时间:2015-04-25 03:58:37

标签: php codeigniter pagination

我想使用codeigniter进行分页但不能成功运行。 在我的模型中我使用像这样

function show_kegiatan($offset,$limit) {
        $query= $this->db->query("SELECT kegiatan . * , lk . * , fakultas.* , count( kegiatan_review.reviews ) AS 'Komentar', fakultas.*
                                    FROM kegiatan 
                                    INNER JOIN lk ON lk.id_LK = kegiatan.id_LK
                                    INNER JOIN fakultas on fakultas.id_fakultas = lk.id_fakultas
                                    LEFT OUTER JOIN kegiatan_review ON kegiatan.id_kegiatan = kegiatan_review.id_kegiatan   
                                    GROUP BY kegiatan.id_kegiatan
                                    ORDER BY kegiatan.tahun_periode DESC
                                    limit $offset",$limit);
        if ($query->num_rows() > 0) {
            return $query->result();
        }
    }

在我的控制器中

public function kegiatan($offset=0) {
           $data['user']=$this->m_login->get_user($this->session->userdata('sesi_admin'));           
           $jml = $this->db->get('kegiatan');           
           $config['base_url'] = base_url().'admin/kegiatan';

           $config['total_rows'] = $jml->num_rows();
           $config['per_page'] = 3; 
           $config['uri_segment'] = 3; 

           $config['full_tag_open'] = "<ul class='pagination pagination-sm' style='position:relative; top:-25px;'>";
           $config['full_tag_close'] ="</ul>";
           $config['num_tag_open'] = '<li>';
           $config['num_tag_close'] = '</li>';
           $config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
           $config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
           $config['next_tag_open'] = "<li>";
           $config['next_tagl_close'] = "</li>";
           $config['prev_tag_open'] = "<li>";
           $config['prev_tagl_close'] = "</li>";
           $config['first_tag_open'] = "<li>";
           $config['first_tagl_close'] = "</li>";
           $config['last_tag_open'] = "<li>";
           $config['last_tagl_close'] = "</li>";

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

           $data['halaman'] = $this->pagination->create_links();           
           $data['offset'] = $offset;

           $data['kegiatan'] = $this->m_admin->show_kegiatan($config['per_page'], $offset);

           $this->load->templateadmin('in/admin_kegiatan',$data);   
  }

在我的输出中,Offset确实有效,但数据无法成功运行。我的模特怎么了?

2 个答案:

答案 0 :(得分:1)

您可以使用$this->uri->segment(3)替换$offset

请尝试这个样本,其余的你必须改变以满足你的需要......

在你的模特中:

function show_kegiatan($perPage, $uri) {
    $uri = ($uri == null || $uri==''?0:$uri);       
    $this->db->select('kegiatan.kolommu1,kegiatan.kolommu2,lk.kolommu1,lk.kolommu2')->from('kegiatan')->join('lk','lk.id_LK = kegiatan.id_LK')->order_by('kegiatan.id_kegiatan','desc')->limit($perPage,$uri);
    $hasil = $this->db->get()->result_array();      
    return (sizeof($hasil) > 0?$hasil:null);
}

在您的控制器中:

function kegiatan(){
    $jml = $this->db->get('kegiatan')->num_rows();

    $config['full_tag_open'] = "<ul class='pagination pagination-sm' style='position:relative; top:-25px;'>";
    $config['full_tag_close'] ="</ul>";
    $config['num_tag_open'] = '<li>';
    $config['num_tag_close'] = '</li>';
    $config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
    $config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
    $config['next_tag_open'] = "<li>";
    $config['next_tagl_close'] = "</li>";
    $config['prev_tag_open'] = "<li>";
    $config['prev_tagl_close'] = "</li>";
    $config['first_tag_open'] = "<li>";
    $config['first_tagl_close'] = "</li>";
    $config['last_tag_open'] = "<li>";
    $config['last_tagl_close'] = "</li>";

    $config['base_url'] = site_url('admin/kegiatan');
    $config['total_rows'] = $jml;
    $config['per_page'] = 3;
    $config['uri_segment'] = 3;
    $config['first_link'] = 'Awal';
    $config['last_link'] = 'Akhir';
    $config['next_link'] = 'Selanjutnya';
    $config['prev_link'] = 'Sebelumnya';

    $this->pagination->initialize($config);
    $data['kegiatan'] = $this->m_admin->show_kegiatan($config['per_page'], $this->uri->segment(3));

    $data['halaman'] = $this->pagination->create_links();
    $this->load->view('layout/admin/v_dashboard',$data);

    $this->load->templateadmin('in/admin_kegiatan',$data);   
}

答案 1 :(得分:0)

limit $offset",$limit

应该是

limit $offset,$limit"