Session Variables as an Array

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
NotOnUrNelly
Forum Commoner
Posts: 61
Joined: Wed Mar 24, 2004 4:45 pm

Session Variables as an Array

Post by NotOnUrNelly »

Hi all,

Im doing an assignment for university that requires me to set up a simple shopping cart mechanism.

I have 2 pages currently the items list page, that has items with buttons.

Once the button is clicked the cartaction page is called and I am adding the recieved item name into separate session variables of the name sent as seen below

Code: Select all

$item = $_REQUEST['item'];
//echo $item;
switch ($item) {
case "coppercasserole":
   $HTTP_SESSION_VARS['coppercasserole'] = $HTTP_SESSION_VARS['coppercasserole'] + 1;
 //  echo "copperCasserole =".$HTTP_SESSION_VARS['coppercasserole'];
    break;
case "coppersaucepan":
$HTTP_SESSION_VARS['coppersaucepan'] = $HTTP_SESSION_VARS['coppersaucepan'] + 1;
//   echo "coppersaucepan =".$HTTP_SESSION_VARS['coppersaucepan'];
    break;
	case "coppersaute":
$HTTP_SESSION_VARS['coppersaute'] = $HTTP_SESSION_VARS['coppersaute'] + 1;
//   echo "coppersaute =".$HTTP_SESSION_VARS['coppersaute'];
    break;
}
This is working fine because each sessionvariable takes the name of the item. Then if the item is added again the value of the session variable is incremented.

Is there a better way of doing this using an array in the session variable. I not quite sur eon how this would work.

Thanks
Jamie
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

What about this?

Code: Select all

if (!isset($_SESSION[$item]))
{
  $_SESSION[$item] = 0;
}
$_SESSION[$item] = $_SESSION[$item] + 1;
NotOnUrNelly
Forum Commoner
Posts: 61
Joined: Wed Mar 24, 2004 4:45 pm

Post by NotOnUrNelly »

Thanks for your reply

Uhm,

Not sure how that would work,

What I am thinking is

session(cart)[typea][1]
[typeb][5]
[typec][9]

where the second number is the quantity added to the cart.

I'm ust not sure on how to do it.
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

NotOnUrNelly wrote:Thanks for your reply

Uhm,

Not sure how that would work,

What I am thinking is

session(cart)[typea][1]
[typeb][5]
[typec][9]

where the second number is the quantity added to the cart.

I'm ust not sure on how to do it.
And how do you propose to get the numbers of for example 'typea' this way?
It's now a problem to make the array you posted. The question is 'what for?'.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

I think you're right to want to put these in an array. Its a simple matter of just adding another index when you're updating the count. Also, $HTTP_SESSION_VARS is deprecated in favour of $_SESSION.

Code: Select all

$item = $_REQUEST['item'];
switch($item)
{
  case 'coppercasserole':
    $_SESSION['items']['coppercasserole']++;
    break;
  case 'coppersaucepan':
    $_SESSION['items']['coppersaucepan']++;
    break;
  case 'coppersaute':
    $_SESSION['items']['coppersaute']++;
    break;
}
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply