CodeIgniter Forums
No results when paginate (WHERE something=$something) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: No results when paginate (WHERE something=$something) (/thread-1836.html)



No results when paginate (WHERE something=$something) - El Forum - 06-29-2007

[eluser]Gabriel P[/eluser]
Hello,

I wish to grab some articles from db WHERE parentid=$catid. The thing is i want to paginate them. So, i believe i did not write the correct code.

Here is my function from the controller:
Code:
function showArtByCat($catid)
    {  
        $data['base_url'] = $this->config->item('base_url');
        
        $data['cats'] = $this->Catmodel->getCats();
        $data['subcats'] = $this->Catmodel->getSubCats($catid);
        $data['catname'] = $this->Catmodel->getCatName($catid);
        
        
        $query = $this->db->query("SELECT COUNT(*) AS count FROM articles WHERE (parentid=".$catid.")");
        $row = $query->row();
        $total_rows = $row->count;

        $per_page = 10;
        $this->db->use_table('articles');
        $this->db->limit($per_page);
        
        if ($this->uri->segment(3) !== FALSE)
        {
            $this->db->offset($this->uri->segment(3));
        }          
        $data['query'] = $this->db->get();

        $config['base_url'] = 'my_base_url';
        $config['total_rows'] = $total_rows;
        $config['per_page'] = $per_page;

        $this->pagination->initialize($config);
        $data['paginate'] = $this->pagination->create_links();
        
        $this->load->view('front/cat', $data);
    }

Could somebody help me? I think i`m tired and i can`t figure it out.

Thanks,
Gabriel


No results when paginate (WHERE something=$something) - El Forum - 07-04-2007

[eluser]Phil Sturgeon[/eluser]
Well you are not accepting the offset value in the controller if I understand you correctly. You are referencing segment 3 but it seems a little odd.

Here's how mine works:

Code:
function index($limit = 25, $offset = 0)
    {    
        $this->load->library('pagination');
        $this->load->model('test_model');
        
        $data['list'] = $this->test_model->getList($limit, $offset);
        
        // Pagination code
        $config['base_url'] = '/page/list/'.$limit.'/';
        $config['total_rows'] = $this->db->count_all('Table');
        $config['per_page'] = $limit;
        $config['uri_segment'] = 4;
        
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
                
        //... make page

    }

Yours would be no different, simply add in your count with a WHERE and add an extra $catID to the start of the controller param list.