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"]);
?>
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 ();
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.