Part of the EllisLab Network
   
 
A Better and more Flexible Paging Solution for CI
Posted: 04 November 2007 04:02 AM   [ Ignore ]  
Lab Assistant
Avatar
RankRank
Total Posts:  163
Joined  04-23-2007

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]

Profile
 
 
Posted: 04 November 2007 02:25 PM   [ Ignore ]   [ # 1 ]  
Research Assistant
RankRankRank
Total Posts:  337
Joined  09-20-2007

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 

Fix the Forums

Profile
 
 
Posted: 04 November 2007 10:52 PM   [ Ignore ]   [ # 2 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  163
Joined  04-23-2007

Wiki Page

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

 Signature 

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

Profile
 
 
Posted: 05 November 2007 12:15 AM   [ Ignore ]   [ # 3 ]  
Research Assistant
RankRankRank
Total Posts:  337
Joined  09-20-2007

Thanks !!!

smile

 Signature 

Fix the Forums

Profile
 
 
Posted: 05 November 2007 01:18 AM   [ Ignore ]   [ # 4 ]  
Grad Student
Rank
Total Posts:  53
Joined  08-03-2007

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

Please fix the url link.

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

Profile
 
 
Posted: 25 November 2007 09:39 AM   [ Ignore ]   [ # 5 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  163
Joined  04-23-2007

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]

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 60711 Total Logged-in Users: 17
Total Topics: 73163 Total Anonymous Users: 1
Total Replies: 394596 Total Guests: 347
Total Posts: 467759    
Members ( View Memberlist )
Active Members:    awptiazulcmbojackCrucialDark PreacherEthan Dunhaminparojacksonj04JoostVjtkendallLuci3nMgM WebmwmerzNachoredwizSabottix