File name is trial.php, don't get the desired result. Get this output - Previous | Page 1 | Page 2Next
Please tell me where is my mistake. Thanks
This is the code:
<?
$page=3;
$limit = 5;
$db = mysql_connect('localhost', 'root', '') or die('database not found');
mysql_select_db('school') or die('Could not select database');
$result = mysql_query("select count(*) from student");
$total = mysql_result($result, 0, 0);
// There are 7 records, $total=7
// work out the pager values
$pager = Pager::getPagerData($total, $limit, $page);
$offset = $pager->offset;
$limit = $pager->limit;
$page = $pager->page;
//Class Pager
class Pager
{
function getPagerData($total, $limit, $page)
{
$total = (int) $total;
$limit = max((int) $limit, 1);
$page = (int) $page;
$numPages = ceil($total / $limit);
$page = max($page, 1);
$page = min($page, $numPages);
$offset = ($page - 1) * $limit;
$ret = new stdClass;
$ret->offset = $offset;
$ret->limit = $limit;
$ret->numPages = $numPages;
$ret->page = $page;
return $ret;
}
}
// use pager values to fetch data
$query = "select lastname from student order by firstname limit $offset, $limit";
$result = mysql_query($query);
// use $result here to output page content
while ($row = mySQL_fetch_assoc($result))
{
echo $row['lastname'] . '<br>';
}
if ($page == 1) // this is the first page - there is no previous page
echo "Previous";
else // not the first page, link to the previous page
echo "<a href=\"trial.php?page=" . ($page - 1) . "\">Previous</a>";
for ($i = 1; $i <= $pager->numPages; $i++) {
echo " | ";
if ($i == $pager->page)
echo "Page $i";
else
echo "<a href=\"trial.php?page=$i\">Page $i</a>";
}
if ($page == $pager->numPages) // this is the last page - there is no next page
echo "Next";
else // not the last page, link to the next page
echo "<a href=\"trial.php?page=" . ($page + 1) . "\">Next</a>";
?>
:mad: