问题描述
我在控制器中有一个分页页面,我对视图中每页列出2个数据感到困惑.我正在使用的控制器代码如下:
I am having a pagination page in my controller, i am kind of puzzled in listing 2 data per page on my view. The controller code which i am using is as follows:
public function newsletter()
{
$this->load->library('pagination');
$config = array();
$config["base_url"] = base_url() . "index.php/welcome/newsletter";
$this->load->model('newsletter_model');
$total_row = $this->newsletter_model->record_count();
$config["total_rows"] = $total_row;
$config["per_page"] = 2; // per page 2 records
$config['use_page_numbers'] = TRUE;
$config['num_links'] = $total_row;
$config['cur_tag_open'] = ' <a class="current">';
$config['cur_tag_close'] = '</a>';
$config['page_query_string'] = FALSE;
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$this->pagination->initialize($config);
if($this->uri->segment(3)){
$page = ($this->uri->segment(3)) ;
}
else{
$page = 1;
}
$this->load->model('newsletter_model');
$data["results"] = $this->newsletter_model->fetch_data($config["per_page"], $page);
$str_links = $this->pagination->create_links();
$data["links"] = explode(' ',$str_links );
$this->load->model('newsletter_model');
$this->load->view('newsletter/newsletter',$data);
}
使用上面的代码,我每页可获得2条记录,但我无法理解分页背后的逻辑.谁能解释我在codeigniter pagination
中工作的逻辑以及我的工作本身,因为这对我来说很容易理解.
With the above code i am getting 2 records per page as i intended, but i could not understand the logic behind the pagination. can anyone explain me the logic worked in codeigniter pagination
, with my work itself as it would be handy for me to understand it.
我的模型代码如下:
public function fetch_data($limit, $id)
{
$this->db->select('*');
$this->db->from('ins_newsletter');
$this->db->order_by('nl_id', 'desc');
$this->db->limit($limit,$id);
$query = $this->db->get();
return $query->result_array();
}
public function record_count()
{
return $this->db->count_all("ins_newsletter");
}
推荐答案
CI的Paginator类仅为您生成分页链接.
CI's Paginator class only generates pagination links for you.
记录分页"逻辑发生在这里:
The "record pagination" logic happens here:
$this->db->limit($limit, $offset);
$limit
是要提取的记录数,$offset
是要返回的第一行的偏移量.
$limit
is the number of records to fetch, $offset
is the offset of the first row to return.
要获取前两个记录,我们将设置$limit
= 2,$offset
= 0
To fetch the first two records, we will set $limit
= 2, $offset
= 0
CI可转换为SQL,如:
CI translates into SQL like:
SELECT ...... FROM ins_newsletter LIMIT 0, 2;
要获取记录编号3和4,我们将设置$limit
= 2,$offset
= 2
To fetch record number 3 and 4, we will set $limit
= 2, $offset
= 2
CI可转换为SQL,如:
CI translates into SQL like:
SELECT ...... FROM ins_newsletter LIMIT 2, 2;
您的代码中有一个小错误.
There is a small bug in your code.
第1页的偏移量应为0,第2页的偏移量应为2,第3页的偏移量应为4,第4页的偏移量应为8,第5页的偏移量应为...等等.
The offset should be 0 for page 1, 2 for page 2, 4 for page 3, 6 for page 4, 8 for page 5...and so on.
public function fetch_data($limit, $page)
{
$offset = ($page - 1) * $limit;
...
}
我建议您将第二个参数更改为$page
而不是$id
.
And I would suggest you to change the second param to $page
instead of $id
.
这篇关于codeigniter:分页逻辑每页显示2个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!