Welcome Guest, Not a member yet? Register   Sign In
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


Messages In This Thread
A Better and more Flexible Paging Solution for CI - by El Forum - 11-04-2007, 02:02 AM
A Better and more Flexible Paging Solution for CI - by El Forum - 11-04-2007, 01:25 PM
A Better and more Flexible Paging Solution for CI - by El Forum - 11-04-2007, 09:52 PM
A Better and more Flexible Paging Solution for CI - by El Forum - 11-04-2007, 11:15 PM
A Better and more Flexible Paging Solution for CI - by El Forum - 11-05-2007, 12:18 AM
A Better and more Flexible Paging Solution for CI - by El Forum - 11-25-2007, 08:39 AM
A Better and more Flexible Paging Solution for CI - by El Forum - 12-06-2008, 04:30 AM
A Better and more Flexible Paging Solution for CI - by El Forum - 12-28-2008, 09:26 PM
A Better and more Flexible Paging Solution for CI - by El Forum - 12-29-2008, 01:11 AM



Theme © iAndrew 2016 - Forum software by © MyBB