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


session_start();

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

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

<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>The selected item has been added to the cart.</p>
	<p>You can continue browsing or go to your basket with the links 
	on the bottom of the page.</p>
	</fieldset><br>	
</div>

<div id="content">
<br>
<fieldset style="width:95%"><legend>ADD TO CART</legend>
<br>
<p><img width="120" src="images/shopping-cart.png" alt="Shopping Cart" style="float:left">
	
<?php

//show how many item in cart
echo "<p>You had " . sizeof($_SESSION['cart']) . " item(s) in you cart.</p>";
echo "<hr/><br><br><br><br>";


$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;
}

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

//check session array item added or not
$ref = 0;
while (list ($key, $val) = each ($_SESSION['cart'])) 
{
if ($val == $id)
{
$ref = 1;
}
}



//add the item to cart if it does not exist 
if ($ref != 1)
{
	if (sizeof($_SESSION['cart']) < 5)
	{
		//insert item id to session array
		array_push($_SESSION['cart'],$id);

		//display inserted item
		echo "<br><p>The ";
		echo "<a href='details.php?id=";
		echo $row->ItemID;
		echo "'>";
		echo $row->ItemName;
		echo "</a>";  
		echo " added to your cart.</p><br><hr/>";
	}
	else
	{
	echo "<br><p>Sorry... You are only allowed to order five different 
	types of products in an order. If you need more please make another
	order.</p><br><hr/>";
	}
}
else
{
echo "<br><p>This item already in your cart.</p><br><hr/>";
}

//links for navigation
echo "<p><a style='float: right' href='cart-display.php'>Go to cart</p></a>";
mysql_close($con);

?>


</fieldset>

</div>

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


</div>

</body>
</html>