i'm creating a e-cart.
i have a problem to update cart. I want to use session array to create cart, but i don't know how to do it.
can some help me to create SESSION ARRAY which can add several data.

$_SESSION = $details;

how to add several data for above array.
please help me??

Recommended Answers

All 7 Replies

It is just an array, use it like any other.

i'm creating a e-cart.
i have a problem to update cart. I want to use session array to create cart, but i don't know how to do it.
can some help me to create SESSION ARRAY which can add several data.

$_SESSION = $details;

how to add several data for above array.
please help me??

You will first have to start the session at the top of the document before using the $_SESSION array. The way it looks you are passing a single value to the $_SESSION assuming $details has all of the content like subtotal, descriptions of the items, etc..

session_start();
$_SESSION = $details;

<?php
//your page must start by calling session_start() first:
session_start();

//THEN you can begin saving data onto $_SESSION
//assuming that in YOUR example, $details is an array - ex:
$details=array('firstName'=>'john', 'lastName'=>'doe');

//then you just assign the array to $_SESSION
$_SESSION['ecart']=$details;
?>

page2.php
<?php
//If you need to retrieve the $_SESSION in page2.php, you MUST call session_start() first
session_start();

echo $_SESSION['ecart']['firstName'];
?>

thanks for the answers, but how can I save data in to session array in several times. I need to print array which look like table.

thanks for the answers, but how can I save data in to session array in several times. I need to print array which look like table.

following hielo's post you could assign the values like this. It is the essentially the same thing that hielo posted but with a different approach assigning the values to the array

<?php
session_start();

$details=array();
$details=$formValueFirstName;//can be the value from a form or db. if db then you will need to run a query to get the information
$details = $LastName;
$details = $subTotal;
.
.
..etc

//then you just assign the array to $_SESSION

$_SESSION=$details;
?>

//page2.php

<?php
. //here you would retrieve the values from the session variable and print it to the screen like hielo suggested. i will add a little table

session_start();

echo '<tableL>
<tr><td>'.$_SESSION.'</td><td>'.$_SESSION.'</td><td>'.$_SESSION.'</td></tr></table>';

?>

As eoliva explain the problem I got good understanding. But I need to add multipal data to the SESSION ARRAY from another php. As I explain this is shopping cart, need to add more product in several time in one session. still I couldn't compleate this. please help??

As eoliva explain the problem I got good understanding. But I need to add multipal data to the SESSION ARRAY from another php. As I explain this is shopping cart, need to add more product in several time in one session. still I couldn't compleate this. please help??

you should be able to call the session variable or array any were on your pages as long as you include session_start(); at the top of each document, then one way to do it, you could concatenate the ids and other information.

i.e. all ids will be separated by a '|' then all you would need to do is explode the array and separate them accordingly

//php file 1
<?php
session_start();
$_SESSION .= $itemID.'|';//note this will be any variable with the value
?>
//php file 2
<?php
session_start();
$_SESSION .= $itemID.'|';//here is another id that has been added to the cart
?>
//php file 2
<?php
session_start();
$_SESSION .= $itemID.'|';//here is another id that has been added to the cart
?>
//php file 3 here we will get the ids out of the session variable
<?php
session_start();
$cartIDs = $_SESSION;
$idsArr = explode('|', $cartIDs);
//then print all the ids or manipulate individually
$i=0;
while($i<sumof($idArr)){
echo $idArr.'item ID<br />';//you could generate a table here for printing of each item based on their id
$i++;
}
?>
hopefully, this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.