help with paging
Posted: Mon Mar 24, 2008 8:49 am
Hi, i'm trying to implement paging to an auction site to display the items over a number of pages. I've been working from a tutorial i found on the net and so far only part of the code works. It displays the correct number of items on each page however you cant view the items over a number of pages. I hope this isnt too confusing and someone might be able to spot were im going wrong, thanks.
Code: Select all
<?php
include("config.php");
include("functions.php");
$validid = pf_validate_number($_GET['id'], "value", $config_basedir);
require("header.php");
// how many rows to show per page
$rowsPerPage = 6;
// by default we show first page
$pageNum = 1;
// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}
// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;
if($validid == 0) {
$sql = "SELECT items.* FROM items WHERE dateends > NOW() LIMIT $offset, $rowsPerPage ";
}
else {
$sql = "SELECT * FROM items WHERE dateends > NOW() AND cat_id = " . $validid . " LIMIT $offset, $rowsPerPage ;";
}
$result = mysql_query($sql);
$numrows = mysql_num_rows($result);
echo "<h2>Items available</h2>";
echo "<table cellpadding='5'>";
echo "<tr>";
echo "<th>Image</th>";
echo "<th>Item</th>";
echo "<th>Bids</th>";
echo "<th>Price</th>";
echo "</tr>";
$maxPage = ceil($numrows/$rowsPerPage);
// print the link to access each page
$self = $_SERVER['PHP_SELF'];
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
$first = " <a href=\"$self?page=1\">[First Page]</a> ";
}
else
{
$prev = ' [Prev] '; // we're on page one, don't enable 'previous' link
$first = ' [First Page] '; // nor 'first page' link
}
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " <a href=\"$self?page=$page\">[Next]</a> ";
$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
}
else
{
$next = ' [Next] '; // we're on the last page, don't enable 'next' link
$last = ' [Last Page] '; // nor 'last page' link
}
if($numrows == 0) {
echo "<tr><td colspan=4>No items!</td></tr>";
}
else {
while($row = mysql_fetch_assoc($result)) {
$imagesql = "SELECT * FROM images WHERE item_id = " . $row['id'] . " LIMIT 1";
$imageresult = mysql_query($imagesql);
$imagenumrows = mysql_num_rows($imageresult);
echo "<tr>";
if($imagenumrows == 0) {
echo "<td>No image</td>";
}
else {
$imagerow = mysql_fetch_assoc($imageresult);
echo "<td><img src='./images/" . $imagerow['name'] . "' width='100'></td>";
}
echo "<td>";
echo "<a href='itemdetails.php?id=" . $row['id'] . "'>" . $row['name'] . "</a>";
if($_SESSION['USERID'] == $row['user_id']) {
echo " - [<a href='edititem.php?id=" . $row['id'] . "'>edit</a>]";
}
echo "</td>";
$bidsql = "SELECT item_id, MAX(amount) AS highestbid, COUNT(id) AS numberofbids FROM bids WHERE item_id=" . $row['id'] . " GROUP BY item_id;";
$bidresult = mysql_query($bidsql);
$bidrow = mysql_fetch_assoc($bidresult);
$bidnumrows = mysql_num_rows($bidresult);
echo "<td>";
if($bidnumrows == 0) {
echo "0";
}
else {
echo $bidrow['numberofbids'] . "</td>";
}
echo "<td>" . $config_currency;
if($bidnumrows == 0) {
echo sprintf('%.2f', $row['startingprice']);
}
else {
echo sprintf('%.2f', $bidrow['highestbid']);
}
echo "</td>";
echo "<td>" . date("D jS F Y g.iA", strtotime($row['dateends'])) . "</td>";
echo "</tr>";
}
}
echo "</table>";
echo "<br>";
// print the page navigation link
echo "<p class='pages'>" . $first . $prev . " Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> pages " . $next . $last . "</p>";
require("footer.php");
?>