分页不能移动下一步

时间:2016-04-27 07:38:12

标签: pagination

我想使用分页显示来自数据库的记录,我使用下面的代码显示限制但是当移动下一页或记录时它无法加载。 如何修复下一步移动的分页无法加载或移动到第2页?

<?php          
    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = 'admin121';
     
    $rec_limit = 10;
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);	

    if(! $conn ) {
        die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db('misdb');
         
    $sql = "SELECT count(S_ID) FROM student";
    $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 S_ID, LastName, FirstName ". 
           "FROM student ".
           "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 "EMP ID :{$row['S_ID']}  <br> ".
             "EMP NAME : {$row['LastName']} <br> ".
             "EMP SALARY : {$row['FirstName']} <br> ".
             "--------------------------------<br>";
    }
         
    if( $page > 0 ) {
        $last = $page - 2;
        echo "<a href = \"$_PHP_SELF?page = $last\">Last 10 Records</a> |";
        echo "<a href = \"$_PHP_SELF?page = $page\">Next 10 Records</a>";
    } else if( $page == 1 ) {
        echo "<a href = \"$_PHP_SELF?page = $page\">Next 10 Records</a>";
    } else if( $left_rec < $rec_limit ) {
        $last = $page - 2;
        echo "<a href = \"$_PHP_SELF?page = $last\">Last 10 Records</a>";
    }
    
    
    

1 个答案:

答案 0 :(得分:0)

您只需要删除空格,因此请更改:

\"$_PHP_SELF?page = $last\"

进入这个:

\"$_PHP_SELF?page=$last\"