Next Page and Previous Page

Discussion in 'PHP' started by geekazoid, Aug 31, 2006.

  1. #1
    geekazoid, Aug 31, 2006 IP
  2. Cryogenius

    Cryogenius Peon

    Messages:
    1,280
    Likes Received:
    118
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You could try something like this (off the top of my head):

    
    $this = basename($_SERVER['SCRIPT_URI'], '.php');
    $prev = $this - 1;
    $next = $this + 1;
    
    $prevlink = $prev . '.php';
    $nextlink = $next . '.php';
    
    PHP:
    You will need to add your own code to check that $prev is never less than 1 and $next is never greater than the last page.

    Hope that helps...

    Cryo.
     
    Cryogenius, Aug 31, 2006 IP
  3. geekazoid

    geekazoid Peon

    Messages:
    208
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    that looks right. would it be possible to make that a bit simpler to read please ... sorry
     
    geekazoid, Sep 3, 2006 IP
  4. drewbe121212

    drewbe121212 Well-Known Member

    Messages:
    733
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    125
    #4
    Heh, you don't get much more simpilar then that.

    Just increment the current value by 1 for the next page, and decrement the current value for the last page, unless the page is already 1 or max.
     
    drewbe121212, Sep 3, 2006 IP
  5. geekazoid

    geekazoid Peon

    Messages:
    208
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ok but where do i put the code ? could some one explain please
     
    geekazoid, Sep 3, 2006 IP
  6. smatts9

    smatts9 Active Member

    Messages:
    1,089
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    88
    #6
    Put the this where ever you want your next / prev links to be located:

    
    <?
    $this = basename($_SERVER['SCRIPT_URI'], '.php');
    $prev = $this - 1;
    $next = $this + 1;
    $prevlink = $prev . '.php';
    $nextlink = $next . '.php'; 
    echo "<a href=$prevlink><< Previous</a> | <a href=$nextlink>Next >></a>";
    ?>
    
    Code (markup):
     
    smatts9, Sep 3, 2006 IP
  7. geekazoid

    geekazoid Peon

    Messages:
    208
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    so its the same code for both ? how does that work ?
     
    geekazoid, Sep 3, 2006 IP
  8. smatts9

    smatts9 Active Member

    Messages:
    1,089
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    88
    #8
    Take my block of code and put it in the Files exactly where you want your NExt and Previous links to appear.
     
    smatts9, Sep 3, 2006 IP
  9. geekazoid

    geekazoid Peon

    Messages:
    208
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    geekazoid, Sep 4, 2006 IP
  10. starke

    starke Active Member

    Messages:
    228
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #10
    Great postings... I'm looking to do the same thing with results from a search...

    Something that when you select one of the search pages, you can hit next from the selected page to go to the next result...

    Will be something working on soon... so look forward to the solution...

    Thanks,
    Starke
     
    starke, Sep 4, 2006 IP
  11. Cryogenius

    Cryogenius Peon

    Messages:
    1,280
    Likes Received:
    118
    Best Answers:
    0
    Trophy Points:
    0
    #11
    This might work better:

    
    $this = basename($_SERVER['SCRIPT_URI'], '.php');
    $prev = intval($this) - 1;
    $next = intval($this) + 1;
    $prevlink = $prev . '.php';
    $nextlink = $next . '.php'; 
    echo "<a href=\"$prevlink\">&lt;&lt; Previous</a> | <a href=\"$nextlink\">Next &gt;&gt;</a>";
    
    PHP:
     
    Cryogenius, Sep 5, 2006 IP
  12. geekazoid

    geekazoid Peon

    Messages:
    208
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    geekazoid, Sep 5, 2006 IP
  13. Cryogenius

    Cryogenius Peon

    Messages:
    1,280
    Likes Received:
    118
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Try using 'PHP_SELF' in place of 'SCRIPT_URI'. If you're still having problems, print out the value of $this after the first line.
     
    Cryogenius, Sep 5, 2006 IP
  14. smatts9

    smatts9 Active Member

    Messages:
    1,089
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    88
    #14
    
    <?php
    $prev = basename($_SERVER['REQUEST_URI'], '.php') - 1;
    $next = basename($_SERVER['REQUEST_URI'], '.php') + 1;
    $prevlink = $prev . '.php';
    $nextlink = $next . '.php';
    echo "<a href=\"$prevlink\">&lt;&lt; Previous</a> | <a href=\"$nextlink\">Next &gt;&gt;</a>";
    ?> 
    
    Code (markup):
    He had SCRIPT_URI and not REQUEST_URI mine should work now.
     
    smatts9, Sep 5, 2006 IP
  15. geekazoid

    geekazoid Peon

    Messages:
    208
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Brilliant thats working ! now is there anyway i can get the next and previous to link be images ??

    So the images would link to the next page and the previous page ?
     
    geekazoid, Sep 6, 2006 IP
  16. jawednazarali

    jawednazarali Guest

    Messages:
    254
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #16
    <?php
    $prev = basename($_SERVER['REQUEST_URI'], '.php') - 1;
    $next = basename($_SERVER['REQUEST_URI'], '.php') + 1;
    $prevlink = $prev . '.php';
    $nextlink = $next . '.php';
    echo "<a href=\"$prevlink\"><img src=\"prev.gif\" border=\"0\"></a> | <a href=\"$nextlink\"><img src=\"next.gif\" border=\"0\"></a>";
    ?> 
    Code (markup):

    remember the prev.gif and next.gif must be located in the same folder as your php files; if they are some where else simple modify the src part and it will work to your needs.
     
    jawednazarali, Sep 6, 2006 IP
  17. geekazoid

    geekazoid Peon

    Messages:
    208
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Thankyou so much guys, Your all sooooo helpfull! ...Job Done!
     
    geekazoid, Sep 6, 2006 IP