Hi,
I have a basket page made up of the contents of a session, If I have 3 products in by basket and dump the session array to screen, this is what I see.
array (
0 => class stdClass { public $brand_name = 'Armani City Glam'; public $brand_id = '17'; public $price = '32.99'; public $product_name = 'Armani City Glam 100ml EDT'; public $bid = '17'; public $pid = '10'; public $mid = '11'; public $size = ''; public $manufacturer_name = 'Armani'; public $image = '/images/product/picture10.jpg'; public $stock = '100'; public $quantity = 1; },
1 => class stdClass { public $brand_name = 'Armani City Glam'; public $brand_id = '17'; public $price = '29.99'; public $product_name = 'Armani City Glam'; public $bid = '17'; public $pid = '7'; public $mid = '11'; public $size = '50ml EDT'; public $manufacturer_name = 'Armani'; public $image = '/images/product/picture7.jpg'; public $stock = '4'; public $quantity = 1; },
2 => class stdClass { public $brand_name = 'Joop Homme'; public $brand_id = '15'; public $price = '12.22'; public $product_name = 'spray'; public $bid = '15'; public $pid = '9'; public $mid = '10'; public $size = '12'; public $manufacturer_name = 'Joop!'; public $image = '/images/product/picture9.jpg'; public $stock = '10'; public $quantity = 1; },
)
The basket works great as I just loop over the items and print to screen, however, when I come to allowing customers to delete items from the basket something strange happens.
I use this code to delete an item from the session array:
PHP Code:
if (isset($_GET['id'])) {
unset($_SESSION['basket'][$_GET['id']]);
header ("Location: /basket/");
}
This is the anchor to achieve the delete:
PHP Code:
<a href="/basket/?id={$smarty.section.item.index}">Remove Item(s)</a>
If the visitor clicks the delete button for the 2nd or 3rd product in the list (on screen) the delete function works perfectly, the item is removed from the session array.
However, as soon as the delete link for the first item in the session is clicked, the whole session gets messed up. Clicking the first delete link produces:
array (
1 => class stdClass { public $brand_name = 'Armani City Glam'; public $brand_id = '17'; public $price = '29.99'; public $product_name = 'Armani City Glam'; public $bid = '17'; public $pid = '7'; public $mid = '11'; public $size = '50ml EDT'; public $manufacturer_name = 'Armani'; public $image = '/images/product/picture7.jpg'; public $stock = '4'; public $quantity = 1; },
2 => class stdClass { public $brand_name = 'Joop Homme'; public $brand_id = '15'; public $price = '12.22'; public $product_name = 'spray'; public $bid = '15'; public $pid = '9'; public $mid = '10'; public $size = '12'; public $manufacturer_name = 'Joop!'; public $image = '/images/product/picture9.jpg'; public $stock = '10'; public $quantity = 1; },
3 => class stdClass { public $brand_name = 'Joop Homme'; public $brand_id = '15'; public $price = '12.22'; public $product_name = 'spray'; public $bid = '15'; public $pid = '9'; public $mid = '10'; public $size = '12'; public $manufacturer_name = 'Joop!'; public $image = '/images/product/picture9.jpg'; public $stock = '10'; public $quantity = 1; },
)
The key 0 (the first item is deleted) thus leaving 1, 2 and in this case 3 available, sorry their were 4 products in this example. Does removing the 0 key cause problems? Is this what could be causing me problems?
If so , what is the best way of removing an item from a session array?
Bookmarks