www.webdeveloper.com
Results 1 to 2 of 2

Thread: Previous / Next Buttons

  1. #1
    Join Date
    Nov 2011
    Posts
    82

    Previous / Next Buttons

    I am looking to create a previous and next button on a site I'm building that responds to page numbers and not direct URLs. Is this something that is easy to do? Essentially, I am look to add a bit of code at the top each page, for instance, $current_page = "1";, and have the buttons respond to the number. This way if I continue to add pages I can just add a new number at the top and it will automatically go from page 1 to page 2 when I click next, and also at the last page when you click next go back to 1.

    Hope that is clear enough. Thanks.

  2. #2
    Join Date
    Nov 2011
    Posts
    82
    Would something like this work?

    ($_GET['page']) ? ($page = $_GET['page']) : ($page = 1);

    $limit = ($page - 1) * 10;



    $num = 10; // number of results to display per page



    $totalRes = mysql_query("SELECT * FROM yourtable");

    $total = mysql_num_rows($totalRes) / $num;



    $sql = mysql_query("SELECT * FROM yourtable LIMIT $limit $num");



    if($_GET['page'] < $total) {

    echo '<a href="index.php?page='.$_GET['page'] + 1.'">next</a>';

    }



    if($_GET['page'] > 1) {

    echo '<a href="index.php?page='.$_GET['page'] - 1.'">Previous</a>';

    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles