hi...i am trying to print products added in a shopping cart....cart will be showed in table rows for each products, along with product name, quantity and cost...if a similar product already exists then instead of creating a new row in the cart then only the quantity value will be updated by adding previous quantity with the new quantity entered. if a new product is added to the cart then a new row will be printed and shown....now this is what i have done as below(plz see code)...please note how i am trying to print all the contents of the array using the loop....particularly in the section where the product with same name doesn't exist....with this whenever a new product is selected then instead of creating a new row it is actually creating another table....am i using the loop correctly?!?!?.....please help..
<?php
session_start();
?>
<html>
<head>
<title> Assignment 1 - Sayed Faiz and Denzil De Sousa </title>
<link rel="stylesheet" href="MainBodyStyle2.css" type="text/css">
</head>
<body>
<?php
if (!empty($_REQUEST['ProductName']))//if something is received from the form
{
$name = $_REQUEST['ProductName'];
if (!isset($_SESSION['cart']))//if session doesn't exists
{
$_SESSION['cart'][$name] = array($name, $_REQUEST['Quantity'], $_REQUEST['UnitPrice']);
print "<p class='style4'>Products in your Shopping Cart</p>";
print "<table border='0' class='style5'>";
print "<tr><td bgcolor='#00CC99'><div align='center'>Product Name</div></td><td bgcolor='#00CC99'><div align='center'>Quantity Added</div></td><td bgcolor='#00CC99'><div align='center'>Product Cost</div></td></tr>";
print "<tr bgcolor='#0099CC'>";
print "<td bgcolor='#00CCCC'><div align='center'>".$_SESSION['cart'][$name][0]."</div></td><td bgcolor='#00CCCC'><div align='center'>".$_SESSION['cart'][$name][1]."</div></td><td bgcolor='#00CCCC'><div align='center'>$".$_REQUEST['UnitPrice']*$_SESSION['cart'][$name][1]."</div></td>";
print "</tr>";
print "<tr>";
print "<td> </td><td bgcolor='#0099CC' class='style1'><div align='center'>Total Cost</div></td><td bgcolor='#0099CC' class='style1'><div align='center'> $".$_SESSION['cart'][$name][0]."</div></td>";
print "</tr>";
print "<tr><td><form name='bottomright' action='Purchase.php' target='topright' method='post'><div align='center'><input type='submit' value='CHECKOUT'></div><td><div align='center'><input type='button' value='Empty Cart' onClick='self.location=\"clearcart.php\"' TARGET='bottomright'></div></td></tr>";
print "</table>";
}
else //if session exists
{
if (!isset($_SESSION['cart'][$name])) //if product with same
{
$_SESSION['cart'][$name] = array($name, $_REQUEST['Quantity'], $_REQUEST['UnitPrice']);
for($i = 0;$i<sizeof($_SESSION['cart']);$i++)
{
print "<p class='style4'>Products in your Shopping Cart</p>";
print "<table border='0' class='style5'>";
print "<tr><td bgcolor='#00CC99'><div align='center'>Product Name</div></td><td bgcolor='#00CC99'><div align='center'>Quantity Added</div></td><td bgcolor='#00CC99'><div align='center'>Product Cost</div></td></tr>";
print "<tr bgcolor='#0099CC'>";
print "<td bgcolor='#00CCCC'><div align='center'>".$_SESSION['cart'][$i][0]."</div></td><td bgcolor='#00CCCC'><div align='center'>".$_SESSION['cart'][$i][1]."</div></td><td bgcolor='#00CCCC'><div align='center'>$".$_REQUEST['UnitPrice']*$_SESSION['cart'][$i][1]."</div></td>";
print "</tr>";
print "<tr>";
print "<td> </td><td bgcolor='#0099CC' class='style1'><div align='center'>Total Cost</div></td><td bgcolor='#0099CC' class='style1'><div align='center'> $".$_SESSION['cart'][$i][0]."</div></td>";
print "</tr>";
print "<tr><td><form name='bottomright' action='Purchase.php' target='topright' method='post'><div align='center'><input type='submit' value='CHECKOUT'></div><td><div align='center'><input type='button' value='Empty Cart' onClick='self.location=\"clearcart.php\"' TARGET='bottomright'></div></td></tr>";
print "</table>";
}
}
else //if product with same name exists
{
$qty = $_REQUEST['Quantity'];
$_SESSION['cart'][$name][1] += $qty;
for($i = 0;$i<sizeof($_SESSION['cart']);$i++)
{
print "<p class='style4'>Products in your Shopping Cart</p>";
print "<table border='0' class='style5'>";
print "<tr><td bgcolor='#00CC99'><div align='center'>Product Name</div></td><td bgcolor='#00CC99'><div align='center'>Quantity Added</div></td><td bgcolor='#00CC99'><div align='center'>Product Cost</div></td></tr>";
print "<tr bgcolor='#0099CC'>";
print "<td bgcolor='#00CCCC'><div align='center'>".$_SESSION['cart'][$name][0]."</div></td><td bgcolor='#00CCCC'><div align='center'>".$_SESSION['cart'][$name][1]."</div></td><td bgcolor='#00CCCC'><div align='center'>$".$_REQUEST['UnitPrice']*$_SESSION['cart'][$name][1]."</div></td>";
print "</tr>";
print "<tr>";
print "<td> </td><td bgcolor='#0099CC' class='style1'><div align='center'>Total Cost</div></td><td bgcolor='#0099CC' class='style1'><div align='center'> $".$_SESSION['cart'][$name][1]."</div></td>";
print "</tr>";
print "<tr><td><form name='bottomright' action='Purchase.php' target='topright' method='post'><div align='center'><input type='submit' value='CHECKOUT'></div><td><div align='center'><input type='button' value='Empty Cart' onClick='self.location=\"clearcart.php\"' TARGET='bottomright'></div></td></tr>";
print "</table>";
}
}
}
}
else //if nothing is received from the form
{
print "<p class='style2'>Your Shopping Cart is Empty</p>";
}
?>
</p>
</body>
</html>