Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: php next button

  1. #1
    New to the CF scene
    Join Date
    Jun 2004
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    php next button

    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?

  • #2
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    You get the number from the user you add 1 to it.
    PHP Code:
    <?
    $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.

  • #3
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by marek_mar
    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
    PHP Code:
    $page_num = ($page_num MAX) ? MAX : ($page_num 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
    Last edited by trib4lmaniac; 09-07-2004 at 09:39 AM.

  • #4
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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

  • #5
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Be careful with ternary expressions as they detract from readability.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #6
    New to the CF scene
    Join Date
    Jun 2004
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #7
    Regular Coder
    Join Date
    Apr 2004
    Location
    Philadelphia
    Posts
    241
    Thanks
    2
    Thanked 5 Times in 5 Posts
    PHP Code:
    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

  • #8
    Regular Coder
    Join Date
    Aug 2004
    Location
    The US of A
    Posts
    767
    Thanks
    1
    Thanked 0 Times in 0 Posts
    QUESTION: are you pulling your data from a database?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •