The following code works but it fails when validating the HTML. Anyone have a better code to do pagination:
$limit = 6;
$con = mysql_connect("$server","$userid","$pass") or die ("Huh? What Server");
$db = mysql_select_db("$database",$con) or die("I said WHAT database");
if (empty($offset) || $offset < 0) {
$offset=0;
}
if (empty($index)) $index=0;
$getrows = mysql_query("select * from birdhouses", $con);
$numrows=mysql_num_rows($getrows);
$query = mysql_query("SELECT * from birdhouses, birdhouse_images where (birdhouses.imageid = birdhouse_images.imageid) limit $offset,$limit", $con);
echo "<table align=center width=100% border=0><tr>";
$num=1;
while ($result=mysql_fetch_array($query)){
$num = ($num < 4 ? $num : 1);
$index++; / Increment the line index by 1 /
echo "<td align=center valign=top class=title>
<br><b>$result[bird_name]</b><br>
<a href='displaybirdhouses.html?id=$result[bird_id]'><img border=0 height=156 width=116 alt='$result[bird_name]' src='images/birdhouses/$result[frontimage]'></a><br>$$result[bird_price]</td>";
if ($num==3 && $index!=$numrows)
echo '</tr><tr>';
$num++;
}
echo ("</tr></table>");
if ($numrows <= $limit) {
}
else {
if ($offset!=0) {
$prevoffset=$offset-$limit;
echo "<br><a onMouseOver=\"window.status='Previous $limit Results'; return true\"; href=\"$PHP_SELF?offset=$prevoffset&index=$prevoffset\"><B><b><br><br><p align='center'>[Previous]</B></a> ";
}
else echo "<b><br><br><p align='center'>[Previous]</b> ";
$pages = intval($numrows/$limit);
if ($numrows%$limit) {
$pages++;
}
for ($i=1;$i<=$pages;$i++) {
if (($offset/$limit) == ($i-1)) {
echo " <b>$i</b> ";
} else {
$newoffset=$limit*($i-1);
echo " <a onMouseOver=\"window.status='Page $i Results'; return true\"; href=\"$PHP_SELF?offset=$newoffset&index=$newoffset\"><B>$i</B></a> \n";
}
}
if (!((($offset/$limit)+1)==$pages) && $pages!=1) {
$newoffset=$offset+$limit;
echo " <a onMouseOver=\"window.status='Next $limit Results'; return true\"; href=\"$PHP_SELF?offset=$newoffset&index=$newoffset\"><B>[Next]</B></a><p>\n";
} else echo " <b>[Next]</b></p>";
}