Page 1 of 1

PHP Pagination Class - What do YOU use?

Posted: Fri Dec 18, 2009 11:32 pm
by Griven
When dealing with large result sets, we typically need to paginate those results in order to keep things readable and give the user a good experience. Although PHP is a great language, and works with databases very well, it doesn't offer a neat pre-packaged method of pagination.

So, how do YOU go about it in your own projects? Is there a specific ready-made class out there that you use, or have you made one of your own?

Re: PHP Pagination Class - What do YOU use?

Posted: Fri Dec 18, 2009 11:38 pm
by pbs
I have created my own pagination function.

Code: Select all

 
function execute($sql)
{
    if ($sql!="")
    {
        $result = mysql_query($sql) or die(mysql_error());
        if ($result)
            return $result;
        else
            return false;
    }
}
function recordset($result)
{
    if ($result)
    {
        while($row = mysql_fetch_assoc($result))
            $data[] = $row;
    }
    return $data;
}
function pagingPN($sql, $page, $limit, $getvars, $class)
{
    if ($page == "")
        $page = 1;
    if ($limit == 0)
        $limit = 1;
    $tsql = $sql;
    $result = mysql_query($tsql) or die("Error: ".mysql_errno().":- ".mysql_error());
    $total = mysql_num_rows($result);
    $totnumpages = ceil($total/$limit);
    if ($offset < 0)
        $offset = 0;
    else          
        $offset = ($page - 1) * $limit;
    $sql = $sql. "  limit $offset, $limit"; 
    $rs = execute($sql);
    $res = recordset($rs);
    $serial_no = ($page - 1) * $limit;
    
    if ($total > 0)
    {
        $link .= "<font face='verdana' size='1'>Page: <strong>".$page."</strong>&nbsp;of&nbsp;<strong>".$totnumpages."</strong>&nbsp;&nbsp;&nbsp;Goto:&nbsp;</font>";
        
        if ($page > 1)
        {
            $link .= "<a href=".$_SERVER['PHP_SELF']."?page=1$getvars class='".$class."' title='Jump to First Page'><<</a>&nbsp;|&nbsp;";
            $prev = $page - 1;
            $link .= "<a href=".$_SERVER['PHP_SELF']."?page=".$prev."$getvars class='".$class."' title='Goto Previous Page'>Previous</a><span class='".$class."'>&nbsp;|&nbsp;</span>";
        }
        else
        {
            $link .= "<span class='".$class."' title='Jump to First Page'><<</span>&nbsp;|&nbsp;<span class='".$class."' title='Goto Previous Page'>Previous&nbsp;|&nbsp;</span>";
        }
        if ($page < $totnumpages)
        {
            $next = $page + 1;
            $link .= "<a href=".$_SERVER['PHP_SELF']."?page=".$next."$getvars class='".$class."' title='Goto Next Page'>Next</a>&nbsp;|&nbsp;";
            $link .= "<a href=".$_SERVER['PHP_SELF']."?page=".$totnumpages."$getvars class='".$class."' title='Jump to Last Page'>>></a>";
        }
        else
        {
            $link .= "<span class='".$class."' title='Goto Next Page'>Next</span>&nbsp;| <span class='".$class."' title='Jump to Last Page'>>></span>";
        }
    }       
    $retarr["sql"] = $sql;
    $retarr["records"] = $res;
    $retarr["serial_no"] = $_no;
    $retarr["link"] = $link;
    return $retarr;
}
 

Re: PHP Pagination Class - What do YOU use?

Posted: Sat Dec 19, 2009 12:47 pm
by Christopher
Search these forums for "pagination":

viewtopic.php?p=561007#p561007