Welcome Guest, Not a member yet? Register   Sign In
pagination "per_page" not working
#1

[eluser]M4rc0[/eluser]
Hi!

So i have my pagination working here, but i'm getting all the results in one page. When i click page 2 or next, it shows all the results again, it's not limiting 10 by 10 (as i want).

So i have 19 results to show, i would like page one to show 10 and page 2 to show the 9 left.
My page is showing 19 in page 1, and the same 19 in page 2.

Here's my code:
Code:
function all()
    {
        $this->load->library('pagination');
        $config['base_url'] = 'http://localhost/videoteks/index.php/movie/all/';
            $config['uri_segment'] = 3;        
            $config['num_links'] = 2;        
        $config['total_rows'] = $this->Movie_model->count_all('movies');
        $config['per_page'] = '10';
        $config['full_tag_open'] = '<div class="pagination">';
        $config['full_tag_close'] = '</div>';
        $config['cur_tag_open'] = '<span class="current">';
        $config['cur_tag_close'] = '</span>';
        $this->pagination->initialize($config);
        $data['pages'] = $this->pagination->create_links();
        $data['header'] = 'All Colection';
        $data['query'] = $this->Movie_model->get_all_movies();
        $this->template->write('pages',$data['pages']);
        $this->template->write_view('content','movie/browse',$data,TRUE);
        $this->template->render();        
    }

Am i missing something?

Also, if i leave all the $config in a configuration file, will I be able to overwrite $config['base_url'] in a different function from the controller?

All those $config will be the same, except the 'base_url' in case i want to use it in a different method, is it possible to do that way?
#2

[eluser]elvix[/eluser]
you need to pass a limit (per page value) and an offset (contained in uri segment 3) to your db function.

get the offset using $this->uri->segment(3,0), so that when there's no offset, the value passed is 0.
#3

[eluser]M4rc0[/eluser]
Awesome! Thanks elvix!!

Man i love this forum :3

I honestly think CI's documentation is the best out there, but how come they don't mention stuff like this? I'd never know we need limit and offset on the query.

Regarding my $config question, do you think i'd have any problems to store the $config in a configuration file expect the $config['base_url'] ? That's not possible, right?

Thanks!
#4

[eluser]elvix[/eluser]
yeah, you should store all that config in config/pagination.php. CI should load it up when you load the lib.

your base_url will change, so there's no point in storing it in the config, unless you want it as a default value (i think this would be rare, but depends on your app).

To be fair (and i don't mean to be condescending), limit and offset are pretty basic MySQL concepts and sorta fall outside the scope of the CI manual. CI's just glue that helps put things together -- you're still going to want to know your DB functions, PHP fundamentals, Apache/htacess/server stuff.

I'd recommend getting the O'Reilly SQL pocket guide (small and cheap and very useful). And use PHP's online manual a lot (php functions are very squirrely, even zen masters don't remember how all the functions work).
#5

[eluser]M4rc0[/eluser]
Yes, that's fair. It's important to know MySQL and PHP, i read php.net very often (the comments always have goodies too) when i look for a function or something in documentation.

I just meant that CI's manual *could* have a small note (like that red box) saying or reminding you need limit and offset. That's all. You will find a lot of basic things in the CI's manual as small reminder notes, so it felt like they forgot this one.

But okay, my bad for not knowing that.

I still think CI's manual is the best though :cheese:

Thanks elvix!




Theme © iAndrew 2016 - Forum software by © MyBB