CodeIgniter 分页入门
无论您对什么进行分页,您通常都需要一个 URI 段来指定页面,除非您希望 URI 段指定一个偏移量。在我的示例中,我将假设需要页面。
因此,假设我们想要对一些 foos 进行分页,并且我们的数据库包含一个名为 foos 的表。假设我们有很多 foo (65)。在我们的 Foos 控制器中,我们有一些方法:
public function index()
{
$this->page();
}
public function page( $page = 1 )
{
// Here we get and show our foos
}
当我们想要获取 foos 当前页面的结果时,我们有一个页码参数,这就是我们如何知道要获取哪一组 foos 的方式,但首先我们还需要 foos 的总数。因此,在我们的页面方法中,我们可以调用模型上的方法来完成所有工作:
public function page( $page = 1 )
{
// Here we get and show our foos
$this->load->model('foos_model');
$view_data['foos'] = $this->foos_model->get_foos( $page );
}
工作全部在模型中完成
请记住,在模型中,我们需要 foo 的总数、分页链接创建,然后是该页面的 foo。让我们从计算 foos 开始:
public function get_foos( $page )
{
// First count all foos
$count = $this->db->count_all('foos');
}
接下来,创建分页链接。假设每页需要 10 个 foo:
public function get_foos( $page )
{
// First count all foos
$count = $this->db->count_all('foos');
// Create the pagination links
$this->load->library('pagination');
$this->load->helper('url');
$paging_conf = [
'uri_segment' => 3,
'per_page' => 10,
'total_rows' => $count,
'base_url' => site_url('foos/page'),
'first_url' => site_url('foos'),
'use_page_numbers' => TRUE
];
$this->pagination->initialize($paging_conf);
// Create the paging buttons for the view
$this->load->vars('pagination_links', $this->pagination->create_links());
}
现在是时候获取我们将展示的实际食物了。注意偏移量的计算:
public function get_foos( $page )
{
// First count all foos
$count = $this->db->count_all('foos');
// Create the pagination links
$this->load->library('pagination');
$this->load->helper('url');
$paging_conf = [
'uri_segment' => 3,
'per_page' => 10,
'total_rows' => $count,
'base_url' => site_url('foos/page'),
'first_url' => site_url('foos'),
'use_page_numbers' => TRUE
];
$this->pagination->initialize($paging_conf);
// Create the paging buttons for the view
$this->load->vars('pagination_links', $this->pagination->create_links());
// The pagination offset
$offset = $page * $paging_conf['per_page'] - $paging_conf['per_page'];
// Get our set of foos
$query = $this->db->get('foos', $paging_conf['per_page'], $offset);
}
最后,在将它们传回控制器之前确保存在 foos:
public function get_foos( $page )
{
// First count all foos
$count = $this->db->count_all('foos');
// Create the pagination links
$this->load->library('pagination');
$this->load->helper('url');
$paging_conf = [
'uri_segment' => 3,
'per_page' => 10,
'total_rows' => $count,
'base_url' => site_url('foos/page'),
'first_url' => site_url('foos'),
'use_page_numbers' => TRUE
];
$this->pagination->initialize($paging_conf);
// Create the paging buttons for the view
$this->load->vars('pagination_links', $this->pagination->create_links());
// The pagination offset
$offset = $page * $paging_conf['per_page'] - $paging_conf['per_page'];
// Get our set of foos
$query = $this->db->get('foos', $paging_conf['per_page'], $offset);
// Make sure we have foos
if( $query->num_rows() > 0 )
return $query->result();
// Else return default
return NULL;
}
回到你的控制器,我们可以将食物传递给视图:
public function page( $page = 1 )
{
// Here we get and show our foos
$this->load->model('foos_model');
$view_data['foos'] = $this->foos_model->get_foos( $page );
// Load the view and pass in the foos
$this->load->view('foos_view', $view_data);
}
显示分页链接和foos
在视图中,我们现在能够显示分页链接和 foos:
echo $pagination_links;
if( ! empty( $foos ) )
{
foreach( $foos as $foo )
echo $foo->name . '<br />';
}
结论
使用 CodeIgniter 对内容进行分页非常简单。如果您有疑问或问题,请阅读 CodeIgniter 文档:https://www.codeigniter.com/userguide3/libraries/pagination.html?highlight=pagination
有关使用 $this->db->query() 与 $this->db->get() 的额外信息
从功能上讲,只要不犯错误,使用 $this->db->query() 会产生与 $this->db->get() 相同的结果。在我的例子中,它看起来像这样:
// $query = $this->db->get('foos', $paging_conf['per_page'], $offset);
$query = $this->db->query(
'SELECT *
FROM foos
LIMIT ' . $offset . ', ' . $paging_conf['per_page'] );