Hello,
A user was kind enough to provide a script that I could hack on, but I can't seem to get the prev | next linking working correctly. Can anyone see what I am doing wrong. It is bringing up all the items, no matter if I set the limit at 2 or 10.
The url is http://www.eleganttime.com/php-bin/phphsk/prevnext.php
and here is the code:
<?
include("db.php");
require_once("sc_fns.php");
db_connct();
mysql_select_db("icart");
$query = ("SELECT * FROM items");
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
for ($i=0; $i <$num_results; $i++)
{
$row = mysql_fetch_array($result);
echo $row["itemName"];
}
$limit=2; // rows to return
$numresults=mysql_query("select * from items order by itemName asc");
$numrows=mysql_num_rows($numresults);
// next determine if offset has been passed to script, if not use 0
if (empty($offset)) {
$offset=0;
}
$query = "select * from items order by itemName asc limit $offset, $limit";
$mysql_result = mysql_query($query, $mysql_link);
$num_rows = mysql_num_rows($mysql_result);
printf("<font color=\"#24377E\" size=\"6\">%s</font><font color=\"#24377E\"><br>\n", $categoryName);
echo "<b>$numrows Items</b></font><br>";
if ($offset==0) { // bypass PREV link if offset is 0
print(" ");
}
else
{
$prevoffset=$offset-10;
printf("<a href=\"$PHP_SELF?categoryID=%s&categoryName=%s&offset=%s¤t=%s&i=%s\">PREV</a> \n", $categoryID, $categoryName, $prevoffset, $current-1, $i-1);
}
// calculate number of pages needing links
$pages=intval($numrows/$limit);
// $pages now contains int of pages needed unless there is a remainder from division
if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}
for ($i=1;$i<=$pages;$i++) { // loop thru
$newoffset=$limit*($i-1);
if($current==$i)
{
print("<font size=\"5\" color=\"#FF0000\"><b>$i </font></b>");
}
else
{
printf("<a href=\"$PHP_SELF?categoryID=%s&categoryName=%s&offset=%s¤t=%s\">$i</a> \n", $categoryID, $categoryName, $newoffset, $i);
}
}
// check to see if last page
if (!(($offset/$limit)==$pages) && $pages!=1) {
$newoffset=$offset+$limit;
}
if($newoffset >= $numrows)
{
print(" ");
}
else
{
printf("<a href=\"$PHP_SELF?categoryID=%s&categoryName=%s&offset=%s¤t=%s&i=%s\">NEXT</a><p>", $categoryID, $categoryName, $newoffset, $current+1, $i+1);
}
print("<table width=\"500\" align=\"center\" border=\"0\" cellpadding=\"0\" cellspacing=\"3\">");
print("<tr><td align=\"left\" width=\"50\"><b>Order</b></td>
<td align=\"left\" width=\"100\"><b>Image</b></td>
<td align=\"left\" width=\"125\"><b>Item</b></td>
<td align=\"left\" width=\"125\"><b>Description</b></td>
<td align=\"right\" width=\"100\"><b>Price</b></td></tr>");
print("</table>");
mysql_select_db("icart");
$query = ("SELECT * FROM items");
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
for ($i=0; $i <$num_results; $i++)
{
$row = mysql_fetch_array($result);
print("<BR><table width=\"500\" align=\"center\" border=\"0\" cellpadding=\"0\" cellspacing=\"3\">");
print("<tr>\n");
printf("<td align=\"left\" width=\"50\">Add Item</td>");
printf("<td align=\"left\" width=\"100\">No Image</td>");
printf("<td align=\"left\" width=\"125\">%s</td>", $row[1]);
printf("<td align=\"left\" width=\"125\">%s</td>", $row[2]);
printf("<td align=\"right\" width=\"100\">%s</td>", $row[3]);
print("</tr>");
print("</table>\n");
print("<hr>\n");
}
if ($offset==0) { // bypass PREV link if offset is 0
print(" ");
}
else
{
$prevoffset=$offset-10;
printf("<a href=\"$PHP_SELF?categoryID=%s&categoryName=%s&offset=%s¤t=%s&i=%s\">PREV</a> \n", $categoryID, $categoryName, $prevoffset, $current-1, $i-1);
}
// calculate number of pages needing links
$pages=intval($numrows/$limit);
// $pages now contains int of pages needed unless there is a remainder from division
if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}
for ($i=1;$i<=$pages;$i++) { // loop thru
$newoffset=$limit*($i-1);
if($current==$i)
{
print("<font size=\"5\" color=\"#FF0000\"><b>$i </font></b>");
}
else
{
printf("<a href=\"$PHP_SELF?categoryID=%s&categoryName=%s&offset=%s¤t=%s\">$i</a> \n", $categoryID, $categoryName, $newoffset, $i);
}
}
// check to see if last page
if (!(($offset/$limit)==$pages) && $pages!=1) {
$newoffset=$offset+$limit;
}
if($newoffset >= $numrows)
{
print(" ");
}
else
{
printf("<a href=\"$PHP_SELF?categoryID=%s&categoryName=%s&offset=%s¤t=%s&i=%s\">NEXT</a><p>", $categoryID, $categoryName, $newoffset, $current+1, $i+1);
}
?>