PHPBuilder - Building Next/Prev Buttons for Query Results Page 3



RSS Twitter
Articles Databases

Building Next/Prev Buttons for Query Results - Page 3

by: Rod Kreisler
|
December 13, 2000

In conclusion our page would look something like this:

<?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

// 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

if (!
$where) {
    if (empty(
$one) || empty($two)) {
        
// some error handling as $one and/or 
        //$two not passed to initial page
    
}
    
$where="$one|$two";
}

// NOTE: if a pipe (|) may be in the value 
// of $one or $two, use a different delimiter
$data=explode('|',$where);
$query_where="where one='$data[0]' AND two='$data[1]'";

$result=mysql_query("select count(*) from tablename $query_where");

list(
$numrec)=mysql_fetch_row($result);

#calc num pages
$numpage=intval($numrec/$limit);

if (
$numrec%$limit) {
    
$numpage++; // add one page if remainder
}

$result=mysql_query ("select * from tablename $query_where limit $offset,$limit");

//<!-- HTML headers and other non-relevent stuff -->

if ($numpage>1) {
    
pagenav();
    print 
"<P>";
}
//<!-- result display loop -->
if ($numpage>1) {
    
pagenav();
    print 
"<P>";
}

?>
Happy coding.
Rod K

« Previous Page
1
|
2
|
3

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Rod Kreisler

Comment:



Comment:

(Maximum characters: 1200). You have characters left.