PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
// how many rows to show per page
$rowsPerPage = 60;
// 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;
$query = "SELECT COUNT(id) AS numrows FROM products WHERE pause = 'off' AND rcstock <> 'out of stock' LIMIT 0,180";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $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 .= " $page "; // no need to create a link to current page
}
else
{
$nav .= " <a href=\"/productsnew/page/$page/\" class='bodylink'>$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='bodylink'>[Prev]</a> ";
$first = " <a href=\"/productsnew\" class='bodylink'>[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='bodylink'>[Next]</a>";
$last = " <a href=\"/productsnew/page/$maxPage/\" class='bodylink'>[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>";
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Limit clauses apply to the number of rows returned. You're only requesting one row which contains an integer representing the total number of rows in the table.
I can't say for certain since I can only see part of the code, but you're setting an offset and not using it, and you're never actually fetching the rows, just the count. Why not use the offset in your query, limit the number returned to 60 instead of 180 and, if you don't want to display records past 180, limit the offset to 120 (or page to 3)?
The only part of code not here, is the bit that shows the products. But the top few rows for the offset are from the 'top'.
I've not done this kind of 'count' before, as usually with page numbers, it's based on everything available. This time it has to be restricted.
While I didn't try to search your code for your coding error, your use of $_SERVER['PHP_SELF'] in your $self variable caught my attention. I hope you are at least filtering it somewhere before echoing it. Otherwise people can steal your user's cookies and infiltrate your system easily with an XSS attack.
Sorry you have now totally lost me. This thread is about the Page Count.
You are referring to something somewhere in my code, that I don't see. Please elaborate.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis wrote:The only part of code not here, is the bit that shows the products. But the top few rows for the offset are from the 'top'.
I've not done this kind of 'count' before, as usually with page numbers, it's based on everything available. This time it has to be restricted.
$query = "SELECT COUNT(id) AS numrows FROM products WHERE pause = 'off' AND rcstock <> 'out of stock' LIMIT $offset,180";
This doesn't work either.
So your count is either going to be 180 or the number of rows in the table, whichever is lower. That being the case, you'll want to get the count and min() it against 180. The limit clause in the query still does nothing. Where you'll want the limit clause is in the query that actually fetches the products, though there you'll want to use LIMIT $offset, 60. Here, too, you'll need to check that $offset does not exceed 120.
As you can see, I am asking for 60 products to show on one page.
So basically, I want three pages, each showing the top 60 pages - totalling 180 products.
I don't know how to get that count at the bottom of the page, to show the 3 pages. I assume there is maths involved to get it right, but still lost, sorry.
$offset = ($pageNum - 1) * $rowsPerPage;
echo "These are our very latest products on the web site. As new BB Guns and Accessories arrive, they will appear here FIRST!!";
$result = mysql_query ("SELECT * FROM products WHERE pause = 'off' ORDER BY id DESC LIMIT $offset, $rowsPerPage");
while ($row = mysql_fetch_object($result))
{ }
mysql_free_result($result);
echo "<br/><br/><div style='clear: both' /></div>";
$query = "SELECT COUNT(id) AS numrows FROM products WHERE pause = 'off' LIMIT $offset,180";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $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 .= " $page "; // no need to create a link to current page
}
else
{
$nav .= " <a href=\"/productsnew/page/$page/\" class='bodylink'>$page</a>";
}
}
// creating previous and next link
// plus the link to go straight to
// the first and last page
$last = " <a href=\"/productsnew/page/$maxPage/\" class='bodylink'>[Last Page]</a>";
}
else
{
$next = ' '; // we're on the last page, don't print next link
$last = ' '; // nor the last page link
}
<?php
$rowsPerPage = 60;
$pageNum = 1;
if (isset($_GET['pagenum'])) {
$pageNum = min(3, intval($_GET['pagenum']));
}
$offset = ($pageNum - 1) * $rowsPerPage;
echo "These are our very latest products on the web site. As new BB Guns and Accessories arrive, they will appear here FIRST!!";
$result = mysql_query ("SELECT * FROM products WHERE pause = 'off' ORDER BY id DESC LIMIT $offset, $rowsPerPage");
while ($row = mysql_fetch_object($result))
{ }
mysql_free_result($result);
echo "<br/><br/><div style='clear: both' /></div>";
$query = "SELECT COUNT(id) AS numrows FROM products WHERE pause = 'off'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$numrows = min(180, $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 .= " $page "; // no need to create a link to current page
} else {
$nav .= " <a href=\"/productsnew/page/$page/\" class='bodylink'>$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='bodylink'>[Prev]</a> ";
$first = " <a href=\"/productsnew\" class='bodylink'>[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='bodylink'>[Next]</a>";
$last = " <a href=\"/productsnew/page/$maxPage/\" class='bodylink'>[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>";
?>
Pagination notwithstanding, someone could still enter pagenum=4 to see past 180 products. This prevents that by setting $pageNum to the lowest of either 3 or what they entered. I've also used intval() so pagenum=tomato won't break the site. This could be more robust, catching negative numbers, for example.