Codeigniter分页链接ID后出错

时间:2015-04-14 18:53:13

标签: codeigniter pagination

抱歉,如果我的问题重复,我的codeigniter分页有问题。我的网址是:http://mysite/news/category/26/(page数字),我只有5个数据,页面限制为10,但生成的链接在第3页上有效,我相信这是因为id / 26 /

这是我的模特:

function categpry($id){
    $string_query = "SELECT * FROM news n JOIN category c ON n.category_id = c.id_category AND n.category_id = $id ORDER BY n.category_id DESC";
    $query = $this->db->query($string_query);
    $config['base_url'] = site_url('news/category/' . $id);
    $config['total_rows'] = $query->num_rows();
    $config['per_page'] = '10';
    $num = $config['per_page'];
    $offset = $this->uri->segment(4);
    $offset = ( ! is_numeric($offset) || $offset < 1) ? 0 : $offset;
    if(empty($offset)){
        $offset = 0;
    }
    $this->pagination->initialize($config);
    $news = $this->db->query($string_query." limit $offset,$num");
    return $nes;
}

生成的链接是1 - 2 - 3 ,它应该没有链接,因为我只有5个数据
请帮帮我,谢谢

1 个答案:

答案 0 :(得分:1)

最后我通过更改脚本来修复它:

function category($id){
     $string_query = "SELECT * FROM news n JOIN category c ON n.category_id = c.id_category AND n.category_id = $id ORDER BY n.category_id DESC";
    $query                      = $this->db->query($string_query);
    $config['base_url']         = site_url('news/category/' . $id);
    $config['total_rows']       = $query->num_rows();
    $limit                      = $config["per_page"] = 10;
    $config["uri_segment"]      = 4;
    $config["use_page_numbers"] = TRUE;
    $this->pagination->initialize($config);
    if($this->uri->segment(4))
        $page = ($this->uri->segment(4)-1)*$limit;
    else
        $page = 1;
    $news= $this->db->query($string_query." limit $page,$limit");
    return $news;
}

谢谢