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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Location
    Scotland
    Posts
    134
    Thanks
    12
    Thanked 0 Times in 0 Posts

    First Pagination Script Error

    Hi Guys,

    i have written apagination test script (so i could get it into my head the way it should be done)

    it looks all well and good but doesn't work as it should.

    for example the url is like this:

    PHP Code:
    http://localhost/PHP/pagination/index.php?page=7 
    if i do this:

    PHP Code:
    http://localhost/PHP/pagination/index.php?page=77777 
    then 77777 numbers are displayed on the screen, there is 222 records in the database and that make 23 pages when i echo back the variables, i'm not sure where i have went wrong:

    PHP Code:
    <?php

     
    // database connection //
     
    include("db_connection.php");

     
    // pagination script start //
     
     // number of records to display per page //
     
    $display_per_page 10;
     
     
    // check the required number of pages //
     
    if(isset($_GET['page'])) {
     
      
    $number_of_pages $_GET['page'];
     
     } else {
     
      
    // query for the number of pages //
      
    $query "SELECT COUNT(*) FROM `files`";
      
    $result mysql_query($query);
      
      
    // put the number of results in an array //
      
    $row mysql_fetch_array($result);
      
      
    // this contains the overall TOTAL number of results in the db //
      
    $total_number_of_results $row['0'];
      
      
    // calculate number of pages //
      
    if($total_number_of_results $display_per_page) {
      
       
    $number_of_pages ceil($total_number_of_results/$display_per_page);
      
      } else {
      
       
    $number_of_pages 1
      
      }
       
     } 
    // end of number of pages if //
     
     // now determine the start of the results //
     
    if(isset($_GET['page'])) {
     
      
    $start_page $_GET['page'];
     
     } else {
     
      
    $start_page 0;
     
     }
     
     
    // do another query this time use the variables in the limit clause //
     
    $query "SELECT * FROM `files` LIMIT $start_page, $display_per_page";
     
    $result mysql_query($query);
     
     
    // loop out the results //
     
    while($row mysql_fetch_array($result)) {
     
       
    $id $row['id'];
     
       echo (
    "$id");
       echo (
    "<br />");
     
     }
     
       echo (
    "<hr />");
     
     
    // 10 results displayed great //
     
     // now make the links //
     
    if($number_of_pages 1) {
     
     
    // find out what exactly the page the user is on [start page say 2 x 10 +1] //
     
    $current_page_the_user_is_on = ($start_page/$display_per_page) + 1;
     
     
    // if its not the first page we need a previous button //
     
    if($current_page_the_user_is_on != 1)
     
     
    // echo a previous button //
     
     // vars //
     
    $prev = ($number_of_pages 1);
     
    $next = ($number_of_pages 1);
     
     echo (
    "<a href=\"index.php?page=$prev\">PREV</a> ");
     
     }
     
     
    // make all the numbered pages //
     
    for($i 1$i <= $number_of_pages$i++) {
     
     echo (
    "<a href=\"index.php?page=$i\">$i</a> ");
     
     }
     
     
    // finally make the next link //
     
    if($current_page_the_user_is_on != $number_of_pages) {
     
     echo (
    "<a href=\"index.php?page=$next\">NEXT</a> ");
     
     }
     
    ?>
    can anyone see any problems at all?

    thanks guys

    Graham

  • #2
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    you don't want to be using $_GET['page'] to determine the number of pages- the number of pages will always be "total number of records"/"records per page", so lose the first 'if'.

    Having 'next' and 'prev' being +-1 of $number_of_pages seems wrong as well, surely it wants to be +-1 of the current page?
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #3
    Regular Coder
    Join Date
    Jul 2007
    Location
    Scotland
    Posts
    134
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hi Mate,

    thanks mate i did some chaanges (pagination can be darn tricky lol)

    PHP Code:
    <?php 

     
    // database connection // 
     
    include("db_connection.php"); 

     
    // pagination script start // 
      
     // number of records to display per page // 
     
    $display_per_page 10
      
     
    // query for the number of pages // 
     
    $query "SELECT COUNT(*) FROM `files`"
     
    $result mysql_query($query); 
       
     
    // put the number of results in an array // 
     
    $row mysql_fetch_array($result); 
       
     
    // this contains the overall TOTAL number of results in the db // 
     
    $total_number_of_results $row['0']; 
       
     
    // calculate number of pages // 
     
    if($total_number_of_results $display_per_page) { 
       
       
    $number_of_pages ceil($total_number_of_results/$display_per_page); 
       
     } else { 
       
       
    $number_of_pages 1;  
        
     } 
    // end of number of pages if // 
      
     // now determine the start of the results // 
     
    if(isset($_GET['page'])) { 
      
      
    $start_page $_GET['page']; 
      
     } else { 
      
      
    $start_page 0
      
     } 
      
     
    // do another query this time use the variables in the limit clause // 
     
    $query "SELECT * FROM `files` LIMIT $start_page, $display_per_page"
     
    $result mysql_query($query); 
      
     
    // loop out the results // 
     
    while($row mysql_fetch_array($result)) { 
      
       
    $id $row['id']; 
      
       echo (
    "$id"); 
       echo (
    "<br />"); 
      
     } 
      
       echo (
    "<hr />"); 
      
     
    // 10 results displayed great // 
      
     // now make the links // 
     
    if($number_of_pages 1) { 
      
     
    // find out what exactly the page the user is on [start page say 2 x 10 +1] // 
     
    $current_page_the_user_is_on = ($start_page/$display_per_page) + 1
      
     
    // if its not the first page we need a previous button // 
     
    if($current_page_the_user_is_on != 1
      
     
    // link vars //
     
    $prevlink = ($number_of_pages-1);
     
    $nextlink = ($number_of_pages+1);
      
     
    // previous link //
      
    if ($number_of_pages 1) { 
            echo 
    "<a href=\"index.php?page=".$prevlink."\">[Prev]</a>&nbsp;\n"
      } 
        
      for(
    $i=1$i $number_of_pages$i++) { 
        
            if (
    $i == $number_of_pages) { 
                echo 
    "<span class=\"paglink\">[$i]</span>&nbsp;\n"
            } else { 
                echo 
    "<a href=\"index.php?page=".$i."\">[$i]</a>&nbsp;\n"
            } 
      } 

      if (
    $number_of_pages $total_number_of_results-1) { 
            echo 
    "<a href=\"index.php?page=".$nextlink."\">[Next]</a>\n"
      } 
      
     } 
      
    ?>
    the first half of the script seems fine , brings back 10 results fine, it seems i have made an error in the links part, can you see any obvious errors at all?

    thanks mate

    Graham


  •  

    Posting Permissions

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