Web Producer w/ Brilliance
Aquent
US-CA-San Rafael

Justtechjobs.com Post A Job | Post A Resume

Building Next/Prev Buttons for Query Results (Part 2)
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;
}

?>
[ Next Page ]


Comments:
ÀçÅñٹ« È«º¸»ç¿ø ¸ðÁýÇÕ´Ï´ÙLGÅÚ·¹ÄÞ11/17/05 05:17
¡Ú¡Ú ½ÇÀü ¸Â°íÆ÷Ä¿ Ãļ­ Çö±Ý¹úÀÚ!!¡Ú¡Ú7Æ÷Ä¿»çÀÌÆ®11/17/05 03:31
°ñÄ¡¾ÆÇ ºô¶ó ÇØ°áÇØ µå¸³´Ï´Ù.ÀºÇý°øÀÎÁß°³»ç11/16/05 05:33
±â³×½ººÏ µµÀü, 100¹è »¡¶óÁö°í ½¬¿öÁø ¿µ¾î¿ø¸® ÀÌÈÆ±â11/16/05 01:17
°ñÄ¡¾ÆÇ ºô¶ó ÇØ°áÇØ µå¸³´Ï´Ù.ÀºÇý°øÀÎÁß°³»ç11/15/05 17:26
Ä«/µå/µ¹·Á¸·±â·Î/¸Á°¡Áö½ÅºÐ/²À º¸¼¼¿ä!ÀÌÇýÁø12/07/04 11:49
½Å.¿ë.ºÒ.·®.ÀÚ/´çÀÏ500/´ë.Ãâ.ºñ.¹ýÀÌÈñÁø12/06/04 03:01
Ä«,µå,¿¬,ü,ÀÚ/¿¹.Á¤.ÀÚ ´ë,Ãâ 100-1000¸¸¿øÀÌ´ÙÇö12/05/04 11:23
´ë'Ãâ'°Å'Àý'½Ã'100%µÇ'°Ô'ÇÏ'´Â'¹æ'¹ýÇѰæ¿í12/05/04 03:29
½Å.¿ë.ºÒ.·®.ÀÚ/´çÀÏ500/´ë.Ãâ.ºñ.¹ýÀÌÈñÁø12/02/04 12:21
½Å.¿ë.ºÒ.·®/Ä«.µå.¿¬.ü/´ë.Ãâ/È¥ÀÚ/ÇØ.°áÇÏ´Â/¹æ.¹ý±èÇö¼­11/28/04 04:25
½Å.¿ë.ºÒ.·®.ÀÚ/´çÀÏ500/´ë.Ãâ.ºñ.¹ýÀÌÈñÁø11/28/04 00:49
´ë'Ãâ'°Å'Àý'½Ã'100%µÇ'°Ô'ÇÏ'´Â'¹æ'¹ýÇѰæ¹Î11/23/04 21:11
Ä«/µå/µ¹·Á¸·±â·Î/¸Á°¡Áö½ÅºÐ/²À º¸¼¼¿ä!ÀÌÁ¤¿¬11/18/04 21:16
5.ºÐ.¸¸¿¡ 4.0¸¸.¿ø ¹«,ÀÌ.ÀÚ·Î ºô.¸®±âÁö¿µÈñ11/17/04 20:37
½Å.¿ë.ºÒ.·®.ÀÚ/´çÀÏ500/´ë.Ãâ.ºñ.¹ýÀÌÈñÁø11/15/04 18:39
Ä«.µå.±ø.¾È.ÇÏ.°í.µ·.¸¸.µé.¾î.¾².´Â.ºñ.¹ýÀÌÈñÁø11/12/04 21:01
½Å¿ëºÒ·®ÀÚ Ä«µåºú 2³âµÚ ¿ø±Ý¸¸ °±´Â¹æ¹ý!!±èÁ¤Èñ02/14/04 08:54
½Å¿ëºÒ·®/Ä«µå¿¬Ã¼/È¥ÀÚ ÇØ°áÇÏ´Â ¹æ¹ý!!(Çʵ¶)±ÝÀ¶Á¤º¸11/06/03 23:42
RE: Building Next/Prev Buttons - PHP/PostgreSLynna Landstreet08/20/03 17:15
RE: Building Next/Prev Buttons - PHP/PostgreSGuillermo Soria06/26/03 12:38
Building Next/Prev Buttons - PHP/PostgreSQLOscar M04/07/03 02:42
Another nice Next/Prev functionWikus01/03/03 02:07
RE: German Tutorial ?mrsoul11/09/02 08:13
register_globals = offMoonLight10/16/02 13:25
Create Table probleMukesh Kumar09/24/02 02:03
RE: Doing it with only one query - Eh?dave09/16/02 16:32
RE: LIMIT with ODBC (access, sql server)Iain Bell09/10/02 07:10
RE: LIMIT with ODBC (access, sql server)Jim B09/04/02 06:35
solutionguy08/20/02 12:21
RE: Working Next / Prev - its very simpleEdwin D. Viñas08/02/02 09:27
German Tutorial ?ChosenFist07/30/02 16:27
RE: Working Next / Prev - its very simpleAntone07/15/02 23:02
RE: i have a list problemMadDog06/20/02 10:10
RE: Working Next / Prev - its very simpleMatt Facer06/07/02 19:53
Working Next / Prev - its very simpleMad 4D Web04/25/02 19:30
RE: how rude - Thank you.Mitch Greene04/04/02 22:45
Cant seem to get it doneAdrianQuah04/03/02 04:18
php on free bsdshowme03/10/02 10:29
RE: How rudeHans de Ree02/27/02 05:55
i have a list problemjapper02/15/02 08:42
RE: LIMIT with ODBC (access, sql server)Samuel Adu02/12/02 09:26
RE: prev/next questionWill02/08/02 11:21
How rudeMike Zon02/07/02 08:51
meta: prev/nextCameron02/07/02 01:38
RE: how rudeRod K01/31/02 10:51
RE: how rudeScorch01/28/02 15:54
RE: LIMIT (equivalent) in MS SQL?patinya12/11/01 21:20
RE: Where is MAX page??anitha10/05/01 14:32
Where is MAX page??micro09/25/01 09:41
customize SQL environmentShabbir09/20/01 06:43
RE: prev/next questiontho09/12/01 14:30
RE: WHY????Kevin Rubio09/11/01 20:47
RE: how rudeBill09/02/01 19:56
got a problem here!!!aidan peiser08/29/01 01:29
How I call the function?Paulo Jorge08/10/01 17:25
LIMIT (equivalent) in MS SQL?Forbes Benning08/07/01 10:45
buttonssurviver07/31/01 08:22
RE: Not work?alan07/22/01 23:16
Use of next,prev buttonsKhunnee06/25/01 02:39
Use of next,prev buttonsKhunnee06/25/01 02:10
RE: Use of next,prev buttonsDjTal06/14/01 04:37
How about this?Joe05/08/01 18:13
RE: thanks!rod k04/23/01 12:56
thanks!fandelem04/21/01 22:17
RE: Use of next,prev buttonsanitha03/27/01 13:59
Use of next,prev buttonsanitha03/26/01 15:43
Display an infinite number of entriesSven03/25/01 11:39
This example and OracleAntal Mittendorff03/24/01 12:16
help with back/next buttons for query resultwaleed03/21/01 11:54
RE: how rude - ManeeshMax03/09/01 08:38
RE: how rude - Maneeshrod k03/03/01 05:53
RE: how rudeMANEESH03/02/01 04:56
RE: prev/next questionSimon Pritchard02/20/01 14:22
What's the problem?Simon Pritchard02/20/01 14:16
prev/next questiondave02/15/01 20:57
LIMIT with ODBCVili02/01/01 18:14
RE: Doing it with only one query - HansDavid Hammerton01/17/01 21:16
Another way With OracleFabienne01/11/01 11:05
RE: how rudeTom Carroll01/02/01 11:07
RE: With OracleThomas Fromm12/28/00 20:37
RE: how rudeNeil P Davis12/28/00 15:16
With OracleBertrand Mansion12/27/00 11:22
RE: how rude - scottmark12/26/00 16:02
Limit For Page Number Displaying?Cem12/23/00 06:54
RE: how rudeScott Molinari12/21/00 16:10
my code...paul gareau12/19/00 18:15
RE: how rudemark12/19/00 13:21
My codeMatt12/18/00 22:48
RE: Doing it with only one query - RodIgor12/17/00 14:47
RE: how rudeScott Molinari12/17/00 14:46
why can we all get alongmike12/17/00 04:48
RE: Doing it with only one query - Rodrod k12/16/00 01:25
RE: Doing it with only one query - Rodmorpheus()12/15/00 23:34
RE: Doing it with only one query - HansHans12/15/00 15:50
RE: Doing it with only one query - HansRod Kreisler12/15/00 12:25
RE: Doing it with only one query - HansHans12/15/00 10:29
RE: how rudeMike D.12/15/00 09:30
RE: Doing it with only one query - HansChris Sommer12/15/00 08:14
RE: how rudeFor those who want some working code12/15/00 01:30
RE: how rudeJim12/14/00 22:26
RE: how rudeBruce12/14/00 19:53
RE: how rudegavster12/14/00 16:58
RE: how rudeChris Sommer12/14/00 16:48
RE: Doing it with only one queryHans Wolters12/14/00 16:38
RE: how rudeTim Perdue, PHPBuilder.com12/14/00 14:23
Not work?kez12/14/00 08:35
Doing it with only one querymorpheus()12/14/00 08:09
how rudeblue presley12/14/00 02:02
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.