CodeIgniter Forums
pagination problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: pagination problem (/showthread.php?tid=39852)



pagination problem - El Forum - 03-22-2011

[eluser]Unknown[/eluser]
I can't get pagination to work quite right. The userguide wasn't as helpful with this, so I went off this site (http://godbit.com/article/pagination-with-code-igniter), but the article is a bit old.

It seems to be mostly working, but the created paging links don't show the right current page. It is always stuck on 1. Any ideas? Thanks.

controller
Code:
public function display()
    {
        $this->load->model('seed_model');
        $this->load->library('pagination');
        $config['base_url'] = 'http://localhost/mc/index.php/browse/display/' . $this->uri->segment(3) . '/';
        $config['num_links'] = 2;
        $config['total_rows'] = $this->seed_model->total();
        $config['per_page'] = 5;
        $this->pagination->initialize($config);
echo $this->seed_model->total() . '<br />';
echo $config['base_url'];
        
        $sort = $this->uri->segment(3);
        if ($sort == 'popular')
        {
            $data['seeds'] = $this->seed_model->get_seeds('views', 'desc', $config['per_page'], $this->uri->segment(4));
        }
        else if ($sort == 'newest')
        {
            $data['seeds'] = $this->seed_model->get_seeds('date', 'desc', $config['per_page'], $this->uri->segment(4));
        }
        else if ($sort == 'oldest')
        {
            $data['seeds'] = $this->seed_model->get_seeds('date', 'asc', $config['per_page'], $this->uri->segment(4));
        }
        else
        {
            $data['seeds'] = $this->seed_model->get_seeds('views', 'desc', $config['per_page'], $this->uri->segment(4));
        }
        $this->load->view('h_view');
        $this->load->view('browse_view', $data);
        $this->load->view('f_view');
    }

view
Code:
&lt;?php echo anchor('browse/display/popular', 'Popular') . '   '; ?&gt;
    &lt;?php echo anchor('browse/display/newest', 'Newest') . '   '; ?&gt;
    &lt;?php echo anchor('browse/display/oldest', 'Oldest') . '   '; ?&gt;

    <p>
        &lt;?php
            foreach ($seeds as $row)
            {
                echo anchor("seed/view/$row->seed", $row->seed) . '<br />';
                echo $row->desc . '<br />';
                echo $row->views . '<br />';
                $this->load->helper('date');
                $unix = mysql_to_unix($row->date);
                echo mdate("%Y/%m/%d", $unix) . '<br /><br />';
            }
        ?&gt;
    </p>
    <p>
        &lt;?php echo $this->pagination->create_links(); ?&gt;
    </p>



pagination problem - El Forum - 03-22-2011

[eluser]neckbone[/eluser]
I think you need to add:

Code:
$config['uri_segment'] = 4;

I believe the default is 3, but since you're passing an extra parameter (sort), the pagination link would be the fourth segment.


pagination problem - El Forum - 03-22-2011

[eluser]Unknown[/eluser]
You've saved my neckbone. It works perfectly now. Thanks Big Grin


pagination problem - El Forum - 03-22-2011

[eluser]neckbone[/eluser]
Glad I could help!