Page Numbers always maxxing out - why?

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!

Moderator: General Moderators

simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Page Numbers always maxxing out - why?

Post 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  = '&nbsp;'; // we're on page one, don't print previous link
   $first = '&nbsp;'; // 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 = '&nbsp;'; // we're on the last page, don't print next link
   $last = '&nbsp;'; // 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Page Numbers always maxxing out - why?

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Page Numbers always maxxing out - why?

Post by simonmlewis »

Sorry you've lost me....
What should the $query be then?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Page Numbers always maxxing out - why?

Post 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)?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Page Numbers always maxxing out - why?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Page Numbers always maxxing out - why?

Post 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")
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Page Numbers always maxxing out - why?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Page Numbers always maxxing out - why?

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Page Numbers always maxxing out - why?

Post 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 = '&nbsp;'; // we're on page one, don't print previous link
$first = '&nbsp;'; // 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 = '&nbsp;'; // we're on the last page, don't print next link
$last = '&nbsp;'; // 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Page Numbers always maxxing out - why?

Post 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 = '&nbsp;'; // we're on page one, don't print previous link
	$first = '&nbsp;'; // 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 = '&nbsp;'; // we're on the last page, don't print next link
	$last = '&nbsp;'; // nor the last page link
}

// print the navigation link
echo "<div class='navpages'>" . $first . $prev . $nav . $next . $last . "</div>";
?>
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Page Numbers always maxxing out - why?

Post by simonmlewis »

Blimey that's clever. I'm just trying to read through what you'd done, to really "get it".
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Page Numbers always maxxing out - why?

Post 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
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Page Numbers always maxxing out - why?

Post by simonmlewis »

Brilliant. I should use that, those mostly the intval one on our category and search pages to avoid dodgy requests.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Page Numbers always maxxing out - why?

Post 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 ;.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Page Numbers always maxxing out - why?

Post 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?
Post Reply