我正在codeigniter
开发一个项目,我在创建pagination
条记录时遇到了困难。
实际上,当我用户$this->db->get()
然后它没关系,分页工作正常。但是当使用$this->db->query()
进行分页时,分页也不起作用,以下是我controller
中的代码。
$this->load->library('pagination');
$pagination_config['base_url'] = base_url().'welcome/index';
$pagination_config['per_page'] = 3;
$pagination_config['num_links'] = 3;
$pagination_config['total_rows'] = $this->db->get('properties')->num_rows();
$this->pagination->initialize($pagination_config);
$q = $this->db->query("SELECT title FROM properties", $pagination_config['per_page']);
$r = $q->result();
foreach($r as $p){
echo $p->title.'<br />';
}
echo '<br />';
echo '<br />';
echo $this->pagination->create_links();
我还在查询中尝试LIMIT
,如下所示,
$q = $this->db->query("SELECT title FROM properties LIMIT 3");
它显示了你的记录,但在所有页面上都是一样的。
答案 0 :(得分:2)
无论您使用什么分页,通常都需要指定页面的URI段,除非您希望URI段指定偏移量。在我的例子中,我将假设对页面的渴望。
所以,假设我们想要对一些foos进行分页,我们的数据库包含一个名为foos的表。假装我们有足够的泡沫(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 );
}
请记住,在模型中我们需要总计数的foos,分页链接创建,然后是此页面的foos。让我们从计算foos开始:
public function get_foos( $page )
{
// First count all foos
$count = $this->db->count_all('foos');
}
接下来,创建分页链接。假设我们每页需要10个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());
}
现在是时候获得我们将展示的实际组合。注意偏移的计算:
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;
}
回到我们的控制器中,我们可以将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 );
// Load the view and pass in the foos
$this->load->view('foos_view', $view_data);
}
在视图中,我们现在能够显示我们的分页链接和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-&gt; db-&gt; query()就会产生与$ this-&gt; db-&gt; get()相同的结果。在我的例子中,它看起来像这样:
// $query = $this->db->get('foos', $paging_conf['per_page'], $offset);
$query = $this->db->query(
'SELECT *
FROM foos
LIMIT ' . $offset . ', ' . $paging_conf['per_page'] );