I need help with session variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vigour
Forum Newbie
Posts: 18
Joined: Thu Sep 29, 2005 2:04 am

I need help with session variables

Post by vigour »

I'm trying to understand session variables. I want to use them in a shopping basket to hold the items. But I do not know how, can someone please give me a easy example how to start the session, put data in it, delete data from it and echo data from it.
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

Code: Select all

session_start();

$value = 'valueinsession';

$_SESSION['name'] = $value;

echo $_SESSION['name'];

session_destroy();
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

remember that for each page that you want to use a session you have to put session_start() at the top of the page
vigour
Forum Newbie
Posts: 18
Joined: Thu Sep 29, 2005 2:04 am

Post by vigour »

mickd wrote:

Code: Select all

session_start();

$value = 'valueinsession';

$_SESSION['name'] = $value;

echo $_SESSION['name'];

session_destroy();
Thanks, that's a good start. But if I want to do this.

Code: Select all

// start session
session_start(); 

// make up item to go in shopping basket 
$item = array(); 
$item['ProductCode'] = "1"; 
$item['Description'] = "Car"; 
$item['Price'] = 240; 
$item['Quantity'] = 1; 

// add item to end of shopping basket 
$_SESSION['basket'][] = $item;

// echo it out
echo $_SESSION['basket'][];
This only outputs the word array. I want to put many $item arrays into the session basket and then echo them out from session basket. Yes I know I probably need a loop to do this, but I can't echo out anything at all except the word array.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Ok, the problem is with the line:

Code: Select all

<?php

echo $_SESSION['basket'][]; 

?>
When you refer to an array and use [] on the end (i.e. no indice specified) that creates a new indice, thus it will appear empty.

If you want to echo what the Basket contains, you'll need to use a loop, such as the following:

Code: Select all

<?php

foreach ($_SESSION['basket'] as $item) {

    print ("
    Product Code: {$item['ProductCode']}
    Description: {$item['Description']}
    Price: {$item['Price']}
    Quantity: {$item['Quantity']}
    \n\n");

}

?>
vigour
Forum Newbie
Posts: 18
Joined: Thu Sep 29, 2005 2:04 am

Post by vigour »

Thanks, works very nice.
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post by dude81 »

I hope this also works

Code: Select all

print_r($_SESSION['basket'])
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

print_r should also display the information but it wont be in a very neat way like

Code: Select all

foreach ($_SESSION['basket'] as $item) { 

    print (" 
    Product Code: {$item['ProductCode']} 
    Description: {$item['Description']} 
    Price: {$item['Price']} 
    Quantity: {$item['Quantity']} 
    \n\n"); 

}
would.
Post Reply