Reputation: 75
I have a script that works a cart-mechanism. I want to store a $_GET
value in a $_SESSION
array I just made , but it stores it only once. The other indexes are not getting added.
Here is my code:
<?php
session_start();
require_once 'db_connect.php';
$_SESSION['product_name']=array ();
array_push($_SESSION['product_name'],$_GET["p_name"]);
?>
Upvotes: 1
Views: 72
Reputation: 152
if(isset($_SESSION['cartitems'])) { #find index of an array if it exits then do not add else add to cart and then if (!array_key_exists($pr_id,$_SESSION['cartitems'])) { $_SESSION['cartitems'][$pr_id]['id'] = $pr_id; $_SESSION['cartitems'][$pr_id]['quantity'] = $quantity; } } else { $_SESSION['cartitems'][$pr_id]['id'] = $pr_id; $_SESSION['cartitems'][$pr_id]['quantity'] = $quantity; } $arr = array(); $arr['count'] = count($_SESSION['cartitems']); $arr['message'] = 'Item successFully added'; echo json_encode($arr);
Upvotes: -1
Reputation: 1436
you unset $_SESSION['product_name']
every time by this line:
$_SESSION['product_name']=array ();
change it like below:
if(!isset($_SESSION['product_name'])
$_SESSION['product_name']=array ();
Upvotes: 3