izagaren123 0 Newbie Poster

ok i am currently having a problem with my shopping basket.

it works fine when you go to a product and select to add it to the basket, it shows on there and you can go back to shop by clicking on the products link and add as many items as you like.

but once i click on the basket link, it empties the cart. im guessing this has something to do with session as it must allocate a new id when the baset button is pressed. i have moved all the sessions to different places but to no avail. and i dont know how to set the session so that it lasts until the user closes the browser or for a set period of time.

here is the code for the basket page.

<?php
session_start();
?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="content-type" />

<link rel="stylesheet" type="text/css" href="sofas.css" /> 
<link rel="SHORTCUT ICON" href="images/BS.jpeg" /> 
<meta name="keywords" content="sofas, chairs, luxury, leather, modern, high class, fabric, belvedere sofas, belvedere, sofa, armchair, couch" /> 
<meta name="description" content="sofas, chairs, luxury, leather, modern, high class, fabric, belvedere sofas, belvedere, sofa, armchair, couch" /> 
<title>Luxury, Modern, Leather, Fabric, Corner, Reclining Sofas provided by Belvedere Sofas</title> 

</head> 
<body> 

<!--This element contains the banner at the top, which includes the logo and starts the page design-->

<div id="topbanner"></div>

<!-- This element is a container for all of the main elements in the site, left, centre and right-->

<div id="main">

				
<!-- This element is the left column and contains the links-->
					
					<div id="left">

					<a>To navigate round the site, please use the links below.</a><br /><br /><br />

					<a href="index.php" accesskey = "h"><img border = "0" src ="images/Home.jpg" alt = "Home"/></a><br /><br />

					<a href="products.php" accesskey = "i"><img border = "0" src ="images/Products.jpg" alt = "Products"/></a><br /><br />

					<a href="discounts.php" accesskey = "d"><img border = "0" src ="images/Discount.jpg" alt = "Discounts"/></a><br /><br />
			
					<a href="basket.php" accesskey = "b"><img border = "0" src ="images/Customer Details.jpg" alt = "Basket"/></a><br /><br />
				
					

</div>

					
<!-- This element is the centre column and contains information about the business-->

						<div id="centre" style="overflow-y: auto;overflow-x: hidden">
<h3>Basket</h3>
<br />

This is your shopping basket. It shows all the items that you have purchased throughout the website. Once you are satisifed with the items in your basket, please press "Next" at the bottom to proceed with inputting your details.
<br /><br />
<?php
include("dblog.php");

if (isset($_GET['id']))
 $id=$_GET['id'];
else
 $id=1;
 
if (isset($_GET['action']))
 $action=$_GET['action'];
else
 $action="empty";
 
switch($action)
{
case "add":
 if (isset($_SESSION['cart'][$id]))
            $_SESSION['cart'][$id]++;
 else 
     $_SESSION['cart'][$id]=1; 
break;
case "remove":
 if (isset($_SESSION['cart'][$id]))
 {
           $_SESSION['cart'][$id]--;
     if ($_SESSION['cart'][$id]==0) 
     unset($_SESSION['cart'][$id]);
 }
break;
case "empty":
 unset($_SESSION['cart']);
 
break;
}
if (isset($_SESSION['cart']))
 
 #echo "item in basket";
 
 {
 echo '<table border=1 cellspacing=0 width="500">';
 $total=0;
 foreach($_SESSION['cart'] as $id => $x)
  {
  
  $result = mysql_query("SELECT name,price FROM products WHERE itemid = '$id' union SELECT name,price FROM discount WHERE itemid = '$id'",$cxn) or die ("Unable to retrieve information from database");	
  
  $row = mysql_fetch_array($result);
  
  $name=$row['name'];
  $name=substr($name,0,40);
  $price=$row['price'];
  $line_cost=$price * $x;
  $total=$total+$line_cost;
  echo '<tr>';
  echo '<td align="left"> '.$name.' </td>';  
  echo '<td align="right"> x '.$x.' <a href="basket.php?action=remove&id='.$id.'">Reduce</a></td>';
  echo '<td align="right">£ '.$line_cost.'</td>';
  echo '</tr>';
  }
 echo '<tr>';
 echo '<td align="right"><br>Total = </td>';
 echo '<td align="right"><b><br> £'.$total.' </b></td>';
 echo '</tr>';
 echo '</table>';
 }
else
echo "Cart is empty";
?>

<br /><br />

<a href="basket.php?action=empty">Empty Cart</a>

<br /><br />
	

<a href="customerinfo.php">Next >></a>

				
<br />

</div>

<!-- This element is the right column and contains general customer information-->
						
							<div id="right">

							<img style="border:0;width:150px;height:100px" src ="images/phone.jpg" alt = "women speaking on the phone"/>

							<a><br />If you are having problems purchasing an item online or have a general query, then please ring the number below and a customer representative will get back to you.</a><br /><h2>0800 123 456</h2></div>
		

</div>
	
							
<!--This element is the bottom banner and closes up the web page design-->						
	
<div id="bottombanner"></div>

</body>
</html>

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.