#native_company# #native_desc#
#native_cta#

PREV_123_NEXT function

By Gavin Schumaker
on September 19, 2000

Version: 1.0

Type: Function

Category: HTML

License: GNU General Public License

Description: The logic of this function is based on the prev123next code snippet, but rewritten to return an array of links that can then be ouput anywhere on the page.
Also added some boundry checking for the offset variable.

Hope this is helpful, as I am somewhat new at programming. =0)

<?php
/*
    FUNCTION prev123Next()
    array prev123Next (string sql_stmt, int limit)
    
    Function prev123Next() takes in an SQL SELECT query and returns an array
    of links (already embedded in anchor tags), based on how many rows were returned by the query.
    
    The main logic for this function is based on code written by Rod Kriesler in his article at 
    https://phpbuilder.com/columns/rod20000221.php3
    
    Here's one way to use this function:
    
    Set up the SQL Statement and the limit variables (limit variable represents how many records you want per page);
    like:
    
    $sql = "SELECT snap, crackle, pop FROM rice_crispies ORDER BY whatever you like";
    $resultsPerPage = 12; // number of rows to return per page
    
    // set up a function to print out the array of links
    function printArray($element){
    echo "$element";
    
    // Call the prev123Next function and assign the return value to a variable
    $prev_next_links = prev123Next($sql, $resultsPerPage);
    
    // Now you can print that out anywhere on the page by using PHP's array_walk() function,
    // passing it the $links array and the printArray() fuction, like:
    
    array_walk($links, 'printArray');
*/

function prev123Next($sql_stmt, $limit){
    // set the $offset variable as global
    global $PHP_SELF;
    global $offset;
    // Execute the query
    $numresults = mysql_query($sql_stmt);
    // Determine the total number of rows returned.
    $numrows = mysql_num_rows($numresults);
    // Make sure that the number of rows returned is greater than zero
    if($numrows >= 1){ 
       // determine if offset has been passed to script, or if offset has been tampered with.
        if (empty($offset) || ($offset < 0) || ($offset > $numrows)) {
            $offset=0;
        }
        // Determine if a "PREV" link is necessary - if so, add it to the links array
        if (($offset > 0) && ($offset <= $numrows)) { 
            $prevoffset = $offset - $limit;
            $link_array[] = "<a href="$PHP_SELF?offset=$prevoffset">PREV</a> &nbsp; n";
        }
        
        // Determine the total number of pages needing links
        $pages=intval($numrows/$limit);
        // $pages variable now contains integer number of pages needed, unless there is a remainder from division
        if ($numrows % $limit) {
            // There is a remainder, so add one page
            $pages++;
        }
        for ($i=1; $i<=$pages; $i++) { // loop thru
            $newoffset=$limit*($i-1);
            $link_array[] = "<a href="$PHP_SELF?offset=$newoffset">$i</a> &nbsp; n";
        }
        // Determine if this is the last page.
        if (!(($offset/$limit)==$pages) && $pages!=1) {
            $newoffset=$offset+$limit;
            // if not last page give NEXT link
            if((($numrows - $offset) > $limit) && ($pages !=1) && ($offset < $numrows)){
                $link_array[] = "<a href="$PHP_SELF?offset=$newoffset">NEXT</a><p>n";
            }
        }
    }else{
        ; // redirect to error page
    }
    
    // return the array containing all of the links.
    return $link_array;
}// end function prev123next

?>