php - MySQL/PHP 分页问题 - 不会转到第二页

标签 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);
    ?>
    

最佳答案

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

关于php - MySQL/PHP 分页问题 - 不会转到第二页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41934501/

相关文章:

javascript - 如何在照片库中添加分页?

php - 分页从0开始

php - 处理文件请求和可用性

php - 使用循环显示 explode 数据

mysql - 加密整个 Rails 数据库

mysql - MariaDb SQL 注入(inject)

java - 基于 SAS 的软件中的分页

php - php函数get_browser的可靠性如何?

javascript - 将生成的 PDF 保存在 FTP 服务器上

Mysql:使用返回的部分数据行来获取更多数据