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!
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.
uhmmm actually you got me confused.. yeah i did check your code and it's somehow logically correct though i cannot test it cause i dont have the full php code.. anyhow, what do you mean by "cant view the items over a number of pages"?
OK i did some reconstructing of your code but NOT ALL OF THEM.. read the comments that i placed so you could understand how to works and you'll just have to arrange the code
<?php
// SOMETHING IS MISSING HERE
// YOU SHOULD QUERY FIRST THE TOTAL NUMBER OF ITEMS SO THAT YOU CAN COMPUTE THE TOTAL NUMBER OF PAGES
// something like ("SELECT * BLAH BLAH BLAH BLAH");
$totalNumRowsQuery = mysql_query("SELECT * FROM BLAH BLAH BLAH") or die(mysql_error());
$totalNumRows = mysql_num_rows($totalNumRowsQuery);
// how many rows to show per page
$rowsPerPage = 6;
// compute the total number of pages
$totalNumberOfPages = $totalNumRows/$rowsPerPage;
// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}
else
{
// by default we show first page if the $_GET['page'] is not defined
$pageNum = 1;
}
// counting the offset... modified your formula
$offset = $pageNum * $rowsPerPage - ($rowsPerPage);
// i dont have any idea what this SQL statement means but execute it
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 ;";
}
// execute the statement and count the number of queried values
$result = mysql_query($sql);
//--------------------------- I STOPPED MODIFYING HERE----------------------------------//
//Basically, what you see above is the basics of pagination, the next thing you have to do is to just display the queried result
//As you can see, i modified your formula, and i renamed your MAX PAGE variable to TOTAL NUMBER OF PAGES
//Also I modified your Formula for computing the OFF SET cause it returns a wrong computation.
//From what i know, your CODE is correct but not logically arranged and some missing code..
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>";
// 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");
?>