Shalu Awzme
Shalu Awzme

Reputation: 45

how to push data into array using array_push in php

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

Answers (3)

You can short using array_push to session


$_SESSION['addtocart'][ ]=$_GET['pid'];

Upvotes: 0

V. Prince
V. Prince

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

Ivan
Ivan

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

Related Questions