EllisLab text mark
Advanced Search
     
A Better and more Flexible Paging Solution for CI
Posted: 04 November 2007 04:02 AM
Avatar
Joined: 2007-04-23
167 posts

Want a better paging solution than what CI provides.

Paging can displayed in number of ways, as links, as a select box like in phpmyadmin, or maybe even as links with a javascript attach for ajax purposes -> which I do frequently.

Then I realize that I don’t need CI to generate the links for me, but I just need the values needed to form the paging.

So I made the following code which you can insert in a CodeIgniter plugin.

<?

function paging($page,$rp,$total,$limit)
{
        $limit 
-= 1;

        
$mid floor($limit/2);
        
        if (
$total>$rp)
            
$numpages ceil($total/$rp);
        else
            
$numpages 1;
        
        if (
$page>$numpages$page $numpages;

            
$npage $page;

        while ((
$npage-1)>0&&$npage>($page-$mid)&&($npage>0))
            
$npage -= 1;
        
        
$lastpage $npage $limit;
        
        if (
$lastpage>$numpages
            
{
            $npage 
$numpages $limit 1;
            if (
$npage<0$npage 1;
            
$lastpage $npage $limit;
            if (
$lastpage>$numpages$lastpage $numpages;
            
}
        
        
while (($lastpage-$npage)<$limit$npage -= 1;        
        
        if (
$npage<1$npage 1;
            
        
$paging['first'1;
        if (
$page>1$paging['prev'$page 1; else $paging['prev'1;
        
$paging['start'$npage;
        
$paging['end'$lastpage;
        
$paging['page'$page;            
        if ((
$page+1)<$numpages$paging['next'$page 1; else $paging['next'$numpages;
        
$paging['last'$numpages;
        
$paging['total'$total;
        
$paging['iend'$page $rp;
        
$paging['istart'= ($page $rp) - $rp 1;
        
        if ((
$page $rp)>$total$paging['iend'$total;
        
        return 
$paging;    
}

?> 

You have to pass 4 parameters:

$page is the current page, this is the number of the current page and not the limit and start values usually used in sql queries. you can convert it into a start for sql query like so:

$start = (($page-1) * $rp); 

$rp is results per page.
$total is the total number of results.
$limit is the number of page values you want to display, this will allow you limit the number of pages to display if the result of your query results in too many pages. Like in google search results, where the current page is also displayed in the middle.

You can then pass it to a variable, and it will return an array. with the following values:

$paging paging($page,$rp,$total,$limit); 

$paging[‘start’] = starting page value.
$paging[‘end’] = ending page value
$paging[‘last’] = last page

$paging[‘total’] = number of results
$paging[‘istart’] = starting result number for current page
$paging[‘iend’] = ending result number for current page

The last three values can be use for something like: Displaying 1 to 10 of 100 items.

In CI you can pass it on to a $data which you can use for views like so:

$data['pg'$paging

You can then use it like this.

<div class="pages">
                                    <
div class="inside">
                                    <
a href="<?=site_url('models/page/'.$pg['first'])?>" title="Go to First Page" class="first"><span>&laquo;</span></a>
                                    <
a href="<?=site_url('models/page/'.$pg['prev'])?>" title="Go to Previous Page" class="prev"><span>&lsaquo;</span></a>
                                    
                                    
<? for ($i=$pg['start'];$i<=$pg['end'];$i++) 
                                       
if ($i==$pg['page']$current 'current'; else $current="";
                                    
?>
                                    
                                    
<a href="<?=site_url("models/page/$i")?>" title="Go to Page <?=$i?>class="page <?=$current?>"><span><?=$i?></span></a>
                                    
                                    
<? } ?>

                                    
<a href="<?=site_url('models/page/'.$pg['next'])?>" title="Go to Next Page" class="next"><span>&rsaquo;</span></a>
                                    <
a href="<?=site_url('models/page/'.$pg['last'])?>" title="Go to Last Page" class="last"><span>&raquo;</span></a>
                                    </
div>
                            </
div

This is just a simple links example. As I said you can get really creative on how you want to display your paging. You can simply change it to a select box.

You can use it as is or modify it, but give me some credit if you are going to include it on your own framework or something.

Have fun and let me know how you used it.

Paulo P. Marinas

 Signature 

A Better and more Flexible Paging Solution for CI
Automatic config[base_url]

 
Posted: 04 November 2007 03:25 PM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2007-09-20
346 posts

Make a wiki page about this will u?

It is easier to organize all user submitted code in the wiki than it is in the forums.

 Signature 

Twitter

 
Posted: 04 November 2007 11:52 PM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2007-04-23
167 posts

Wiki Page

http://codeigniter.com/wiki/Flexible_Paging_Plugin/

 Signature 

A Better and more Flexible Paging Solution for CI
Automatic config[base_url]

 
Posted: 05 November 2007 01:15 AM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2007-09-20
346 posts

Thanks !!!

smile

 Signature 

Twitter

 
Posted: 05 November 2007 02:18 AM   [ # 4 ]   [ Rating: 0 ]
Joined: 2007-08-03
52 posts

http://codeigniter.com/wiki/Flexible_Paging_Plugin/

Please fix the url link.

However it’s a nice work. I will try to use it.

 
Posted: 25 November 2007 10:39 AM   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2007-04-23
167 posts

Since this solution is a function, you can use it on other frameworks or projects as well.

 Signature 

A Better and more Flexible Paging Solution for CI
Automatic config[base_url]

 
Posted: 06 December 2008 06:30 AM   [ # 6 ]   [ Rating: 0 ]
Joined: 2008-10-01
23 posts

Hi,

As i used above code but i exactly don’t getting how to pass the parameters in paging function can you give me small example for controller and model how to call this function with above four parameters dynamically.

 Signature 

Thanks and regards,
—-Amit Patel

 
Posted: 28 December 2008 11:26 PM   [ # 7 ]   [ Rating: 0 ]
Joined: 2008-04-18
171 posts

hi…
seems to be better paging plugin but I want to know what exactly i have to write in model in order to make this code running? Please help me. Thanks in advance.

 
Posted: 29 December 2008 03:11 AM   [ # 8 ]   [ Rating: 0 ]
Joined: 2006-07-14
4237 posts

A thought : why does the paging function need to know how many items the page has to contain? If it knows the number of pages instead of the total results it doesn’t need to calculate the number of page.

In mysql this is, in the simplest form,

SELECT CEILING(COUNT(*)/?) page_count FROM table