Welcome Guest, Not a member yet? Register   Sign In
Pagination question
#1

[eluser]artur z[/eluser]
Hi,
I recently tried to implement CI pagination into my project. I would like to think that i know my way around CI, but this drives me mad.
Here's my problem

1. I have a Controller called test (it's just a dummy controller)

Code:
function paginate()
        {
            $config['base_url'] = base_url().'test/paginate';
            $config['total_rows'] = $this->db->count_all('receipts');
            $config['per_page'] = '1';
            $config['uri_segment'] = '3';
            $config['num_links'] = 1;
            /* Initialize config */
            $this->pagination->initialize($config);

            $data['query'] = $this->db->get('receipts', $config['per_page'], $this->uri->segment(3));
            $data['body'] = 'pag';
            $this->load->vars($data);
            $this->load->view('container');


        }

2. And a view called pag.

Code:
<?php foreach($query->result() as $value):?>
<?=$value->id?>
<?php endforeach;?>
<?= $this->pagination->create_links(); ?>


It's pretty simple i know. I set $config['per_page'] to '1' just to check the number of records returned by $data['query']. So far everything looks fine. I have 5 records in my db and pagination gives me five pages.

3. The problem is that the first page contains a record with the id of 42, the second pagination link jumps directly to a record with id 44 instead of 43. My records have the following id's 42,43,44,45,46. Page number five returns no record at all. Can somebody please explain it to me?
#2

[eluser]tonanbarbarian[/eluser]
Is the record with id 43 displayed on one of the other pages? I am guessing that it is.
The issue here is that you are not specifying any ordering
Assuming you are using MySQL then the ordering may change each time you update the table because of the way that MySQL stores its data.
If you do not enter any ordering then MySQL just grabs it from the order in the table.
Pretty sure that is what is happening here
If you want it in ID order then tell it that and I am sure you will find 43 on page 2 as expected
#3

[eluser]artur z[/eluser]
[quote author="tonanbarbarian" date="1201574050"]Is the record with id 43 displayed on one of the other pages? I am guessing that it is.
The issue here is that you are not specifying any ordering
Assuming you are using MySQL then the ordering may change each time you update the table because of the way that MySQL stores its data.
If you do not enter any ordering then MySQL just grabs it from the order in the table.
Pretty sure that is what is happening here
If you want it in ID order then tell it that and I am sure you will find 43 on page 2 as expected[/quote]

Quick and dirty… I usually order my results in some way, but it doesn't solve the issue here.
I'm still missing one record, it looks like something's wrong with my offset?


Code:
/* Order by id ASC */
            $this->db->orderby('id', 'desc');
            /* Run query */
            $data['query'] = $this->db->get('receipts', $config['per_page'], $this->uri->segment(3, 0));
#4

[eluser]artur z[/eluser]
Last page does not return a record.
#5

[eluser]artur z[/eluser]
I tried almost everything … still nothing. The number of pages/records seem to be correct five pages five records.
But it just doesn't work, when i klick on page 1 i get the wrong record (record 2) and when i click on the last record (record 5)
i get nothing ?! Help – please!
#6

[eluser]tonanbarbarian[/eluser]
supply some more code so we can see what if anything you are doing wrong
provide whatever is being used in the controller, any model code and anything else that is relevant and might be called
#7

[eluser]artur z[/eluser]
Hi,
I don't use a model here, i created this controller in my sandbox enviroment to see if
i can get a correct pagination. My problem, as i stated above is:

I get the correct count for records in mysql – 5 records
I get the correct number of pagelinks when i set 'per_page' to 1 record
When i click on pagelink 1 or 5 i don't get a record.

See below for my controller / view. Maybe i'm a complete noob, but this
should work – or?

Controller: test.php

Code:
<?php

    class Test extends Controller
    {
        function test()
        {
            parent::Controller();
            $this->load->library('pagination');
        }

        function index()
        {

        }


        function paginate()
        {
            
            /* base url is http://localhost:8888/culinari/ */
            $config['base_url'] = base_url().'test/paginate';
            $config['total_rows'] = $this->db->count_all('receipts');
            $config['per_page'] = '1';
            $config['uri_segment'] = '3';
            /* Sanity check, return only one result per page*/
            $config['num_links'] = 1;

            /* Initialize config */
            $this->pagination->initialize($config);

            /* Order by id, ASC */
            $this->db->orderby('id', 'asc');
            /* Run query */
            $data['query'] = $this->db->get('receipts', $config['per_page'], $this->uri->segment(3, 0));
            /* Take us to our view */
            $this->load->view('test', $data);


        }

    }

?>

View test.php

Code:
<?php foreach($query->result() as $value):?>
<strong>id:&lt;?=$value->id?&gt;</strong>  title:&lt;?=$value->seotitle?&gt;<br />
&lt;?php endforeach;?&gt;
&lt;?= $this->pagination->create_links(); ?&gt;
#8

[eluser]tonanbarbarian[/eluser]
i cannot see anything immediately incorrect
however i recommend turning on profiling so that you can see what data is being sent and what queries are being run
at the start of paginate add the following
Code:
$this->output->enable_profiler(true);
this may help you to determine what the problem is
i would also suggest trying to set the per_page to 2 and see if it still has problems
#9

[eluser]artur z[/eluser]
Thanks,
It seems that the offset needs a small push forward (backward actuallly) to work.
The code below works fine:

Code:
function paginate()
        {
            
            /* base url is http://localhost:8888/culinari/ */
            $config['base_url'] = base_url().'test/paginate';
            $config['total_rows'] = $this->db->count_all('receipts');
            $config['per_page'] = '1';
            $config['uri_segment'] = '3';
            /* Sanity check, return only one result per page*/
            $config['num_links'] = '0';

            /* Initialize config */
            $this->pagination->initialize($config);

            /* Correct our offset */
            if($this->uri->segment(3) > '0'):
            $offset=$this->uri->segment(3) -1;
            elseif(!$this->uri->segment(3)):
            $offset=0;
            endif;

            /* Order by id, ASC */
            $this->db->orderby('id', 'asc');
            /* Run query */
            $data['query'] = $this->db->get('receipts', $config['per_page'], $offset);
            /* Take us to our view */
            $this->load->view('test', $data);


        }

    }




Theme © iAndrew 2016 - Forum software by © MyBB