#native_company# #native_desc#
#native_cta#

Building Next/Prev Buttons for Query Results

By Rod Kreisler
on December 13, 2000

When I wrote my first article on this subject,
my intention was NOT to provide working code but rather to provide a thought
process that individuals could use to solve their own unique problems.
Judging by some of the comments and questions posted here, that was not
what most people wanted.
Unfortunately, I have not the time or desire to write code that will work
for all individuals in all cases. The following code will work fine
(with, possibly, some light changes to the HTML to suit your needs/tastes)
in most instances. Since this code ONLY provides incremental page links,
it is not suited to instances where there will be many resulting pages, but
should be OK for instances with between 0 to approximately 30 pages depending
on the width of the area in which you place the code.
That said, let’s move on to the code itself:

<?php

function pagenav() {

    global 
$limit,$offset,$numpage,$where;

    if (
$where) {

        
$safewhere=urlencode($where);

    }

    echo 
"

    <TABLE CELLPADDING=0 BORDER=0 CELLSPACING=5 WIDTH=100>

    <TR>

        <TD ALIGN=RIGHT>"
;

        if ($offset>=$limit) {

            
$newoff=$offset-$limit;

            

            echo 
"<A HREF="$PHP_SELF?offset=$newoff&where=$safewhere">

                &lt;-- PREV</A>

                </TD>"
;

        } else {

            echo 
"&lt;-- PREV";

        }

        echo "<TD ALIGN=CENTER> &nbsp; ";

        for ($i=1;$i<=$numpage;$i++) {

            if (((
$i-1)*$limit)==$offset) {

                print 
"$i ";

            } else {

                
$newoff=($i-1)*$limit;

                echo "<A HREF="$PHP_SELF?offset=$newoff&where=$safewhere">

                    $i</A> "
;

            }

        }

        echo 
"&nbsp; </TD>

        <TD ALIGN=LEFT>"
;

        if (
$offset!=$limit*($numpage-1)) {

            
$newoff=$offset+$limit;

            echo 
"<A HREF="$PHP_SELF?offset=$newoff&where=$safewhere">

                NEXT--&gt;</A>

                </TD>"
;

        }else{

            echo 
"NEXT--&gt;</TD>";

        }

        echo 
"</TR>

    </TABLE>"
;

// END FUNCTION

?>



The script relies upon three to four outside variables. The first two,
$limit and $offset can be set by adding the following lines at the beginning of your code:

<?php

// set this to the number of results you wish on each page

$limit=20

// if no offset has been passed, offset should be 0

if (!$offset) {

    
$offset=0

}

?>



1
|
2
|
3