Page 1 of 2
Page Numbers always maxxing out - why?
Posted: Tue Nov 05, 2013 6:06 am
by simonmlewis
This is showing 60 per page, but it is maxxing out on the page numbers at the bottom, with 15+ pages.
I only want it to show 3 pages.
How do I make it do that?
Code: Select all
// 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>";
Re: Page Numbers always maxxing out - why?
Posted: Tue Nov 05, 2013 7:00 am
by Celauran
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.
Re: Page Numbers always maxxing out - why?
Posted: Tue Nov 05, 2013 7:04 am
by simonmlewis
Sorry you've lost me....
What should the $query be then?
Re: Page Numbers always maxxing out - why?
Posted: Tue Nov 05, 2013 9:05 am
by Celauran
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)?
Re: Page Numbers always maxxing out - why?
Posted: Tue Nov 05, 2013 9:12 am
by simonmlewis
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.
Code: Select all
$query = "SELECT COUNT(id) AS numrows FROM products WHERE pause = 'off' AND rcstock <> 'out of stock' LIMIT $offset,180";
This doesn't work either.
Re: Page Numbers always maxxing out - why?
Posted: Tue Nov 05, 2013 4:29 pm
by Eric!
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.
For better protection from this:
Code: Select all
$self=htmlspecialchars($_SERVER["PHP_SELF"], ENT_QUOTES, "utf-8")
Re: Page Numbers always maxxing out - why?
Posted: Wed Nov 06, 2013 4:05 am
by simonmlewis
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.
Re: Page Numbers always maxxing out - why?
Posted: Wed Nov 06, 2013 6:25 am
by Celauran
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.
Code: Select all
$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.
Re: Page Numbers always maxxing out - why?
Posted: Wed Nov 06, 2013 8:14 am
by simonmlewis
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.
<?php
$rowsPerPage = 60;
$pageNum = 1;
if(isset($_GET['pagenum']))
{ $pageNum = $_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' 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
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>";
?>
Re: Page Numbers always maxxing out - why?
Posted: Wed Nov 06, 2013 12:10 pm
by Celauran
Quick and untested, but this is basically what I meant.
Code: Select all
<?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>";
?>
Re: Page Numbers always maxxing out - why?
Posted: Wed Nov 06, 2013 12:17 pm
by simonmlewis
Blimey that's clever. I'm just trying to read through what you'd done, to really "get it".
Re: Page Numbers always maxxing out - why?
Posted: Wed Nov 06, 2013 1:55 pm
by Celauran
I only really changed two things.
Code: Select all
$pageNum = min(3, intval($_GET['pagenum']));
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.
Code: Select all
$numrows = min(180, $row['numrows']);
Same basic principle here. If the number of rows is fewer than 180, use that, otherwise cap at 180
Re: Page Numbers always maxxing out - why?
Posted: Thu Nov 07, 2013 2:10 am
by simonmlewis
Brilliant. I should use that, those mostly the intval one on our category and search pages to avoid dodgy requests.
Re: Page Numbers always maxxing out - why?
Posted: Thu Nov 07, 2013 2:15 am
by simonmlewis
Can you do this?
Code: Select all
if(isset($_GET['pagenum']))
{
$pageNum = ((intval($_GET['pagenum']));
}
To use the inval but not restrict the page numbers?
So it can be 1, 2, 3, 4, 5, 6.... and so on, but NOT use words as the page number?
BTW you cannot use this as it errors on the ;.
Re: Page Numbers always maxxing out - why?
Posted: Thu Nov 07, 2013 5:36 am
by Celauran
simonmlewis wrote:Can you do this?
Code: Select all
if(isset($_GET['pagenum']))
{
$pageNum = ((intval($_GET['pagenum']));
}
To use the inval but not restrict the page numbers?
So it can be 1, 2, 3, 4, 5, 6.... and so on, but NOT use words as the page number?
BTW you cannot use this as it errors on the ;.
I see no reason this would cause errors. What error is it generating?