The below script counts the number of entries in the database as a total and then displays 5 entries per page with Previous and Next buttons for pagination. The problem with this code is that the same 5 entries appear on every page. I am not sure about my SELECT statement on line 18 and the specifics on line 9 with GET. Your help would be greatly appreciated. Thank you in advance.
<?php
$db = odbc_connect('','',''); //put your connection here
function inv_rows($r1) {
ob_start();
(int)$number=odbc_result_all($r1);
ob_clean();
return $number;
}
$page = isset($_GET["page"]) ? $_GET["page"] : 1; //not sure of "page" text
if(empty($page)){$page = 1; }
$query = "SELECT * FROM plastic"; // name of table is plastic with columns ename and iname.
$num_result = odbc_exec($db, $query);
$numrows = inv_rows($num_result);
echo '<p>There are '.$numrows.' ideas.</p>';
$limit = 5;
$limitvalue = $page * $limit - ($limit);
$limitnew = $limitvalue + $limit;
$sql = "SELECT * from (SELECT TOP 5 * from (SELECT TOP 5 * from plastic ORDER BY ename, iname DESC) as table1 ORDER BY ename, iname DESC) as table2 order by ename, iname ASC";
$result = odbc_exec($db, $sql);
while(odbc_fetch_row($result)){
?>
<table style="width: 600;">
<tr>
<td style="width: 300; height: 25px;">Name:</td>
td style="width: 300; height: 25px;">Idea Name:</td>
</tr>
<tr>
<td style="width: 300; height: 25px;"><?php echo odbc_result($result, "ename"); ?></td>
<td style="width: 300; height: 25px;"><?php echo odbc_result($result, "iname"); ?></td>
</tr>
<tr>
<td colspan="5" style="height: 25px"><hr/></td>
</tr>
</table>
<?php //PREVIOUS AND NEXT BUTTONS
}
if($page !=1){
$pageprev = $page - 1;
echo " <strong><a href='?page=$pageprev'>PREV</a></strong> "; }
else{ echo " PREV "; }
$numofpages = $numrows / $limit;
for($i = 1; $i <= $numofpages; ++$i){
if($i == $page){ echo " [$i] "; }
else{ echo " <strong><a href='?page=$i'>$i</a></strong> "; }
}
if(($numrows % $limit) != 0){
if($i == $page){ echo " [$i] "; }
else{ echo " <strong><a href='?page=$i'>$i</strong></b> "; }
}
if(($numrows - ($limit * $page)) > 0){
$pagenext = $page + 1;
echo " <strong><a href='?page=$pagenext'>NEXT</a></strong> "; }
else{ echo " NEXT "; }
odbc_free_result($result);
exit;
?>