How do I use PDO for this query of pagination?
Posted: Wed Mar 22, 2017 10:55 am
I am trying to upgrade our code, but these are quite old and I'm not sure sure how to convert this to PDO. Help?!
Code: Select all
$query = "SELECT COUNT(id) AS numrows FROM products WHERE pause = 'off' AND rcstock <> 'out of stock'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = min(160, $row['numrows']);
// how many pages we have when using paging?
$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
{
if ($page == "1")
{
$nav .= " <a href=\"/productsnew\" class='pagelink'>$page</a>";
}
else
{
$nav .= " <a href=\"/productsnew/page/$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=\"/productsnew/page/$page\" class='pagelink'>Prev</a> ";
$first = " <a href=\"/productsnew\" class='pagelink'>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=\"/productsnew/page/$page\" class='pagelink'>Next</a>";
$last = " <a href=\"/productsnew/page/$maxPage\" class='pagelink'>Last Page</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 . "</div>";