2013-02-18 54 views
0

這裏是我的代碼:如何配置CodeIgniter分頁?

$config['base_url'] = base_url() . 'settings/profile/'; 
$config['total_rows'] = $data['count_user_posts']; 
$config['per_page'] = 3; 
$offset = ($this->uri->segment('3') * $config['per_page'])/2; 
$this->pagination->initialize($config); 
$config['use_page_numbers'] = True; 
$data['user_posts'] = $this->post_model->get_user_posts($_SESSION['user_id'], $config['per_page'], $config['offset']); 

問題是,當我點擊2或任何其他鏈接,它顯示了從以前的頁面也有一些數據。任何解決方案 - 我做錯了什麼?

+0

在上面的代碼中你計算'$ offset'但你傳遞或服用考慮相同而獲取記錄? – Rikesh 2013-02-18 08:27:07

+0

我不明白....在$偏移量我試圖從第三個uri段獲得偏移號碼 – rafi 2013-02-18 08:29:00

+0

您需要將偏移量傳遞給您的模型,並使用它來計算要返回的記錄。 – Jeemusu 2013-02-18 08:54:35

回答

1

下面是一個例子,我怎麼執行了

在控制器

function index() 
{ 
    $this->load->library('pagination'); 

    $config = array(); 
    $config["base_url"] = base_url() . "city/index"; 
    $config["total_rows"] = $this->city_model->record_count(); 
    $config["per_page"] = 20; 
    $config["uri_segment"] = 4; 

    $this->pagination->initialize($config); 

    $page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0; 
    $data["city"] = $this->city_model->get_cities($config["per_page"], $page); 
    $data["links"] = $this->pagination->create_links(); 
} 

在模型

function record_count() 
{ 
    return $this->db->count_all("city"); 
} 

function get_cities($limit,$start) 
{  

    $this->db->limit($limit,$start); 
    $this->db->select('*'); 
    $this->db->from('city'); 
    $this->db->order_by('city_name'); 
    $query = $this->db->get(); 

    if ($query->num_rows() > 0) { 
     foreach ($query->result() as $row) { 
      $data[] = $row; 
    } 

return $data; 
} 

return false; 

} 
1

做這樣

public function index($offset = 0) 
{ 
    $this->load->library('pagination'); 
    $limit = 10; 

    $total = $this->legend_model->get_legend_count($language_id); 

    $config['base_url'] = base_url().'legend/index/'; 
    $config['total_rows'] = $total; 
    $config['per_page'] = $limit; 
    $config['uri_segment'] = 3; 

    $config['first_link'] = '<< First'; 
    $config['last_link'] = 'Last >>'; 
    $config['next_link'] = 'Next ' . '&gt;'; 
    $config['prev_link'] = '&lt;' . ' Previous'; 
    $config['num_tag_open'] = '<span class="number">'; 
    $config['num_tag_close'] = '</span>'; 

    $config['cur_tag_open'] = '<span class="current"><a href="#">'; 
    $config['cur_tag_close'] = '</a></span>'; 

    $this->pagination->initialize($config); 
    $data['offset'] = $offset; 
    $data['legends'] = $this->legend_model->get_legend($language_id, $limit, $offset); 

    $this->template->write('title', 'Legend : Manage Legend'); 
    $this->template->write_view('content', 'legend/index', $data); 
    $this->template->render(); 
} 

在我已完成了模型

//Get legend 
    public function get_legend($language_id = 1, $limit = 10, $offset = 0) 
    {   
     $this->db->select('l.id,lt.title,lt.status'); 
     $this->db->from('legends l'); 
     $this->db->join('legend_translations lt', 'l.id = lt.legend_id'); 
     $this->db->where('lt.language_id', $language_id); 
     $this->db->order_by('l.id DESC'); 
     $this->db->limit($limit, $offset); 

     $legend = $this->db->get(); 

     return $legend->result(); 
    } 
+0

這個$偏移值從哪裏來? $ data ['offset'] = $ offset; – rafi 2013-02-18 08:35:36

+0

---從哪裏可以得到這個$偏移可變的? – rafi 2013-02-18 08:51:41

+0

偏移量 - 從哪個記錄開始,第一次設置的值是0,在第二頁中是10,如果極限= 10 – Shaolin 2013-02-18 08:51:55

1

。這將是offset變量...........感謝的每個人對我的幫助爲$代碼....

if($this->uri->segment(4) > 0) 
    $offset = ($this->uri->segment(4) + 0)*$config['per_page'] - $config['per_page']; 
else 
    $offset = $this->uri->segment(4);