在模型中使用查询的 Codeigniter 分页
Posted
技术标签:
【中文标题】在模型中使用查询的 Codeigniter 分页【英文标题】:Codeigniter Pagination Using Query in Model 【发布时间】:2015-07-03 20:18:12 【问题描述】:我想使用 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 确实有效,但数据无法成功运行。我的模型有什么问题?
【问题讨论】:
【参考方案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);
【讨论】:
【参考方案2】:limit $offset",$limit
应该是
limit $offset,$limit"
【讨论】:
以上是关于在模型中使用查询的 Codeigniter 分页的主要内容,如果未能解决你的问题,请参考以下文章
使用 codeigniter 和 jquery 进行 AJAX 分页
php 在CodeIgniter App中创建分页功能。在CodeIgniter中学习如何为分页创建模型,控制器和视图:https://www.cl