Hey Guys,
I have been implementing a pagination script that I have used on numerous projects, into a new project.
It was previously working absolutely fine. But has recently started displaying all of the items on one page. It still correctly counts the number of records and displays the correct number of pages, but 'all' the items are on every single page.
The code is as follows
<?php
$per_page = 9;
$product_collection = $_GET['product_collection'];
$product_category = $_GET['product_category'];
if ($product_collection=="") {unset($product_collection);}
if ($product_category=="") {unset($product_category);}
//Connect to Database
$link=mysql_connect('localhost','user','password');
mysql_select_db('grah2005_shannon', $link);
if (!$link)
{
die('Could not connect: ' . mysql_error());
}
$cat = @mysql_query("select category from categories");
$col = @mysql_query("select collection from collections");
//**Collect variables for pagination
$t = mysql_query("SELECT product_id, product_name, price, product_description, product_collection, product_category, product_thumbnail FROM products ORDER BY product_id DESC");
if(!$t) {die(mysql_error());}
$a= mysql_fetch_object($t);
$total_items= mysql_num_rows($t);
echo "<h2>There are $total_items pieces to have a look at</h2>";
$limit= $_GET['limit'];
$page= $_GET['page'];
//set default if: $limit is empty, non numerical, less than 10, greater than 50
if((!$limit) || (is_numeric($limit) == false) || ($limit < $per_page) ) {
$limit = $per_page; //default
}
//set default if: $page is empty, non numerical, less than zero, greater than total available
if((!$page) || (is_numeric($page) == false) || ($page > $total_items)) {
$page = 1; //default
}
//calcuate total pages
$total_pages = ceil($total_items / $limit);
$set_limit = ($page * $limit) - $limit;
//prev. page
$prev_page = $page - 1;
if($prev_page >= 1) {
echo("<a href=http://www.grahamdrew.com/shannonhazell/$limit/$prev_page/products.php> <b><< Prev</b></a>");
}
//Display middle pages
for($a = 1; $a <= $total_pages; $a++)
{
if($a == $page) {
echo(" $a | "); //no link
} else {
echo(" <a href=http://www.grahamdrew.com/shannonhazell/$limit/$a/products.php> $a
</a> | ");
}
}
//next page
$next_page = $page + 1;
if($next_page <= $total_pages) {
echo("<a href=http://www.grahamdrew.com/shannonhazell/$limit/$next_page/products.php> <b>Next >></b> </a>");
}
echo "<hr /><br />";
//query for displaying records
$q = mysql_query("SELECT product_id, product_name, price, product_description, product_collection, product_category, product_thumbnail FROM products ORDER BY product_id DESC");
if(!$q) die(mysql_error());
if(mysql_num_rows($q) == 0) {echo "No products meet your requirements";}
//show data matching query:
while($code = mysql_fetch_object($q)) {
//Truncate $code variables
$id=$code->product_id;
$name=$code->product_name;
$price=$code->price;
$description=$code->product_description;
$collection=$code->product_collection;
$category=$code->product_category;
$thumbnail_draft=$code->product_thumbnail;
$thumbnail = "http://www.grahamdrew.com/shannonhazell/$thumbnail_draft";
//print data here.
echo "<div class='product_container'><div class='product_image_container' style='background-image: url($thumbnail)'></div><br/>$name<br />$price</div>";
}
//prev. page
echo "<hr /><br />";
$prev_page = $page - 1;
if($prev_page >= 1) {
echo("<a href=http://www.grahamdrew.com/shannonhazell/$limit/$prev_page/products.php> <b><< Prev</b></a>");
}
//Display middle pages
for($a = 1; $a <= $total_pages; $a++)
{
if($a == $page) {
echo(" $a | "); //no link
} else {
echo(" <a href=http://www.grahamdrew.com/shannonhazell/$limit/$a/products.php> $a
</a> | ");
}
}
//next page
$next_page = $page + 1;
if($next_page <= $total_pages) {
echo("<a href=http://www.grahamdrew.com/shannonhazell/$limit/$next_page/products.php> <b>Next >></b> </a>");
}
//all done
?>
I don't know if I've accidentally changed something. But I've been trying to debug it now for hours and I can't seem to find the problem. Apologies if it's glaringly obvious, but any help you guys could provide - I would be incredibly grateful for.
Thanks,