// how many rows to show per page
$rowsPerPage = 56;
// by default we show first page
$pageNum = 1;
// if $_GET['pagenum'] defined, use it as page number
if(isset($_GET['pagenum']))
{
$pageNum = $_GET['pagenum'];
}
// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;
$result = mysql_query ("SELECT * FROM products WHERE pause = 'off' AND stock <> 'out of stock' ORDER BY id DESC LIMIT $offset, $rowsPerPage");
while ($row = mysql_fetch_object($result))
{
This is my current script. It does this to enable page numbering further down. Each page can have 56 items on it.
I know LIMIT tells it how many to extract from the DB.
But here's the question: I want to also limit howo many "overall" it extracts.
So it shows 56 per page. 100 overall.
Any means of doing that?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Use another variable for the amount to find in the LIMIT that could be $rowsPerPage but caps at (however many it takes to only show up to the 100th row):
Put $rowCount in your query where $rowsPerPage currently is. You're wanting to change the number of rows returned depending on the offset. $rowsPerPage doesn't change, but $rowCount will.
@requinix - why the call to max()? Why not just a simple assignment?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
// if $_GET['pagenum'] defined, use it as page number
if(isset($_GET['pagenum']))
{
$pageNum = $_GET['pagenum'];
}
$rowsPerPage = 56;
// by default we show first page
$pageNum = 1;
// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;
// how many rows to show per page
if ($offset + $rowsPerPage > 100) {
$rowCount = max(0, 100 - $offset);
} else {
$rowCount = $rowsPerPage;
}
$result = mysql_query ("SELECT * FROM products WHERE pause = 'off' AND stock <> 'out of stock' ORDER BY id DESC LIMIT $offset, $rowCount");
while ($row = mysql_fetch_object($result))
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
$query = "SELECT * FROM products WHERE pause = 'off' AND stock <> 'out of stock' ORDER BY id DESC LIMIT $offset, $rowCount";
echo $query;
$result = mysql_query ($query);
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
pickle wrote:@requinix - why the call to max()? Why not just a simple assignment?
Making sure the $rowCount doesn't go below zero. Like if the page number was too high (because someone manipulated the URL) then the $offset would be >100 and the $rowCount would be negative. Assuming a negative number is a bad thing.
$rowsPerPage = 56;
// by default we show first page
$pageNum = 1;
// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;
// how many rows to show per page
if ($offset + $rowsPerPage > 100) {
$rowCount = max(0, 100 - $offset);
} else {
$rowCount = $rowsPerPage;
}
$query = "SELECT * FROM products WHERE pause = 'off' AND stock <> 'out of stock' ORDER BY id DESC LIMIT $offset, $rowCount";
echo $query;
$result = mysql_query ($query);
That produces:
SELECT * FROM products WHERE pause = 'off' AND stock <> 'out of stock' ORDER BY id DESC LIMIT 0, 56
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
If you're sorting by the ID because you want the most recent products first then you really should be sorting by an actual date field instead. The ID should always be treated like it was a mere (unique) number - don't give any additional meanings to it like "higher numbers mean more recent products", despite the fact that it may work like that.