I want to display records from database using pagination, I am using the the code below it display the limit but when moving the next page or records it caot load. How to fix pagination moving next caot load or move to page 2?
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'admin121';
$rec_limit = 10;
$co = mysql_coect($dbhost, $dbuser, $dbpass);
if(! $co ) {
die('Could not coect: ' . mysql_error());
}
mysql_select_db('misdb');
$sql = "SELECT count(S_ID) FROM student";
$retval = mysql_query( $sql, $co );
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, $co );
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>";
}
if( $page > 0 ) {
$last = $page - 2;
echo "<a href = "count2.php?page = $last">Last 10 Records</a> |";
echo "<a href = "count2.php?page = $page">Next 10 Records</a>";
}else if( $page == 0 ) {
echo "<a href = "count2.php?page = $page">Next 10 Records</a>";
}else if( $left_rec < $rec_limit ) {
$last = $page - 2;
echo "<a href = "count2.php?page = $last">Last 10 Records</a>";
}
mysql_close($co);
?>
