Pagination Code Needed

Discussion in 'PHP' started by Atik Hasan, Dec 20, 2014.

  1. #1
    Hi there, I need PHP pagination Code. Can someone give me this code. Thanks for your attention. Thanks
     
    Solved! View solution.
    Atik Hasan, Dec 20, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    What are you basing the pagination on? Do you wanna fetch records from a database? Parse an array? Divide a string?
     
    PoPSiCLe, Dec 20, 2014 IP
  3. Atik Hasan

    Atik Hasan Greenhorn

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #3
    I want to display 20 products in each page. so I need this code. Thanks for replying my answer.Thanks
     
    Atik Hasan, Dec 21, 2014 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,893
    Likes Received:
    4,553
    Best Answers:
    123
    Trophy Points:
    665
    #4
    Do you have a cms or framework? or is your site using an ecommerce system?
     
    sarahk, Dec 21, 2014 IP
  5. #5
    I'm assuming you're fetching the results from a database - the simplest way of doing this is using a GET-parameter (for pages) and LIMIT the returns from the database.
    Say you have something like this to fetch records from the database:
    
    SELECT * FROM products
    
    Code (markup):
    to fetch the first 20 records, you'd do this:
    
    SELECT * FROM products LIMIT 0, 20
    
    Code (markup):
    to get the next 20 records, you'd do this:
    
    SELECT * FROM products LIMIT 20, 40
    
    Code (markup):
    and so forth and so on.

    To do this via PHP you'd do something like this:
    
    $page = isset($_GET['page]) ? $_GET['page'] : 0; // what this does is see if the ?page=-parameter is set in the URL, and if it is, assign the value to $page
    $records = ($page != 0 || !empty($page)) ? $page * 20 : 0; 
    $limit = ($page != 0 || !empty($page)) ? $page * 20 * 2 : 20; // this tells you which limit to use
    // then you build your query like this:
    SELECT * FROM products LIMIT $page, $limit;
    
    PHP:
    You'll of course also need a way to go through the total pages, something like loop through the total records found and create a list of links of pages (1 to whatever number you reach)
     
    PoPSiCLe, Dec 22, 2014 IP