I have a SQL query that outputs a bunch of rows that match a certain criteria. I'm trying to add pagination to this process so that it's easier to use.
My process is like this:
- Figure out the lower limit of this page, figure out higher limit of this page (ex. 1 - 20, 21 - 40, etc.)
- Pull those rows from the MySQL table
- List those rows
- If there are more than 20, add
floor($totalRows / 20)
pages - ???
- Pagination
My pagination seems to be working semi-properly, but the last page (whatever it may be) lists one less than the amount of rows it should. Here's my code:
$page = $_GET["p"];
$bL = (($page - 1) * 20) + 1;
$tL = $page * 20;
$total = mysql_num_rows(mysql_query("SELECT id FROM coupons WHERE zone = '$zone' AND dateExp > CURDATE()"));
if($tL > $total)
{
$tL = (($page - 1) * 20) + ($total % 20);
}
$gotRows = "SELECT * FROM coupons WHERE zone = '$zone' AND dateExp > CURDATE() ORDER BY retailerName LIMIT " . $bL . ", " . $tL;
//list all those rows
$numPages = floor($total / 20) + 1;
echo "<p id='results'>" . $total . " result" . $plural . " · Displaying " . $bL . " to " . $tL . " </p>";
echo "</table>";
$pG = 1;
while($pG <= $numPages)
{
if($pG == $page)
{
echo $pG;
}
else
{
echo "<a href='zone?z=" . $zone . "&p=" . $pG . "'>" . $pG . "</a>";
}
if($pG != $numPages)
{
echo "·";
}
$pG = $pG + 1;
}
Any help?
Edit: Nevermind, I solved the rows problem. But my problem with the page links on the bottom being wrong still persists. On page 1, it works properly - "1" is not a link, "2" is. On page 2, however, only "1" as a link shows. "2" doesn't show.