<?Php
/*
* @author Istvan Franko
* @copyright 2014
*/

session_start();

if (!isset($_SESSION['cart'])) 
{
  $_SESSION['cart']=array(); 
}
?>

<html>

<head>
<title>Natty Notebooks Inc.</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
</head>

<body>

<div id="container">

	<div id="header">
	<! header picture in css >
	</div>
	
	<div id="navigation">
		<?php include 'navigation-bar.php'; ?>
	</div>
	
<div id="aside">
	<!--Link for navigation-->
	<a href="product-list.php"><p style="text-align:center">Back to list</a></p>	

	<fieldset style="width:85%"><legend>HELP</legend>
	<p>You have removed an item from your cart.</p> 
	</fieldset><br>
</div>

<div id="content">
<br>
<fieldset style="width:95%"><legend>REMOVE FROM CART</legend>

<br>
<p><img width="120" src="images/shopping-cart.png" alt="Shopping Cart" style="float:left">

<?Php


$con = mysql_connect("localhost","*****","*****");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("university", $con);

//get the ID from the query string.

$id=$_GET['id']; //get the value from the address bar
if(!is_numeric($id))
{ //check is a number.
echo "Sorry, there appears to have been an error.";
exit;
}

//get item number from session array
$ref = ($_SESSION['cart'][$id]);

$query=Mysql_query("SELECT * FROM natty WHERE ItemID=$ref ");
$row=mysql_fetch_object($query);
echo mysql_error();


//display removed item
echo "<p>The ";
echo "<a href='details.php?id=";
echo $row->ItemID;
echo "'>";
echo $row->ItemName;
echo "</a>";  
echo " removed from your cart.<hr/><br><br><br><br>";

//remove item from session array
//unset($_SESSION['cart'][$id]);

echo "<br><hr/>";
echo "<a href='product-list.php'><p>Back to list</a>";
echo "<a style='float: right' href='cart-display.php'>Go to cart</p></a>";
mysql_close($con);

//rebuild the session array

//read session array to local array
while (list ($key, $val) = each ($_SESSION['cart'])) 
{
$cart[$key] = $val;
}

//check the local array length
$result = count($cart);

//delete session cart
unset ($_SESSION['cart']);

//create a session array
$_SESSION['cart']=array(); 

//loop for write back the local array to session without removed
for ($x=0; $x != $result; $x++)
{
	if ($cart[$x] != $ref)
	{
	array_push($_SESSION['cart'],$cart[$x]);
	}
}

?>


</fieldset>

</div>

<div id="footer">
<p align="center">Copyright &#169; Istvan Franko, 2014</p>
</div>


</div>

</body>
</html>