Hi Everyone,
I am working on a cart that i am trying to build for a project but got some php error message; I have tried to sort myself same errors keep coming. The idea behind the cart is
[ul][li]- The person select a shoe size from a dropdown list
-If the item id and the size selected are already in cart, so the qty of that item increase by 1, but if it is the same id and a different size then, a new product is to be added.
-if the item id is not in the cart then, it is a new product…so it is added to the cart[/li]
[li][/li][/ul]
Here is my code:
[php]<?php
$items = array(
array(‘id’ => ‘1’, ‘desc’ => ‘Addidas Original FlipFlap’,‘price’ => 24.95, ‘size’=>‘424546’),
array(‘id’ => ‘2’, ‘desc’ => ‘Reebok Filas’,‘price’ => 200, ‘size’=>‘323845’),
array(‘id’ => ‘3’, ‘desc’ => ‘Songs of the Goldfish (2CD set)’,‘price’ => 19.99),
array(‘id’ => ‘4’, ‘desc’ => ‘Simply JavaScript (SitePoint)’, ‘price’ => 39.95)
);
session_start();
if (!isset($_SESSION[‘cart’])) {
$_SESSION[‘cart’] = array();
}
if (isset($_POST[‘action’]) and $_POST[‘action’] == ‘Buy’) {
$size="";
//Get the values for the post and make sure they are integers
$pid = filter_var( $_POST[‘id’], FILTER_VALIDATE_INT, array(‘min_range’=> 1) );
$size = $_POST[‘doSize’];
if (isset($_SESSION[‘cart’][$pid], $size)) {
$_SESSION[‘cart’][$pid][$size][‘quantity’]++;
print_r($_SESSION[‘cart’]);
}
else{
$_SESSION[‘cart’][$pid][$size]= array(‘id’=>$pid, ‘size’=>$size, ‘quantity’=>1);
print_r($_SESSION[‘cart’]);
}
}//END POST
include’catalog.html.php’;
//unset($_SESSION[‘cart’]);
?>
[/php]
[php]Here the HTML:
Product catalog table { border-collapse: collapse; } td, th { border: 1px solid black; }Your cart contains <?php echo count($_SESSION['cart']);?> item(s).
Item Description | Price | Size | |
---|---|---|---|
<?php echo $item['desc']; ?> | $<?php echo number_format($item['price'], 2); ?> | <?php if (isset($item['size'])) { ?> <?php foreach(str_split($item['size'], 2) as $size):?> <?php echo $size;?> <?php endforeach;?> <?php }?> |
|
All prices are in imaginary dollars.
[/php]When i add new product all works good. the quantity increase and all…But when items are added up to 3 items i got the [php]PHP ERROR MESSAGE[/php]
[php]Notice: Undefined offset: 38 in C:\wamp\www\shopping-cart\index.php on line 22[/php]
[php]Notice: Undefined index: quantity in C:\wamp\www\shopping-cart\index.php on line 22.[/php]
[php]Here is the result i actually trying to make
Array( [0]=> Array( [id]=> Array ( id)=> id [size ]=> size [quantity]=> x ) )
Array ( [1] => Array ( [1] => Array ( [id] => 1 [size] => 42 [quantity] => 3 ) )[/php]