Jump to content

session array variable


elexion

Recommended Posts

hello, I am trying to add a function to my shopping cart that allows several items to be saved in a session array variable. But i am not quite sure how to do that.this is the current code i'm using. As you can see this only allows for 1 item to be stored when your not logged in.

<?php session_start() ?><hmtl><script type="text/javascript">history.back();</script><?phpinclude("connect.php");$usersname = (isset($_SESSION['email'])) ? $_SESSION['email'] : "";$producten = $_SESSION['producten'];echo $usersname;//this variable should make an empty array$product_array= array();$product = $_GET['product'];$console = $_GET['console'];$prijs = $_GET['prijs'];if($usersname != ""){$result = mysql_query("INSERT INTO tmp_shopping_cart(`productnaam`,`console`,`prijs`, `klant`) VALUES('$product', '$console', '$prijs', $usersname)");$_SESSION['numberProducts']++;}if($usersname == ""){//$_SESSION['productArray'] = ? I'm not quite sure how to fill this part in$_SESSION['producten'] = $product;$_SESSION['numberProducts']++;$_SESSION['prijs'] += $prijs;}?></html>

How do i keep adding "$product" to the array so i can store more then 1 item in my shopping cart?

Link to comment
Share on other sites

You want to add a product to the array?$_SESSION['productArray'][] = $product;
Yes, but once i've added a product and try to add another one i don't want the first to be replaced. I'm currently not sure if this line of code above works properly i can't access my site properly only through a proxy(for some reason my webhost has banned my IP adress......)
Link to comment
Share on other sites

Yes, but once i've added a product and try to add another one i don't want the first to be replaced. I'm currently not sure if this line of code above works properly i can't access my site properly only through a proxy(for some reason my webhost has banned my IP adress......)
The line of code I gave you adds a new element to the array with the value you assigned to it.
Link to comment
Share on other sites

The line of code I gave you adds a new element to the array with the value you assigned to it.
Would this be the proper way to print it?
echo $_SESSION['productArray'];

Sorry for asking i'm just trying to write the code and have to test it through a proxy which isn't working at all

Link to comment
Share on other sites

If you want to see the values in an array, use print_r()

print_r($_SESSION['productArray']);

To see more information about the values in the array, use var_dump()

var_dump($_SESSION['productArray']);

Link to comment
Share on other sites

If you want to USE elements in the array (not just look at them for debugging purposes), it works like this:echo $_SESSION['productArray'][0]; // first elementecho $_SESSION['productArray'][1]; // second elementetc.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...