2017-01-30 25 views
0

很抱歉,如果这是一个愚蠢的问题,但我仍然在这一个新手位和学习,我有试错去。的MySQL/PHP分页问题 - 不会去第二页

我有以下分页脚本,但它确实在工作的时候点击“下一页”不显示最后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); 
    ?> 
    
+0

( 1)MySQL语法说:第一个是LIMIT,第二个是OFFSET。看看程序中使用的变量,似乎它们的位置是互换的。 (2)当您在新页面时,'$ offset'似乎没有改变。 (3)一旦你的逻辑正确,请考虑切换到'mysqli_'或'PDO'准备查询立即编写不容易受到SQL注入攻击的脚本。 –

回答

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 
+0

该解决方案可以通过在查询中引入限制和偏移属性来改进。到目前为止,它将首先获取所有行,然后尝试找到要显示的子集。因此,它可能会影响网页加载时间大大当表中的大量数据。作为一般的好习惯,请尽量避开mysql_的()函数提出了一个解决方案时,因为它们是由不同的标准有问题。 –