Codeigniter分页无法正常工作

时间:2016-09-27 12:08:38

标签: php codeigniter

我的控制器: -

public function index($id,$page=0) {

    $config = array();
    $config["base_url"] = base_url() . "index.php/categorypage/index/".$id."/";
    $total_row = $this->Categorymodel->record_count($id);
    $config["total_rows"] = $total_row;
    $config["per_page"] = 20;
    $config['use_page_numbers'] = TRUE;
    $config['num_links'] = $total_row;
    $config['cur_tag_open'] = '&nbsp;<a class="current">';
    $config['cur_tag_close'] = '</a>';
    $config['next_link'] = 'Next';
    $config['prev_link'] = 'Previous';
    $this->pagination->initialize($config);
    $page = $this->uri->segment(3) ;

    $data["results"] = $this->Categorymodel->fetch_data($config["per_page"], $page,$id);
    $str_links = $this->pagination->create_links();
    $data["links"] = explode('&nbsp;',$str_links );

    $this->load->view('frontend/template/other_head');
    $this->load->view('frontend/template/category_nav');
    $this->load->view('frontend/category_content',$data);
    $this->load->view('frontend/template/footer');
}

我的模特

class Categorymodel extends CI_Model {

    // Count all record of table "contact_info" in database.
    public function record_count($catid) {

        //$this->db->where('cat_id',$catid);
         $query = $this->db->get("tbl_product");
        return $query->num_rows();

    }

    // Fetch data according to per_page limit.
    public function fetch_data($limit,$page, $pageid) {

        $this->db->limit(12,$limit);
        $this->db->where('cat_id', $pageid);
        $query = $this->db->get("tbl_product");
        //echo $this->db->last_query();exit;
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }

            return $data;
        }
        return false;
    }
}

我无法获得&#34; Offset&#34;值,并且分页URL未获取其他值。请帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

好像你有错误:

$this->db->limit(12,$limit);

应限制($ limit,$ page)

答案 1 :(得分:0)

您可以告诉CI您用于页码的段。

$config['uri_segment'] = 4;

以类似方式计算查询的偏移量:

    $offset = (
        $this->uri->segment($config['uri_segment']) - 1
    ) * $config['per_page'];
    $offset < 0 ? $offset = 0 : '';

就像之前所说的那样,$this->db->limit($limit, $offset)中的第二个参数是偏移的。