DiigIT | IT Community
No Profile Image
Welcome Guest
New User? Register | Login

Associative arrays to update Cart Cookie

By: | 24 Jul 2009 11:50 am

Embarassing - was ignoring PHP dynamics ... cookies do not mirror changes immediately.
Could anyone help out by giving an example of how the data from this array can be displayed on a page (html table) ?

Array ( [ECAcart] => Array (
[coppercasserole] => Array ( [qty] => 1 [desc] => Copper Casserole [price] =>

2199 )
[steelcasserole] => Array ( [qty] => 1 [desc] => Steel Casserole [price] => 2099
)
[steelfrying] => Array ( [qty] => 1 [desc] => Steel Frying [price] => 899 )
)
)

cheers

Comments

You didn`t say what variable all of this is stored in so I`ll call it $array.
print "<table border=`1`>";
foreach ($array[`ECAcart`] as $item)
{
printf("<tr><td>%s</td><td>%s<td><td>%s</td></tr>",
$item[`qty`], $item[`desc`], $item[`price`]
);
}
print "</table>";
Shopping carts are not stored in cookies for good reasons. A cookie is on the client machine and can be manipulated by the person who runs the browser. Hence your "prices" can be changed by them--very bad.

Instead, the session variables would be used to store all of your cart data. It is easy to put an array in the $_SESSION superglobal. The only thing that is stored in a cookie is the session_id which refers to the session variable on the server.

If you are serious about deploying this, I urge you to rethink your cart data storage strategy.

By: | 24 Jul 2009

Leave a comment

Enter the text in the image
img
Can't read?
Type the characters you see in the picture below.


Close Move