【问题标题】:CodeIgniter Pagination - Page links acting weirdCodeIgniter Pagination - 页面链接表现怪异
【发布时间】:2013-01-12 20:04:20
【问题描述】:

好的,我在过去的一个小时里一直在做这件事,我这辈子都想不通。 它的页数正确..就像我现在应该有两页。我现在将它限制在 5 并且有 7 个结果。但是,当我单击第 2 页时,它不会显示其他两个帖子。 现在,我的论坛网址是这样的: forum/category/2(论坛控制器,类别方法,2为类别ID) 我认为这些页面如下所示:第 1 页的 forum/category/2/1 和第 2 页的 forum/category/2/2 等等。但是,第 2 页给了我这个 URL:

forum/category/5... 哪个类别 ID 5 中没有主题.. 但它应该指向类别 ID 2 的第二页!所以我不确定......它可能会显示 5,因为它每页只显示 5?不知道它是如何工作的,但这是我的以下代码:

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'] = 5;
    $config['uri_segment'] = 4;

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

    $page = ($this->uri->segment($config['uri_segment'])) ? $this->uri->segment($config['uri_segment']) : 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();
}

这是我在该控制器中使用的函数:

//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();
}

【问题讨论】:

标签: php codeigniter pagination


【解决方案1】:

$config['uri_segment'] 被 CI 用来确定当前页面,而不是生成链接。

试试这个:

$config['base_url'] = site_url('forum/category/' . $id);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    • 2013-11-17
    • 1970-01-01
    • 2012-05-14
    • 1970-01-01
    • 2014-02-03
    • 1970-01-01
    相关资源
    最近更新 更多