For some reason my session variable isn't been populated with my items as they are being passed? Can anyone point me in the direction why?
It will add an item into the array, however it will only display one at a time on the cart page. Meaning if I add one item A and then add an item B, item B will only be shown on the cart page.
This is just a test i'm playing with to help understand php a little more - obviously I need more practice!
Product Page
<?php
$result = mysql_query ('SELECT * FROM stock WHERE item="redplate"');
// fetch the resulting row as an associative array
while ($row = mysql_fetch_assoc ($result)){
echo '£', number_format( $row ['price'] / 100, 2, '.', ' ' );
}
?>
<form id="form1" name="form1" method="post" action="cart.php">
<input type="hidden" name="item" id="item" value="redplate" />
<input type="submit" name="button" id="button" value="Add to Shopping Cart" />
Shopping Cart Page
<?php session_start();
?>
<?php include 'dbconfig.php'//connects to database?>
<?php include 'connect.php'//checks my credentials?>
<?php
////Adding something to the shopping basket from product page
if (isset($_POST['item'])){
$item = $_POST['item'];
$wasFound = false;
$i = 0;
//If cart is not set or cart array empty
if (!isset($_SESSION['cart_array'])||count($_SESSION['cart_array']) < 1) {
//Run if the cart is empty or not set
$_SESSION['cart_array'] = array(1 => array('item' => $item, 'quantity' => 1));
}else{
//Run if the cart has at least one item
foreach ($_SESSION['cart_array'] as $each_item) {
$i++;
while (list($key, $value) = each($each_item)) {
if ($key == 'item' && $value == $item) {
//This item is already in our shopping cart so add one to quantity
array_splice($_SESSION['cart_array'], $i-1, 1, array(array('item' => $item, 'quantity' => $each_item['quantity']+1)));
$wasFound = true;
} //Close if condition
} //Close While loop
}//Close foreach loop
//
if ($wasFound == false) {
array_push($_SESSION['cart_array'],array('item' => $item, 'quantity' => 1));
}
}
}
?>
<?php
//////If a user empties their cart
if (isset($_GET['cmd'])&& $_GET['cmd']=="emptycart")
{
unset($_SESSION["cart_array"]);
}
/////This will empty their cart and reset array
?>
<?php
///// Puts cart together for shopper to view
$cartOutput="";
if (!isset($_SESSION['cart_array'])|| count($_SESSION['cart_array'])<1){
$cartOutput = "<h2 align='center'>Your shopping basket is empty</h2>";
} else {
$i = 0;
foreach ($_SESSION['cart_array'] as $each_item){
$i++;
$item_name = $each_item['item'];
$sql = "SELECT * from stock WHERE item = '$item'";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result)){
$product_name=$row['name'];
$price=$row['price'];
}
$cartOutput = "<h2>Your Shopping Basket $i</h2>";
$cartOutput = "Item Name: " .$each_item['item']. "<br />";
$cartOutput = "Item Quantity: ". $each_item['quantity']. "<br />";
//while (list($item, $price) = each($each_item)){
//$cartOutput = "$key: $value<br />";
// }
}
}
?>
//If user chooses to empty their shopping cart)
<?php
if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart")
{
unset($_SESSION["cart_array"]);
}
?>
<a href="cartaction.php?cmd=emptycart">Click Here to Empty Your Shopping Cart</a>