【问题标题】:MySQL/PHP Pagination Issue - Won't go to second pageMySQL/PHP 分页问题 - 不会转到第二页
【发布时间】:2017-01-30 19:55:55
【问题描述】:

对不起,如果这是一个愚蠢的问题,但我在这方面还是个新手,并且在不断尝试和错误中学习。

我有以下分页脚本,它确实可以工作,但是当我单击“下一页”时,它不会在下一页显示最后 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) MySQL 语法说:LIMIT 第一和 OFFSET 第二。查看程序中使用的变量,它们的位置似乎互换了。 (2) $offset 在新页面时似乎没有改变。 (3) 逻辑正确后,请考虑立即切换到mysqli_PDO 准备好的查询,以编写不易受到SQL 注入攻击的脚本。

标签: php mysql pagination


【解决方案1】:
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

【讨论】:

  • 可以通过在查询本身中合并限制和偏移属性来改进此解决方案。到目前为止,它将首先获取所有行,然后尝试找到要显示的子集。因此,当表格中有大量数据时,它可能会极大地影响页面加载时间。作为一般的良好做法,在提出解决方案时请尽量避免使用 mysql_() 函数,因为它们在各种标准下都是有问题的。
猜你喜欢
  • 1970-01-01
  • 2015-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
相关资源
最近更新 更多