Building Next/Prev Buttons for Query Results (Part 2)
Rod Kreisler
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\"> <-- PREV</A> </TD>" ; } else { echo "<-- PREV" ; } echo "<TD ALIGN=CENTER> " ; 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 " </TD> <TD ALIGN=LEFT>" ; if ( $offset != $limit *( $numpage - 1 )) { $newoff = $offset + $limit ; echo "<A HREF=\"$PHP_SELF?offset=$newoff&where=$safewhere\"> NEXT--></A> </TD>" ; }else{ echo "NEXT--></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/PostgreS Lynna Landstreet 08/20/03 17:15
RE: Building Next/Prev Buttons - PHP/PostgreS Guillermo Soria 06/26/03 12:38
Building Next/Prev Buttons - PHP/PostgreSQL Oscar M 04/07/03 02:42
Another nice Next/Prev function Wikus 01/03/03 02:07
RE: German Tutorial ? mrsoul 11/09/02 08:13
register_globals = off MoonLight 10/16/02 13:25
Create Table proble Mukesh Kumar 09/24/02 02:03
RE: Doing it with only one query - Eh? dave 09/16/02 16:32
RE: LIMIT with ODBC (access, sql server) Iain Bell 09/10/02 07:10
RE: LIMIT with ODBC (access, sql server) Jim B 09/04/02 06:35
solution guy 08/20/02 12:21
RE: Working Next / Prev - its very simple Edwin D. Viñas 08/02/02 09:27
German Tutorial ? ChosenFist 07/30/02 16:27
RE: Working Next / Prev - its very simple Antone 07/15/02 23:02
RE: i have a list problem MadDog 06/20/02 10:10
RE: Working Next / Prev - its very simple Matt Facer 06/07/02 19:53
Working Next / Prev - its very simple Mad 4D Web 04/25/02 19:30
RE: how rude - Thank you. Mitch Greene 04/04/02 22:45
Cant seem to get it done AdrianQuah 04/03/02 04:18
php on free bsd showme 03/10/02 10:29
RE: How rude Hans de Ree 02/27/02 05:55
i have a list problem japper 02/15/02 08:42
RE: LIMIT with ODBC (access, sql server) Samuel Adu 02/12/02 09:26
RE: prev/next question Will 02/08/02 11:21
How rude Mike Zon 02/07/02 08:51
meta: prev/next Cameron 02/07/02 01:38
RE: how rude Rod K 01/31/02 10:51
RE: how rude Scorch 01/28/02 15:54
RE: LIMIT (equivalent) in MS SQL? patinya 12/11/01 21:20
RE: Where is MAX page?? anitha 10/05/01 14:32
Where is MAX page?? micro 09/25/01 09:41
customize SQL environment Shabbir 09/20/01 06:43
RE: prev/next question tho 09/12/01 14:30
RE: WHY???? Kevin Rubio 09/11/01 20:47
RE: how rude Bill 09/02/01 19:56
got a problem here!!! aidan peiser 08/29/01 01:29
How I call the function? Paulo Jorge 08/10/01 17:25
LIMIT (equivalent) in MS SQL? Forbes Benning 08/07/01 10:45
buttons surviver 07/31/01 08:22
RE: Not work? alan 07/22/01 23:16
Use of next,prev buttons Khunnee 06/25/01 02:39
Use of next,prev buttons Khunnee 06/25/01 02:10
RE: Use of next,prev buttons DjTal 06/14/01 04:37
How about this? Joe 05/08/01 18:13
RE: thanks! rod k 04/23/01 12:56
thanks! fandelem 04/21/01 22:17
RE: Use of next,prev buttons anitha 03/27/01 13:59
Use of next,prev buttons anitha 03/26/01 15:43
Display an infinite number of entries Sven 03/25/01 11:39
This example and Oracle Antal Mittendorff 03/24/01 12:16
help with back/next buttons for query result waleed 03/21/01 11:54
RE: how rude - Maneesh Max 03/09/01 08:38
RE: how rude - Maneesh rod k 03/03/01 05:53
RE: how rude MANEESH 03/02/01 04:56
RE: prev/next question Simon Pritchard 02/20/01 14:22
What's the problem? Simon Pritchard 02/20/01 14:16
prev/next question dave 02/15/01 20:57
LIMIT with ODBC Vili 02/01/01 18:14
RE: Doing it with only one query - Hans David Hammerton 01/17/01 21:16
Another way With Oracle Fabienne 01/11/01 11:05
RE: how rude Tom Carroll 01/02/01 11:07
RE: With Oracle Thomas Fromm 12/28/00 20:37
RE: how rude Neil P Davis 12/28/00 15:16
With Oracle Bertrand Mansion 12/27/00 11:22
RE: how rude - scott mark 12/26/00 16:02
Limit For Page Number Displaying? Cem 12/23/00 06:54
RE: how rude Scott Molinari 12/21/00 16:10
my code... paul gareau 12/19/00 18:15
RE: how rude mark 12/19/00 13:21
My code Matt 12/18/00 22:48
RE: Doing it with only one query - Rod Igor 12/17/00 14:47
RE: how rude Scott Molinari 12/17/00 14:46
why can we all get along mike 12/17/00 04:48
RE: Doing it with only one query - Rod rod k 12/16/00 01:25
RE: Doing it with only one query - Rod morpheus() 12/15/00 23:34
RE: Doing it with only one query - Hans Hans 12/15/00 15:50
RE: Doing it with only one query - Hans Rod Kreisler 12/15/00 12:25
RE: Doing it with only one query - Hans Hans 12/15/00 10:29
RE: how rude Mike D. 12/15/00 09:30
RE: Doing it with only one query - Hans Chris Sommer 12/15/00 08:14
RE: how rude For those who want some working code 12/15/00 01:30
RE: how rude Jim 12/14/00 22:26
RE: how rude Bruce 12/14/00 19:53
RE: how rude gavster 12/14/00 16:58
RE: how rude Chris Sommer 12/14/00 16:48
RE: Doing it with only one query Hans Wolters 12/14/00 16:38
RE: how rude Tim Perdue, PHPBuilder.com 12/14/00 14:23
Not work? kez 12/14/00 08:35
Doing it with only one query morpheus() 12/14/00 08:09
how rude blue presley 12/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.