...

View Full Version : php next button



purekarma
09-07-2004, 03:44 AM
I am hoping to create a next button on my page so that it always moves to the next page. The pages are organized by variables (so that page 2 is $pg=2, page 3 is $pg=3, etc). How do I make it so that the button looks at the current address (for example: www.site.com/index.php?pg=2) and then automatically adjust its own link to go to next page (for example: www.site.com/index.php?pg=3)? So, that when it gets to say page 5, the link will automatically change itself to go to page 6, and then it will change itself to got o page 7?

marek_mar
09-07-2004, 07:11 AM
You get the number from the user you add 1 to it.


<?
$page_num = $_REQUEST['pg'];
$page_num += 1;
// Optional check if number is too large or so
if($page_num > MAX)
{
$page_num = 1;
}
elseif($page_num < 0)
{
$page_num = MAX;
}
print '<a href="index.php?pg=' . $page_num . '">Next</a>';
?>

You should make MAX a variable and set it to the highest page number.

trib4lmaniac
09-07-2004, 10:36 AM
if($page_num > MAX)
{
$page_num = 1;
}
elseif($page_num < 0)
{
$page_num = MAX;
}
Isn't that supposed to be the other way round? Besides, I love one liners :cool:

$page_num = ($page_num > MAX) ? MAX : ($page_num < 1) ? 1 : $page_num;
Also, I personally would use $_GET instead of request as that tells me where the variable is coming from, and $page_num += 1; could become $page_num++;

All very minor points :thumbsup:

Spookster
09-07-2004, 05:30 PM
Be careful with ternary expressions as they detract from readability.

marek_mar
09-07-2004, 05:30 PM
Yes with 30 secs to spare I didn't have much time to think about it...
As to the other way around: My idea was to let it go in a loop but as you showed in you one-liner it is easy to change.
As to $_GET vs. $_REQUEST: who cares. GET are least secure so you could allow the more secure* ways to do it too.

*by more secure I mean it is harder to change the not GET vars

purekarma
09-10-2004, 02:41 AM
thanks, I will try the code out. one more question...how do I code it so that on the last page, the "next" changes to "end" or something along those lines?

As5a5sIn5
09-10-2004, 03:11 AM
if($PAGE == '5'){
/*change the var obviously...and whatever the last page is put where the 5 is*/

echo '<html tags for the link and image that says back or w/e';
}

thats the basics..i think u can figure the rest :D

Kurashu
09-10-2004, 04:16 AM
QUESTION: are you pulling your data from a database?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum