Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

Building Next/Prev Buttons for Query Results (Part 2)
The $where variable is a little difficult to explain. If your code generates a dynamic where clause based upon data passed to the script, you will need to construct the $where variable. It is not entirely safe to pass the EXACT where clause in the query string of the URL, so you can "encode" the where clause. NOTE: if your where clause is entirely static (i.e. is always the same for each instance of the script) or your query does not utilize a where clause you need do nothing.
For example, let's say our where clause is based on two variables passed to the script: $one and $two, and would be used in the following manner in the where clause of our query:
where one='$one' and two='$two'
We would want to add this code somewhere at the top of the script:

<?php

if (!$where) // where was not passed {
    
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]'";

?>
How this will be used will become clear in a moment. NOTE: if you do not have a where clause, do not use the above code in your script. No modificatins to the function are neccesary however.
The last variable we need is $numpage, which is the number of pages that will display. In short, it is the number of records divided by $limit, rounded up. We need to preform two queries for this operation to work successfully. The first query returns the number of rows that would be returned without a limit:

<?php

$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

?>
Now, we can do our query that returns the actual records we will display:

<?php

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

?>
The only thing remaining is to call the function. But what if our query is limited in such a way as there will only be one set of results? We certainly don't want to display page links if there are no other pages!

<?php

if ($numpage>1) {
    
pagenav();
    print
"<P>";
}

?>
will solve this problem.
[ Next Page ]

[Page 1]  [Page 2]  


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.