How to create paging in PHP (page no)

Discussion in 'PHP' started by dineshsingh1984, Jan 15, 2011.

  1. #1
    how to create paging when fetch data from mysql database
    
    
    $sql = "select * from product";
    $rs  = mysql_query($sql) or die("database error");
    
    $i=1;
    while($re  = mysql_fetch_array($rs))
    {
    S.No : echo $i."<br>";
    Product Name : echo $re['item'];
    Product Description : echo $re['desc'];
    $i++;
    }
    
    
    PHP:
    I want display only 10 product on one page if product more than 10 then print on second page.

    plz help me................
     
    dineshsingh1984, Jan 15, 2011 IP
  2. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #2
    Use something like this.Adapt it to your needs.
    
    $limit = 10;
    $start = 0;
    $page = 1;
    if(isset($_GET['page']) && $_GET['page'] != "")
    {
    $page = $_GET['page'];
    }
    $start = ($page - 1) * $limit;
    
    $sql = "select * from product  LIMIT ".$start.",".$limit;
    $rs  = mysql_query($sql) or die("database error");
    
    $i=1;
    while($re  = mysql_fetch_array($rs))
    {
    S.No : echo $i."<br>";
    Product Name : echo $re['item'];
    Product Description : echo $re['desc'];
    $i++;
    }
    
    $pages = intval($total_rows / $limit);
    if($total_rows%$limit > 0)
    $pages += 1;
    
    echo 'Current page : '.$page.' / '.$pages.' ';
    if($page > 1)
    {
                        echo '<a href="?page='.($page-1).'">&laquo; Previous</a>';
                        $show = 1;
    }
                    
                    if($page < $pages && $pages > 1)
                    {
                        if($show == 1)
                        {
                            echo '&nbsp;|&nbsp;';
                        }
                        
                        echo '<a href="?page='.($page+1).'">Next &raquo;</a>';
                    }
    
    PHP:
     
    tvoodoo, Jan 15, 2011 IP
  3. mnvlxxx

    mnvlxxx Peon

    Messages:
    47
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hope this helps. You just need to place the page number at $current_page var.

    
    $results_per_page=10;
    $current_page=0;
    $sql = "select * from product limit " . $current_page*$results_per_page . ", " . $results_per_page;
    $rs  = mysql_query($sql) or die("database error");
    
    $i=1;
    while($re  = mysql_fetch_array($rs))
    {
    S.No : echo $i."<br>";
    Product Name : echo $re['item'];
    Product Description : echo $re['desc'];
    $i++;
    }
    
    PHP:
     
    mnvlxxx, Jan 15, 2011 IP