I wanted to make a simple shopping cart which takes a $_GET variable and puts it in the $_SESSION variable. The code I have tried is this:
<?php
session_start();
if (is_numeric($_GET['add'])) $_SESSION[(string)$_GET['add']] = 1; ?>
Because my item ids are numeric so I check first to stop random things to be added to the session variable. I then then do a var_dump to see the result. The first time I run the code with ?add=102 I get:
array(1) { [102]=> int(1) }
I then run the script again with ?add=108 I get:
array(1) { [108]=> int(1) }
What I want is:
array(2) { ["102"]=> int(1), ["108"]=> int(1) }
What am I doing wrong? My concept is to convert the $_GET variable to a string and store the quantity 1 and the string value of $_GET in $_SESSION associatively. This should allow me to add as many items as long as their id is not the same, which is what I want.
Here is alternatives I have tried:
strval($_GET['add']),
(string)($_GET['add']),
$_GET['add']
Nothing seems to work.
Any help would be appreciated.