Thread: Pagination...

    #1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    On planet earth
    Posts
    101
    Rep Power
    11

    Pagination...


    I have this code that produces pagination with clickable links to produces 5 results (or records) per page. I want to display all results in the pagination. The thing is, every time I click the links below, it shows the same first 5 records, is there anyway I can get pagination without the need of filtering results?

    PHP Code:
    $list = new ListDB(); 
    $list->ListDisplay();  
    $per_page 5
    $list_count count($list->data); 
    $pages ceil($list_count/$per_page);  
    if(!isset(
    $_GET['page'])) {     header("location: list.php?page=1");
    }else 
    {     
    $page $_GET['page']; 
    }  
    $start = (($page 1)*$per_page); 
    This code to produce the clickable links
    PHP Code:
     for($number 1;
    $number<=$pages;
    $number++){     
    echo 
    '<a href="?page'.$number.'">'.$number.'</a>'

  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Dec 2004
    Posts
    3,016
    Rep Power
    376
    and where is you query that gets the record? are you using this page parameter to change your query? see offset & limit
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    On planet earth
    Posts
    101
    Rep Power
    11
    I get my query from ListDB()

    I don't know how to offset and limit a query

    Oh, I'm limiting it by the id in the database
    Last edited by cssbonding; February 18th, 2014 at 04:54 AM.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by cssbonding
    I don't know how to offset and limit a query
    Then you should look that up. It's no secret knowledge.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    On planet earth
    Posts
    101
    Rep Power
    11
    OK, so I needed "LIMIT 0, 5" in the MySQL statement
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    That gives you the first five entries. But you want the 5 entries of a particular page.

    You've already written down the formula for calculating the offset:

    PHP Code:
    $start = (($page 1)*$per_page); 
    Now you need to include this in the query.

    Make sure to cast $start into an integer so that there won't be SQL injections.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    On planet earth
    Posts
    101
    Rep Power
    11
    Originally Posted by Jacques1
    That gives you the first five entries. But you want the 5 entries of a particular page.

    You've already written down the formula for calculating the offset:

    PHP Code:
    $start = (($page 1)*$per_page); 
    Now you need to include this in the query.

    Make sure to cast $start into an integer so that there won't be SQL injections.
    Yup, it had already been done, thanks Jaques
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    On planet earth
    Posts
    101
    Rep Power
    11
    OK, so I've eventually got the pagination running, albeit, a bit unorthodox, but it still works, the only problem is when I type a number in the url to go to a page that I know doesn't exist i.e. higher than the amount of pages. say for example, I type 7 or 8 when there are only 6 pages being displayed, how do I get it back to 1 so as to "reset" the display?

    I am using this inside the script
    PHP Code:
    $page = (filter_input(INPUT_GET'page'FILTER_SANITIZE_SPECIAL_CHARS)) ? (int)filter_input(INPUT_GET'page'FILTER_SANITIZE_SPECIAL_CHARS) : 1
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Dec 2004
    Posts
    3,016
    Rep Power
    376
    you just need to check the total rows in table / rows allowed per page <= $page Variable..

    or when you use $page to get rows.. it should give you blank rows then you can detect this and take user to page 1?
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Dec 2004
    Posts
    3,016
    Rep Power
    376
    you just need to check the total rows in table / rows allowed per page <= $page Variable..

    or when you use $page to get rows.. it should give you blank rows then you can detect this and take user to page 1?
  20. #11
  21. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    On planet earth
    Posts
    101
    Rep Power
    11
    Originally Posted by paulh1983
    you just need to check the total rows in table / rows allowed per page <= $page Variable..

    or when you use $page to get rows.. it should give you blank rows then you can detect this and take user to page 1?
    It does take me to page one when the url is left at php?page=, there is that problem that has now arisen, that it needs to show 1 after the = sign
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Dec 2004
    Posts
    3,016
    Rep Power
    376
    so when you redirect to page 1, YOU add that to the code?
    //redirect(url.php?page=1);

    or in your code you could check and do
    // if page is empty or non-int, set it to 1...
  24. #13
  25. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0
    The concept is below,
    $limit=5;
    $page=$_GET['p'];
    if($page=='')
    {
    $page=1;
    $start=0;
    }
    else
    {
    $start=$limit*($page-1);
    }
    That means,
    Normally the page is in 1st page. So your limit should be 0 - 5 for display 5 records on each page
    When you go to 2nd page, the limit must changed. So you need to find the limit as i represented above method. Now your limit 5 to 5

    Reference: php, mysql, js, jquery, ajax, .htaccess,robots.txt,phponwebsites: Sorting column with pagination by clicking column header using php and mysql

IMN logo majestic logo threadwatch logo seochat tools logo