This one has done my head in for hours and I don’t seem to be any closer to a fix.
What I’m doing is the following.
-If a GET ID and Count as passed
-See if a session is set. If not create one and pass in a single array. This part seems to work.
-If it is set get the value which is an array and check to see if there is an sub array with an ID in there.
– If it’s in there update the count in the array item within the array but the passed in Cnt value.
Update the main array and save it back to a session
“`
if (isset($_GET[‘ID’]) || isset($_GET[‘Cnt’])){
$ID = $_GET[‘ID’];
$Cnt = $_GET[‘Cnt’];
$ExpireTime = time() + 900;
if (isset($_SESSION[‘MySite’])){
$CartArray[] = $_SESSION[‘MySite’];
echo sizeof($CartArray) .”<BR>”;
print “<pre>”;
print_r($CartArray);
print “</pre>”;
$found = false;
foreach ($CartArray as $CartItem => &$value) {
if ($CartItem[‘ID’] == $ID) {
$CartItem[‘Cnt’] = $CartItem[‘Cnt’] + $Cnt;
$found = true;
break;
} else {
$newItem = [“ID”=>$ID, “Cnt”=>$Cnt];
array_push($CartArray, $newItem);
$_SESSION[‘MySite’] = $CartArray;
$CartArray = $_SESSION[‘MySite’];
break;
}
}
print “<pre>”;
print_r($CartArray);
print “</pre>”;
} else {
$newItem = [“ID”=>$ID, “Cnt”=>$Cnt];
$_SESSION[‘MySite’] = $newItem;
$CartArray[] = $_SESSION[‘MySite’];
}
echo “<hr>”;
print “<pre>”;
print_r($CartArray);
print “</pre>”;
}