PHP Page Number failure - one extra erroneous page
Posted: Thu Jun 30, 2016 9:58 am
Got a bit of an issue here.
We have this page that shows all the categories assigned NOT to "ancilliaries". Then per category, it shows each product.
Because of how many there are, I want to split it into pages. I thought this would work via the first level script you will see, and indeed it does, except, it adds another page on the bottom that isn't needed.
On the final page (4) it shows the final category and final product to be shown. I know this by removing the page numbering element.
But put it back, and you get a 5th page, that's empty.
Why the 5th page?
There are 18 categories in all. Varied number of product per category.
We have this page that shows all the categories assigned NOT to "ancilliaries". Then per category, it shows each product.
Because of how many there are, I want to split it into pages. I thought this would work via the first level script you will see, and indeed it does, except, it adds another page on the bottom that isn't needed.
On the final page (4) it shows the final category and final product to be shown. I know this by removing the page numbering element.
But put it back, and you get a 5th page, that's empty.
Why the 5th page?
There are 18 categories in all. Varied number of product per category.
Code: Select all
<?php
$detect = new Mobile_Detect;
echo "<div class='head'><h1>all stone flooring</h1></div>";
// how many rows to show per page
$rowsPerPage = 5;
// by default we show first page
$pageNum = 1;
// if $_GET['pagenum'] defined, use it as page number
if (isset($_GET['pagenum'])) {
$pageNum = $_GET['pagenum'];
}
$offset = ($pageNum - 1) * $rowsPerPage;
$queryc = "SELECT p.id AS id, catid, category, c.priority AS priority FROM products AS p INNER JOIN categories AS c ON p.catid = c.id WHERE pause <> 'on' AND producttype <> 'ancilliaries' GROUP BY category ORDER BY category LIMIT $offset, $rowsPerPage";
$resultc = $pdo->query($queryc);
$count = 0;
$countcat = 0;
while ($rowc = $resultc->fetch(PDO::FETCH_OBJ))
{
$countcat ++;
echo "<div class='stone-type-header'";
if ($countcat == "1") { echo " style='margin-top: 1px'";}
echo "><h2>$rowc->category</h2></div>";
$count = 0;
$query = "SELECT * FROM products WHERE catid =:catid AND pause <> 'on' ORDER BY title";
$result = $pdo->prepare($query);
$result->execute(array(':catid' => $rowc->catid));
while ($row = $result->fetch(PDO::FETCH_OBJ))
{
$titlereplace = str_replace(" ", "-", $row->title);
$categreplace = str_replace(" ", "-", $row->category);
$count ++;
echo "<div class='thumbs-tile'><a href='/product/$categreplace/$row->id/$titlereplace'><img src='/images/productphotos/small/$row->photoprimary' alt='$row->title' />
<div class='thumbs-tile-overlay'>
<div class='thumbs-tile-overlay-inner'>
<div class='thumbs-tile-title'>$row->title</div>
<div class='thumbs-tile-price'>From ";
if ($row->price1 > "0" && $row->discount1 == "yes")
{
echo "<i class='fa fa-caret-down' aria-hidden='true' style='color: #ff0000'></i> £";
printf ("%.2f", $row->price1);
}
else
{
echo "£";
printf ("%.2f", $row->instoreprice1);
}
echo " + VAT</div></div>
</div></a>
</div>";
// if not on a mobile, ensure a clear line after ever four items, incase of dodgy image heights
if ( $detect->isMobile() && !$detect->isTablet() ) {
}
else
{
if (($count % 4) == 0)
{
echo "<div style='clear:both'></div>";
}
}}
echo "<div style='clear:both'></div>";
}
echo "<br/>";
$query = "SELECT COUNT(id) AS numrows FROM products WHERE pause <> 'on' AND producttype <> 'ancilliaries' GROUP BY category";
$result = $pdo->query($query);
$numrows = $result->fetchColumn();
$maxPage = ceil($numrows/$rowsPerPage);
// print the link to access each page
$self = $_SERVER['PHP_SELF'];
$nav = '';
for($page = 1; $page <= $maxPage; $page++)
{
if ($page == $pageNum)
{
$nav .= " <div class='pagelinkactive'>$page</div> "; // no need to create a link to current page
}
else
{
$nav .= " <a href=\"/stone-flooring/$page\" class='pagelink'>$page</a>";
}
}
// creating previous and next link
// plus the link to go straight to
// the first and last page
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " <a href=\"/stone-flooring/$page\" class='pagelink'><i class='fa fa-angle-left' aria-hidden='true'></i> Prev</a> ";
$first = " <a href=\"/stone-flooring/\" class='pagelink'><i class='fa fa-angle-double-left' aria-hidden='true'></i> First Page</a>";
}
else
{
$prev = ' '; // we're on page one, don't print previous link
$first = ' '; // nor the first page link
}
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " <a href=\"/stone-flooring/$page\" class='pagelink'>Next <i class='fa fa-angle-right' aria-hidden='true'></i></a>";
$last = " <a href=\"/stone-flooring/$maxPage\" class='pagelink'>Last Page <i class='fa fa-angle-double-right' aria-hidden='true'></i></a>";
}
else
{
$next = ' '; // we're on the last page, don't print next link
$last = ' '; // nor the last page link
}
// print the navigation link
echo "<div class='navpages'>" . $first . $prev . $nav . $next . $last;
echo "</div>";
?>