简体   繁体   中英

PHP pagination does not work

I have following code to display only 10 records at one page. When I load the page for the first time, first 10 records are displayed well (see screenshot) 第一次加载 . But when I click on "Next 10 records", the same URL is not loaded with extra GET parameters using $_SERVER['PHP_SELF']. My address bar shows this instead:

http://localhost/PHP/$_SERVER['PHP_SELF']?page=$page

Here is my complete code;

<?php
$con = @mysqli_connect( "localhost:3306", "root", "P@ssw0rd", "world" ) or die ("Couldn't connect to server");
//get total number of records
$query = "SELECT count(ID) FROM city";
$result = mysqli_query ( $con, $query) or die ("Couldn't execute SELECT query: ". mysqli_error($con));
$rec_count = mysqli_num_rows($result);

$rec_limit = 10; //number of records to display

if( isset($_GET['page']) ){ //when user clicks link
    $page = $_POST['page'] + 1; //page count increment by 1
    $offset = $rec_limit * $page;
}
else{ //user has not yet clicked any link
    $page = 0;
    $offset = 0;
}

//xxxxxxxx  $rec_count is taken from result set
$left_rec = $rec_count - ($page * $rec_limit); //number of records remaining to display

$display_query = "SELECT * FROM city LIMIT $offset, $rec_limit";
$display_result = mysqli_query ( $con, $display_query) or die ("Couldn't execute SELECT query: ". mysqli_error($con));

echo "<table>";
echo "<th>ID</th> <th>Name</th> <th>Country Code</th> <th>District</th> <th>Population</th>";
while( $row = mysqli_fetch_assoc($display_result) ){
    extract($row);
        echo "<tr>
              <td>$ID</td>
              <td>$Name</td>
              <td>$CountryCode</td>
              <td>$District</td>
              <td>$Population</td>
        </tr>";
}
echo "</table>";


if( $page == 0 ){ //user has not yet clicked any link
    echo '<a href="$_SERVER[\'PHP_SELF\']?page=$page"> Next 10 records </a>';
}
else if( $page>0 ){ //user has clicked at least once on link
    $last = $page - 2; //here -2 because, in isset block again increment by 1
    echo '<a href='. $_SERVER['PHP_SELF']. '?page=$last> Last 10 records </a>';
    echo '<a href='. $_SERVER['PHP_SELF']. '?page=$page> Next 10 records </a>';
}
else if( $left_rec < $rec_limit ){ //when only records less than 10 remains
    $last = $page - 2;
    echo '<a href='. $_SERVER['PHP_SELF']. '?page=$last> Last 10 records </a>';
}   

?>

 echo '<a href="'. $_SERVER['PHP_SELF']. '?page='.$page.'"> Next 10 records </a>';

和类似的$ last。

if( $page == 0 ){ //user has not yet clicked any link
    echo '<a href="'.$_SERVER['PHP_SELF'].'?page='.$page.'"> Next 10 records </a>';
}

This will fix your single and double quotes and will echo the values instead of the variables. You have to apply the same logic to all the inputs. You can also do:

if( $page == 0 ){ //user has not yet clicked any link
    echo "<a href=\"{$_SERVER['PHP_SELF']}?page=$page\"> Next 10 records </a>';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM