• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A Better and more Flexible Paging Solution for CI

#1
[eluser]paulopmx[/eluser]
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.

Code:
<?

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;    
}

?&gt;

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:

Code:
$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:

Code:
$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:

Code:
$data['pg'] = $paging;

You can then use it like this.

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

                                    <a href="&lt;?=site_url('models/page/'.$pg['next'])?&gt;" title="Go to Next Page" class="next"><span>&rsaquo;</span></a>
                                    <a href="&lt;?=site_url('models/page/'.$pg['last'])?&gt;" 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

#2
[eluser]llbbl[/eluser]
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.

#3
[eluser]paulopmx[/eluser]
Wiki Page

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

#4
[eluser]llbbl[/eluser]
Thanks !!!

Smile

#5
[eluser]bijon[/eluser]
Quote:http://codeigniter.com/wiki/Flexible_Paging_Plugin/
Please fix the url link.

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

#6
[eluser]paulopmx[/eluser]
Since this solution is a function, you can use it on other frameworks or projects as well.

#7
[eluser]Amit Patel[/eluser]
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.

#8
[eluser]Computerzworld[/eluser]
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.

#9
[eluser]xwero[/eluser]
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,
Code:
SELECT CEILING(COUNT(*)/?) page_count FROM table


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.