Reputation: 45
I am developing a simple online shop cart page using php and xml I have a issue while appending data to array. The working is that when we click on add to cart the id should append to an array and store in session variable:
<?php session_start();
if(!isset($_POST['addtocart']))
{
$_SESSION["array1"] =array();
array_push($_SESSION["array1"],$_GET["pid"]);
print_r($_SESSION["array1"]);
}
?>
It is not appending id only showing the id of the product that I clicked
Upvotes: 1
Views: 94
Reputation: 4201
You can short using array_push to session
$_SESSION['addtocart'][ ]=$_GET['pid'];
Upvotes: 0
Reputation: 130
try this one.
session_start();
if( !isset($_POST['addtocart']) )
{
if( !isset($_SESSION['array1']) ) $_SESSION["array1"] =array();
$_SESSION['array1'][] = $_GET['pid'];
}
print_r($_SESSION["array1"]);
Upvotes: 2
Reputation: 433
This should work well
<?php session_start();
$data = array();
if(!isset($_POST['addtocart']))
{
array_push($data, $_GET["pid"], "test", "more data");
print_r($data);
}
?>
Upvotes: 0