CodeIgniter Forums
Pagination class - 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 class (/showthread.php?tid=19457)



Pagination class - El Forum - 06-08-2009

[eluser]Fr3aked0ut[/eluser]
Hey all, I am having a problem with the pagination in CI.

Here is my code:
Code:
function index( $page = "" )
    {
// Some code...
            if( !isset($page) OR $page == 0 OR $page < 5 )
            {
                $page = 5;
            }
            $from = $page - 5;
            $until = $page;
            $pquery = $this->db->query("SELECT * FROM dev_posts ORDER BY id DESC LIMIT $from,$until");
            foreach ($pquery->result() as $posts)
            {            
                $content .= <<&lt;HTML
SOME HTML            
HTML;
            }
            
$this-&gt;load->library('pagination');
$config['base_url'] = site_url('pages/index');
$qu = $this->db->query('SELECT COUNT(*) AS total FROM dev_posts');
$total = $qu->row();
$config['total_rows'] = $total->total;
$config['per_page'] = 5;
$config['full_tag_open'] = '<p>';
$config['full_tag_close'] = '</p>';

$this->pagination->initialize($config);

$content .= $this->pagination->create_links();
        
        $this->output->set_output($content);
    }

The problem is that it is always select the same data from the database..

What should I do? Thanks in advance.


Pagination class - El Forum - 06-08-2009

[eluser]TheFuzzy0ne[/eluser]
I don't think $until should be quite so dynamic. Usually it's a hard coded value that defines how many results should be pulled from the database. It shouldn't need to change for each request.

It might benefit you to add $this->output->enable_profiler(TRUE); in your controller method, then you will be able to see what's actually being fed into your query.


Pagination class - El Forum - 06-08-2009

[eluser]Fr3aked0ut[/eluser]
Huh? :\

It should be quite dynamic.. What you just said is that each page should pull another number of results - But you're wrong. Each page should pull the same number of results, just older results.

What does $this->output->enable_profiler(TRUE) do and how it'll help me?


Pagination class - El Forum - 06-08-2009

[eluser]TheFuzzy0ne[/eluser]
OK, let's start from the beginning.

Please could you explain what the whole 'if( !isset($page) OR $page == 0 OR $page < 5 )' is all about? I can see what it does, but I've no idea why.

Also, please mention the maximum number of results you're wanting to get from the database per request.


Pagination class - El Forum - 06-08-2009

[eluser]Fr3aked0ut[/eluser]
It's for security, if someone will enter 0 or something lower than 5 in the URL, it will automatically change to 5.

I want to get 5 results from the database on each page.

Thanks.


Pagination class - El Forum - 06-08-2009

[eluser]TheFuzzy0ne[/eluser]
OK, so your query needs to be something along these lines:

Code:
$pquery = $this->db->query("SELECT * FROM dev_posts ORDER BY id DESC LIMIT $from, 5");

With your current method, the number of results returned will match the page number. Page 100 will show up to 100 results.

At the top of your method, put this line of code:
Code:
$this->output->enable_profiler(TRUE);

When you access that controller next, you'll get a nice display at the bottom of the page outlining database queries and other useful information. You can then check see the values being passed in to the query, and ensure that they are what you were expecting.


Pagination class - El Forum - 06-08-2009

[eluser]n0xie[/eluser]
Code:
/**
* example from a portfolio page
*/

// controller: portfolio.php
class Portfolio extends Controller {

    function Portfolio(){
        parent::Controller();

    }

    function index()
    {
        $this->page()
    }

    function page($offset=0)
    {
        $this->load->library('pagination');
        $this->load->model('portfolio_model');
        
        if(! is_numeric($offset))
        {
            $offset = 0;
        }
        $limit = 5;
        $totalrows = $this->portfolio_model->count();

        if ($totalrows <= $offset)
        {
            $offset = $totalrows - $limit;
        }
        
        $config['base_url'] = base_url().'portfolio/page/';
        $config['total_rows'] = $totalrows;
        $config['per_page'] = $limit;
        $config['full_tag_open'] = '<p style="text-align:center">';
        $config['full_tag_close'] = '</p>';
        $config['next_link'] = '»';
        $config['prev_link'] = '«';

        $this->pagination->initialize($config);

        $section['content'] = $this->portfolio_model->get_portfolio($limit, $offset);
                $this->load->view('portfolio_view',$section, TRUE);
    }
}

// model: portfolio_model.php
class Portfolio_model extends Model
{
    function Portfolio_model ()
    {
        parent::Model();
    }

    function get_portfolio($limit=5,$offset=0)
    {
        $this->db->order_by('id','desc');
        $query = $this->db->get('portfolio',$limit, $offset);
        if ($query->num_rows() > 0)
        {
            return $query->result();
        } else {
            return FALSE;
        }
    }

    function count()
    {
        $query = $this->db->query('SELECT COUNT(*) AS total FROM portfolio');
        return $query->row()->total;
    }

//view: portfolio_view.php
<h1>Portfolio</h1>

&lt;?php foreach ($content as $row) { ?&gt;
// do some html formatting here
&lt;?php } ?&gt;

&lt;?php echo  $this->pagination->create_links(); ?&gt;

I removed a custom helper function to check if the $offset was indeed a real integer, as well as the checks if the query returned false, but it should work unless I made a typo (had to adjust a few things since I use more 'magic'). Good luck.


Pagination class - El Forum - 06-08-2009

[eluser]Fr3aked0ut[/eluser]
I'll check that out, my server does not work now so I can't check it now... But when it will I'll check and notify you.

Thanks alot!