如果按字段nama订购,则无法显示所有项目

时间:2015-09-29 05:59:17

标签: codeigniter

我在Codeigniter中有模型代码功能,如下所示,如果我按RAND()排序可以显示所有项目但是如果按字段名称排序,如part_no则无法显示所有项目,在下一页同样显示第1页。 / p>

模型

function listProductPerCategory($id, $limit, $start){
    $this->db->select('
        category.id, category.category, product.id, product.id_category,
        product.name, product.picture, product.description, product.permalink, product.part_no
    ');

    $this->db->join('category', 'product.id_category = category.id');
    $this->db->where('product.id_category', $id);
    $this->db->order_by('product.part_no');
    $this->db->limit($limit, $start);
    $query = $this->db->get($this->tableProduct);

    return $query->result();
}

控制器

function listcategory($page = NULL) {
    $id = $this->input->get('list');
    $data['categoryHead'] = $this->M_home->listCategory();
    $this->load->view('frontend/template/header', $data);
    $data['productsLast'] = $this->M_home->listProductLast();
    $data['productsLast2'] = $this->M_home->listProductLast2();
    $data['category'] = $this->M_home->listCategory();

    //paging
    $config['base_url']         = base_url('categoryproduct'.'?'.http_build_query($_GET));
    $config['total_rows']       = $this->M_home->listProductPerCategory_num_rows($id);
    $config['per_page']         = $per_page = 12;
    $config['uri_segment']      = 2;
    $config['first_link']       = 'First';
    $config['last_link']        = 'Last';
    $config['next_link']        = 'Next';
    $config['prev_link']        = 'Prev';
    $config['page_query_string'] = TRUE;
    //end paging

    $this->pagination->initialize($config);
    $data['paging'] = $this->pagination->create_links();
    $page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;

    $data['listCategory'] = $this->M_home->listProductPerCategory($id, $per_page, $page);
    $this->load->view('frontend/category', $data);

    $this->load->view('frontend/template/footer');
}

2 个答案:

答案 0 :(得分:0)

你明白这一点  在SQL LIMIT中,start是记录的编号而不是PAGE。

但是从你传递给listProductPerCategory的是页码 因此,从第1页到第2页,下一页只有一个记录移动而不是12个记录。

所以你需要修复函数listProductPerCategory(不要忘记更改变量名,这可能让你自己迷惑,更改$ start - > $ page)

所以你应该得到如下

function listProductPerCategory($id, $limit, $page){

if($page < 1){
  $page = 1;
}

if($page > 1){
  $start =  $limit * $page;
}else{
  $start = 0;
}



        $this->db->select('
            category.id, category.category, product.id, product.id_category,
            product.name, product.picture, product.description, product.permalink, product.part_no
        ');
        $this->db->join('category', 'product.id_category = category.id');
        $this->db->where('product.id_category', $id);
        $this->db->order_by('product.part_no');
        $this->db->limit($limit, $start);
        $query = $this->db->get($this->tableProduct);
        return $query->result();
    }

答案 1 :(得分:0)

控制器 -

试试这个 -

function listcategory($page = 0){
    $id = $this->input->get('list');
    $data['categoryHead'] = $this->M_home->listCategory();
    $this->load->view('frontend/template/header', $data);
    $data['productsLast'] = $this->M_home->listProductLast();
    $data['productsLast2'] = $this->M_home->listProductLast2();
    $data['category'] = $this->M_home->listCategory();
    //paging
    $config['base_url']         = base_url('categoryproduct'.'?'.http_build_query($_GET));
    $config['total_rows']       = $this->M_home->listProductPerCategory_num_rows($id);
    $config['per_page']         = $per_page = 12;
    $config['num_links'] = 2;
    $config['uri_segment']      = 3;
    $config['first_link']       = 'First';
    $config['last_link']        = 'Last';
    $config['next_link']        = 'Next';
    $config['prev_link']        = 'Prev';
    $config['page_query_string'] = TRUE;
    //end paging
    $this->pagination->initialize($config);
    $data['paging'] = $this->pagination->create_links();
    $data['page'] = $page;
//  $page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
    $data['listCategory'] = $this->M_home->listProductPerCategory($id, $per_page, $page);
    $this->load->view('frontend/category', $data);
    $this->load->view('frontend/template/footer');

您的模型中的条件排序错误。

试试这个 -

$ this-&gt; order_by('title','ASC或DESC或RANDOM');