PHP function for pagination/paging

Found this function. Not tested yet but maybe useful in the future.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
// example variable
$sql_limit = (isset($_GET['limit'])) ? $_GET['limit'] : 0;
 
function navigation_links($curr_limit, $num_records, $limit_val, $limit_var = "limit", $next = "next >", $prev = "< prev", $seperator = "|") {
    // rebuild query string
    if (!empty($_SERVER['QUERY_STRING'])) {
        $parts = explode("&", $_SERVER['QUERY_STRING']);
        $newParts = array();
        foreach ($parts as $val) {
            if (stristr($val, $limit_var) == false) array_push($newParts, $val);
        }
        $qs = (count($newParts) > 0) ? "&".implode("&", $newParts) : "";
    } else {
        $qs = "";
    }
    $navi = "";
    if ($curr_limit > 0) {
        $navi .= "<a href="".$_SERVER['PHP_SELF']."?".$limit_var. "=".($curr_limit-$limit_val).$qs."">".$prev."</a>";
    }
    $navi .= " ".$seperator." ";
    if ($curr_limit < ($num_records-$limit_val)) {
        $navi .= "<a href="".$_SERVER['PHP_SELF']."?".$limit_var. "=".($curr_limit+$limit_val).$qs."">".$next."</a>";
    }
    return trim($navi, " | ");
}
 
// example placing links ($num_all is the value of all records in you result set)
echo navigation_links($sql_limit, $num_all, 10);
?>

source | more functions avail.

Leave a Reply

Your email address will not be published. Required fields are marked *