MySQL / PHP分页问题 - 不会进入第二页

时间:2017-01-30 11:14:11

标签: php mysql pagination

很抱歉,如果这是一个愚蠢的问题,但我仍然有点新手,并且在我试用和试错时学习。

我有下面的分页脚本,但是当我点击"下一页"它没有显示下一页的最后2个产品。它只停留在原始3页的第一页。

  • 总数据库记录5
  • 每页限制设置为3(用于测试目的)

      <?php
     $dbhost = '*******';
     $dbuser = '*******';
     $dbpass = '*******';
    
     $rec_limit = 3;
     $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    
     if(! $conn ) {
        die('Could not connect: ' . mysql_error());
     }
     mysql_select_db('********');
    
     /* Get total number of records */
     $sql = "SELECT count(id) FROM products ";
     $retval = mysql_query( $sql, $conn );
    
     if(! $retval ) {
        die('Could not get data: ' . mysql_error());
     }
     $row = mysql_fetch_array($retval, MYSQL_NUM );
     $rec_count = $row[0];
    
     if( isset($_GET{'page'} ) ) {
        $page = $_GET{'page'} + 1;
        $offset = $rec_limit * $page ;
     }else {
        $page = 0;
        $offset = 0;
     }
    
     $left_rec = $rec_count - ($page * $rec_limit);
     $sql = "SELECT id, name, description, price ". 
        "FROM products ".
        "LIMIT $offset, $rec_limit";
    
     $retval = mysql_query( $sql, $conn );
    
     if(! $retval ) {
        die('Could not get data: ' . mysql_error());
     }
    
     while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
        echo "Product :{$row['name']}  <br> ".
           "Description : {$row['description']} <br> ".
           "Price : {$row['price']} <br> ".
           "--------------------------------<br>";
     }
    
     if( $page > 0 ) {
        $last = $page - 2;
        echo "<a href = \"$_PHP_SELF?page = $last\">Previous Page</a> |";
        echo "<a href = \"$_PHP_SELF?page = $page\">Next Page</a>";
     }else if( $page == 0 ) {
        echo "<a href = \"$_PHP_SELF?page = $page\">Next Page</a>";
     }else if( $left_rec < $rec_limit ) {
        $last = $page - 2;
        echo "<a href = \"$_PHP_SELF?page = $last\">Last Page</a>";
     }
    
     mysql_close($conn);
    ?>
    

1 个答案:

答案 0 :(得分:0)

Pagination in php is very simple concepts. You can learn easily. Just follow the below steps.

      1. Find the Start values based on page number:


 $limit=10;
 $page=$_GET['p'];
 if($page=='')
 {
  $page=1;
  $start=0;
 }
 else
 {
  $start=$limit*($page-1);
 }

     Where,
                  $limit is the number rows per page.
                  $page is page number.
                  $start is starting point of limit in mysql query.
If the page number is 1, then the start is 0 which is find by $start=$limit*($page-1).
                  $start=10*(1-1)
                  $start=10*(0)
                  $start=0

If the page number is 2, then the start is 10.
Similarly, if the page number is 3, then the start is 20.

                    2. Find total number of records in mysql table:

Then we need to calculate the total number of data in table. Then find the maximum pages using php script like below:

$tot=mysql_query("SELECT * FROM table1")  or die(mysql_error());
$total=mysql_num_rows($tot);
$num_page=ceil($total/$limit);


     Where,
                mysql_num_rows() returns the numbers results in numeric.
                ceil() returns the whole digit number. ie, ceil(2.3) => 3.
                $maxpage returns the number of pages.


                     3. Function for pagination:

        The php script for pagination function is:

function pagination($page,$num_page)
{
  echo'<ul ;
  for($i=1;$i<=$num_page;$i++)
  {
     if($i==$page)
{
 echo'<li ;
}
else
{
 echo'<li ;
}
  }
  echo'</ul>';
}      

           Where, 
                        You need to use for loop for display number of pages in mysql table.

Reference: http://www.phponwebsites.com/2014/04/php-mysql-pagination.html