我在我的控制器中有一個分頁頁面,我對我的視圖中的每頁列表2數據感到困惑。這我使用的控制器代碼如下:codeigniter:每頁顯示2個列表的分頁邏輯
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
的工作邏輯,我的工作本身,因爲它可以方便我理解它。
我的模型代碼如下:
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");
}
您的意思是爲什麼頁面1顯示前兩條記錄,頁面2顯示下兩條記錄? –
@ChrisLam,耶夥計,我們不知道它背後的邏輯,我們只是在網上跟着一個教程,並暗示它..你能詳細說明它是如何顯示第1頁的前兩條記錄,第2頁顯示的是後兩條記錄?等等...... –
這將有助於社區,誰不知道誰可以抓住邏輯.. –