PHP return to previous page
I've created a product catalogue with category - subcategory - individual product page. I pass a $category to the individual product page. This page lists various details about the item, including which category the item belongs to. Since the $category variable is already set, I thought <a href="category.php?category=$category">Return to previous category</a>
would be enough to return it to the subcategory page but it doesn't, part of the category.php that handles this looks like this.
EDIT: Full code
product_list.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php
include "storescripts/connect_to_mysql.php";
?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
</style>
<meta name="Description" content="Lightweight aluminum boat docks, lifts, and accessories" />
<meta name="Keywords" content="Aluminum boat dock ladder lift water wheels" />
<script src="scripts/swfobject_modified.js" type="text/javascript"></script>
</head>
<title>Products</title>
<link rel="stylesheet" href="style/style.css" type="text/css" media="screen"/>
</style>
<body>
<div align="center" id="mainWrapper">
<?php include_once("template_header.php");?>
<table width="100%" border="0" cellspacing="0" cellpadding="15">
<tr>
<td valign="top" align="center"><table width="100%" border="0" align="center">
<tr>
<td align="center" valign="middle"><p>Aluminum Docks</p>
<p><a href="category.php?category=Aluminum_Docks"><img src="inventory_images/aluminum docks/PN99002-6_24x4_PKG_LG.jpg" width="25%" height="64"/></a></p></td>
<td align="center"><p>Floating Docks</p>
<p><a href="category.php?category=Floating_Dock"><img src="inventory_images/floating dock/100225279.jpg" width="25%" height="64"/></a></p></td>
<td align="center"><p>Frame Docks</p>
<p><a href="category.php?category=Frame_Dock"><img src="inventory_images/frame dock/frameDock.jpg" width="25%" height="64" alt="Frame Dock" /></a></a></p></td>开发者_运维百科
<td align="center"><p>Pipe Docks</p>
<p><a href="category.php?category=Pipe_Dock"><img src="inventory_images/pipe dock/PN99002_16X4_SECTION_LG.jpg" width="25%" height="64" alt="Pipe Dock" /></a></p></td>
</tr>
<tr>
<td align="center"><p>Boat Lifts</p>
<p><a href="category.php?category=Boat_Lifts"><img src="inventory_images/boat lifts/GM1060_LG.jpg" width="25%" height="64" alt="Boat Lift" /></a></p></td>
<td align="center"><p>Boat Lift Accessories</p>
<p><a href="category.php?category=Boat_Lift_Accessories"><img src="inventory_images/boat lift acceessories/canopy_lg (1).png" width="25%" height="64" alt="Boat Lift Accessory" /></a></p></td>
<td align="center"><p>Rollers & Caddies</p>
<p><a href="category.php?category=Rollers_and_Caddies"><img src="inventory_images/rollers and caddies/caddy270 (1).jpg"width="25%" height="64" alt="Caddy" /></a></p></td>
<td align="center"><p>Accessories</p>
<p><a href="category.php?category=Accessories"><img src="inventory_images/accessorries/2step_LG.png" width="25%" height="64" alt="Accessory" /></a></p></td>
</tr>
</table>
</table>
<p> </p>
<?php include_once("template_footer.php");?>
</div>
</body>
</html>
**category.php**
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php
// Run a select query to get my letest 6 items
// Connect to the MySQL database
include "storescripts/connect_to_mysql.php";
$category=$_GET['category'];
$sql = mysql_query("SELECT * FROM products WHERE category='$category' LIMIT 50");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
$dynamicList="";
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
$dynamicList .= '<table width="100%" border="0" cellspacing="0" cellpadding="6">
<tr>
<td width="17%" valign="top"><a href="product.php?id=' . $id . '" target="target_product"><img style="border:#666 1px solid;" src="inventory_images/' . $id . '.jpg" alt="' . $product_name . '" width="100" height="64" border="1" /></a></td>
<td width="83%" valign="top">' . $product_name . '<br />
$' . $price . '<br />
<a href="product.php?id=' . $id . '">View Product Details</a></td>
</tr>
</table>';
}
} else {
$dynamicList = "We have no products listed in our store yet";
}
mysql_close();
?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
</style>
<meta name="Description" content="Lightweight aluminum boat docks, lifts, and accessories" />
<meta name="Keywords" content="Aluminum boat dock ladder lift water wheels" />
<script src="scripts/swfobject_modified.js" type="text/javascript"></script>
</head>
<title><?php echo $category; ?></title>
<link rel="stylesheet" href="style/style.css" type="text/css" media="screen"/>
</style>
<body>
<div align="center" id="mainWrapper">
<?php include_once("template_header.php");?>
<div align="left">
<p><?php echo $dynamicList;?>
</p>
<div id="target_product"></div>
<p> </p>
</div>
<?php include_once("template_footer.php");?>
</div>
</body>
</html>
**product.php**
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php
include "storescripts/connect_to_mysql.php";
session_start();
//Check to see if the URL variable is set and exists in the database
if(isset($_GET['id'])){
$id=preg_replace('#[^0-9]#i','',$_GET['id']);
// Use this variable to see if the id exists, if yesthen get the product
// details, if no then exit the script and give message why.
$sql = mysql_query("SELECT * FROM products WHERE id ='$id' LIMIT 1");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
// Get all product details
while($row = mysql_fetch_array($sql)){
$product_name = $row["product_name"];
$price = $row["price"];
$details = $row["details"];
$quantity = $row["quantity_in_stock"];
$category = $row["category"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
}
}else{
echo "That item does not exist.";
exit();
}
}else{
echo "Data to render this page is missing.";
exit();
}
mysql_close();
?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
</style>
<meta name="Description" content="Lightweight aluminum boat docks, lifts, and accessories" />
<meta name="Keywords" content="Aluminum boat dock ladder lift water wheels" />
<script src="scripts/swfobject_modified.js" type="text/javascript"></script>
</head>
<title><?php echo $product_name; ?></title>
<link rel="stylesheet" href="style/style.css" type="text/css" media="screen"/>
</style>
<body>
<div align="center" id="mainWrapper">
<?php include_once("template_header.php");?>
<table width="100%" border="0" cellspacing="0" cellpadding="15">
<tr>
<td width="20%" valign="top" ><img src="inventory_images/<?php echo $id;?>.jpg" alt="<?php echo $product_name; ?>" />
<td width="80%" valign="top"> <h3><?php echo $product_name; ?></h3>
<p>$<?php echo $price; ?><br/>
<?php echo $category; ?>
<br/>
<?php echo $details; ?><br/>
<?php echo $quantity;?>
<p><a href="product_list.php?category=$category">Return to previous category</a></p>
</table>
<p> </p>
<?php include_once("template_footer.php");?>
</div>
</body>
</html>
If you want all data that was containing your previous page, use SESSION superglobal for this purpose.
精彩评论