2013-01-11 71 views
0

好吧,我一直在這個過去的一個小時,不能爲我的生活弄清楚這一點。 這是說限制2,25 ..當然,跳過兩個應該顯示的行...我仍然在第1頁,鏈接甚至不顯示!我認爲第一頁應該是0,25,因爲它顯示了應顯示在第一頁上的所有行。CodeIgniter分頁不顯示應顯示的所有行

到目前爲止,我只有5行,但它只顯示了其中的3個......前兩行沒有顯示查詢最初生成,直到添加LIMIT子句。 這裏是我的編程明智:

public function category($id, $page = NULL) { 
    //grab topics 
    parent::before(); 
    $data['forum_title'] = $this->forum->get_cat($id); 
    $data['id'] = $id; 

    $config = array(); 
    $config['base_url'] = base_url() . 'forum/category/'; 
    $config['total_rows'] = $this->forum->topic_count($id); 
    $config['per_page'] = 25; 
    $config['uri_segment'] = 4; 

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

    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0; 
    $data['topics_array'] = $this->forum->get_topics($id, $config['per_page'], $page); 

    $data['links'] = $this->pagination->create_links(); 
    $this->load->view('forum/category', $data); 
    parent::after(); 
} 

我承認我不懂URI_SEGMENT考驗..我有點事,但不是真的。我正在按照教程來生成這個。但是,我打印我的$限制和$開始變量和限制是25,因爲它應該是,但開始打印爲2 ..當我認爲它應該是0在第一頁。因爲還沒有25個結果,所以甚至不應該有第二頁。

這裏是我的函數來生成這些。 count_all_results()函數起作用(總共返回5個結果)。但是,只要添加LIMIT子句,get_topics()函數就不能正常工作。

//count the topics in a certain category id for pagination 
public function topic_count($id) { 
    $this->db->where('cat_id', $id); 
    $this->db->from('forum_topic'); 

    return $this->db->count_all_results(); 

} 
//get all topics in a certain category 
public function get_topics($id, $limit, $start) { 
    $this->db->distinct(); 
    $this->db->select('t.id, t.title, t.sticky, t.locked, u.username as author, COUNT(p.id) as postcount, (SELECT u.username 
     FROM forum_post fp 
     INNER JOIN user u ON fp.author_id = u.user_id 
     WHERE fp.topic_id = t.id 
     ORDER BY fp.date DESC 
     LIMIT 1) as lposter, (SELECT fp.date 
     FROM forum_post fp 
     WHERE fp.topic_id = t.id 
     ORDER BY fp.date DESC 
     LIMIT 1) as lastdate'); 
    $this->db->from('forum_topic t'); 
    $this->db->join('user u', 'u.user_id = t.author_id', 'inner'); 
    $this->db->join('forum_post p', 'p.topic_id = t.id', 'inner'); 
    $this->db->where('t.cat_id', $id); 
    $this->db->group_by('t.id, t.title, t.sticky, t.locked, author, lposter, lastdate'); 
    $this->db->order_by('t.sticky desc, p.date desc'); 
    $this->db->limit($limit, $start); 
    return $this->db->get()->result(); 
} 

我的網址我在現在的問題是論壇/分類/ 2 ...所以我假設的頁面將是這樣的:論壇/分類/ 2/1第1頁,論壇/分類/第2頁2/2等。正確? 因此,我將4作爲URI段,因爲它位於第4個參數位置。

任何幫助,將不勝感激......

EDITTTTTTTTT

好有人幫助我與此有關。但是,當我點擊第2頁時(我現在將限制降低到5,並且當前有7個結果,因此它生成了第2頁...它將用5代替ID 2(類別ID 2)..而不是去第2頁?? .... 2應該看起來像這樣:forum/category/2/2 ...但是它是這樣做的:論壇/ category/5

回答

2

您正在使用2個不同的URI段。您正在初始化分頁對象4,然後您發送段3到get_topics()(因爲$this->uri->segment(3))。您應該可能使用3,因爲URI段從0開始不是1。 ,無論你使用3還是4,你都應該在兩個地方使用同一個。使用$this->uri->segment($config['uri_segment'])而不是$this->uri->segment(3)

+0

敬畏的工作。然而,當我點擊第2頁時(我現在將限制降低到5,目前有7個結果,因此它生成了第2頁...它將用5替換ID 2(類別ID爲2)..而不是到第2頁?? ....第2頁應該看起來像這樣:論壇/類別/ 2/2 ...但相反,它是這樣做的:論壇/類別/ 5 – Peanut

+0

頁碼未在URL中表示 - 這是如果你每頁顯示10個結果,你會得到類別/ 0,category/10,category/20現在,/ 2/2被5取代,這可能是你的uri段設置,嘗試增加1。 – Kira