Codeigniter分页 - 配置(SQL语句)

时间:2012-07-14 16:16:35

标签: codeigniter pagination

我在显示分页时遇到了一些问题。即使只有一个查询,也会出现分页。我知道这是我在 $ Pagesquery 上的sql语句缺乏的。

这是我的控制器

    //This will only display local packages
    public function displayLocalPackages($page=0,$offset=6,$search=''){

        $search = $this->input->post('search');     
        $row = $this->m_tour->getAllLocalTours($offset,$page,$search);
        $data['usersTable'] = $row->result();
        $data['pagination'] = $this->m_tour->getLocTourPages();
        $data['offset'] = $offset;
        $data['page'] = $page;
        $data['search'] = $search;
        $data['title'] = 'Local Packages';
        $this->load->view('vdisplaylocaltours',$data);

    }

这是我的模型功能

    //will get the number of local pages

    public function getLocTourPages(){
    $Pagesquery = $this->db->get('tb_package');
            $config['base_url'] = site_url('ctour/displayLocalPackages');
     $config['total_rows']= $Pagesquery->num_rows();
            $config['per_page'] = 6;
            $config['first_link'] = 'First'; 
            $config['prev_link'] = 'Previous';
            $config['next_link'] = 'Next';
            $config['last_link'] = 'Last';  
            $this->pagination->initialize($config);
    return $this->pagination->create_links();   

    }   

public function getAllLocalTours($offset,$count,$search){

        #This will only occur if $search is NOT empty
        if ($search!=''){ 
                $this->db->where("(cat_id LIKE '%1%' AND pack_name LIKE '%$search%')");
                   $this->db->where('pack_status','available');
         }

        #cat_id corresponds with the local packages 
          $this->db->where('cat_id',1);
          $this->db->where('pack_status','available');
          $this->db->order_by('pack_name', 'desc'); 
          $this->db->order_by('pack_id', 'desc'); 

         #Query on the table package
          $LocalQuery = $this->db->get('tb_package',$offset,$count);

            if($LocalQuery->num_rows > 0){
                return $LocalQuery;
            }
            else{
            return FALSE;   
        }           
    }

2 个答案:

答案 0 :(得分:0)

总行数应为

$config['total_rows']= $Pagesquery->num_rows()/6; //per page is given as 6 in your code

答案 1 :(得分:0)

看着我的代码时,我意识到我只需要在'getLocTourPages'函数中输入相同的sql查询:P

$这 - > DB-化合物其中( 'CAT_ID',1);  $这 - > DB-化合物其中( 'pack_status', '可');