I am unable to maintain a GET variable with this pagination script. Was hoping you all could help.
I am using a GET function so the user can choose which categories are displayed. I'm using a pagination script I found online and I am now confronted with this issue, when I choose "page 2" $dynCat no longer is parsed in the URL because the pagination script creates a new URL and uses GET also.
Could someone help me out with using pagination with a variable query? Thank you.
ERROR Notice: Undefined index: dynCat ----/dynCat.php on line 32
I understand why it is undefined, I just don't know how to integrate and maintain the user's Query (i.e. GET variable from URL) with the pagination script. Thanks again.
Page1 (User Selects option)
<?php
//Generate and list Categories
include "storescripts/connect_to_mysql.php";
$dynCat = "";
$data = mysql_query("SELECT category, id FROM products GROUP BY category") or die(mysql_error());
while($info = mysql_fetch_array( $data ))
{
$listcategory = $info["category"];
$dynCat .=
'
<li>
<a href="dynCat.php?dynCat='.$listcategory.'" > '.$listcategory.' </a>
</li>
';
}
mysql_close();
?>
<?php echo $dynCat; ?>
Page2 dynCat.php (User Views Selection with Pagination)
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php
//Generate and list Categories
include "storescripts/connect_to_mysql.php";
$dynGallery = "";
$dynCat = "";
$data = mysql_query("SELECT category, id FROM products GROUP BY category") or die(mysql_error());
while($info = mysql_fetch_array( $data ))
{
$listcategory = $info["category"];
$dynCat .=
'
<li>
<a href="dynCat.php?dynCat='.$listcategory.'" > '.$listcategory.' </a>
</li>
';
}
mysql_close();
?>
<?php
//Query User Selection & Pagination
include('storescripts/connect_to_mysql.php');
$cat = mysql_escape_string($_GET['dynCat']);
$tableName="products";
$targetpage = "dynCat.php";
$limit = 3;
$query = "SELECT COUNT(*) as num FROM $tableName WHERE category = '$cat'";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages['num'];
$stages = 3;
$page = mysql_escape_string(isset($_GET['page'])) ? mysql_escape_string($_GET['page']) : 0;
if($page){
$start = ($page - 1) * $limit;
}else{
$start = 0;
}
// Get category Data and Images
$query1 = "SELECT * FROM $tableName WHERE category = '$cat' ORDER BY views DESC LIMIT $start, $limit";
$result = mysql_query($query1);
// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$LastPagem1 = $lastpage - 1;
$paginate = '';
if($lastpage > 1)
{
$paginate .= "<div class='paginate'>";
// Previous
if ($page > 1){
$paginate.= "<a href='$targetpage?page=$prev#gallery'>previous</a>";
}else{
$paginate.= "<span class='disabled'>previous</span>"; }
// Pages
if ($lastpage < 7 + ($stages * 2)) // Not enough pages to breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter#gallery'>$counter</a>";}
}
}
elseif($lastpage > 5 + ($stages * 2)) // Enough pages to hide a few?
{
// Beginning only hide later pages
if($page < 1 + ($stages * 2))
{
for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter#gallery'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1#gallery'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage#gallery'>$lastpage</a>";
}
// Middle hide some front and some back
elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
{
$paginate.= "<a href='$targetpage?page=1#gallery'>1</a>";
$paginate.= "<a href='$targetpage?page=2#gallery'>2</a>";
$paginate.= "...";
for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter#gallery'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1#gallery'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage#gallery'>$lastpage</a>";
}
// End only hide early pages
else
{
$paginate.= "<a href='$targetpage?page=1#gallery'>1</a>";
$paginate.= "<a href='$targetpage?page=2#gallery'>2</a>";
$paginate.= "...";
for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter#gallery'>$counter</a>";}
}
}
}
// Next
if ($page < $counter - 1){
$paginate.= "<a href='$targetpage?page=$next#gallery'>next</a>";
}else{
$paginate.= "<span class='disabled'>next</span>";
}
$paginate.= "</div>";
}
mysql_close();
?>
<?php
$productCount = mysql_num_rows($result); // count the output amount
while($row = mysql_fetch_array($result))
{
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$category = $row["category"];
$subcategory = $row["subcategory"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
$dynGallery .=
'
<div>
<li>
<a href="product.php?id=' . $id . '#gallery">
<img style="border:#FFF 1px solid;" src="inventory_images/' . $id . '.jpg" alt="' . $product_name . '" width="180" height="255" border="1" style="opacity: 0;/></a>
<h4> <a name= ' . $id . ' id= ' . $id . ' value= ' . $id . ' href="product.php?id= ' . $id . '#gallery"> ' . $product_name . ' </a> </h4>
<p>'. $category .'</p>
<p>'. $subcategory .'</p>
<span> $' . $price . '</span>
</li>
</div>
';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<?php include ("../header.php");?>
<?php include ("../menu.php");?>
<a id="shop"></a>
<div class="body">
<div class="sidebar">
<div class="first">
<h2><a href="index.php">Dora Designs</a></h2>
<!-- Dynamic Categories -->
<ul>
<?php echo $dynCat; ?>
</ul>
</div>
<!-- <div>
<h2><a href="#">Weddings</a></h2>
</div>
<div>
<h2><a href="#">Birthdays</a></h2>
</div>
-->
</div>
<div class="content">
<div class="figure">
<img src="/images/galleryholder.png" alt=""/>
</div>
<div class="products">
<div class="paging" align = "center">
<a id="gallery"></a>
<? echo $paginate;?>
<? echo '</br>'. $cat;?>
<? echo '</br>'. $total_pages.' Results'; ?>
</div>
<ul>
<?php echo $dynGallery; ?>
</ul>