9 Replies - 19680 Views - Last Post: 20 February 2011 - 06:33 AM Rate Topic: -----

#1 Kkarinisme   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 07-February 09

$_SESSION as a multidimensional array

Posted 18 February 2011 - 07:42 AM

I am trying to create a $_SESSION multidimensional array. I have a php file which receives variables from a buynow button function. I have checked and the variables are being correctly received into the php file. I now want to put these variables in a session array; any way I program it, I get error codes. I am posting my most recent code attempt with the echo statement I have inserted to tell me when I have created the array correctly.

<?php

$artitem = $_POST['artitem']; 
$prodnum = $_POST['prodnum'];
$original = $_POST['original'];

$_SESSION['artchoice']=array('title'=>$title,'prodnum'=>$prodnum,'original'=>$original);
echo "$_SESSION['artchoice']['title']";
?>



Is This A Good Question/Topic? 0
  • +

Replies To: $_SESSION as a multidimensional array

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3802
  • View blog
  • Posts: 13,797
  • Joined: 08-August 08

Re: $_SESSION as a multidimensional array

Posted 18 February 2011 - 07:59 AM

It always helps to start a session with session_start().
;)
Was This Post Helpful? 0
  • +
  • -

#3 VolcomMky   User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 315
  • Joined: 13-May 09

Re: $_SESSION as a multidimensional array

Posted 18 February 2011 - 09:14 AM

What are the errors you are getting?

Because the array looks like it should work fine so I am thinking CTphpnwb has the answer for you unless your getting a different error.
Was This Post Helpful? 0
  • +
  • -

#4 Kkarinisme   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 07-February 09

Re: $_SESSION as a multidimensional array

Posted 18 February 2011 - 08:39 PM

Sorry, I should have said that I do a session_start() at the beginning of the program. This is just one small file in quite a large website. I am just now working to get it possible to make online purchases.

The error code is Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' for the echo command (line 8 in the code snippet. Thx, for any ideas.
Was This Post Helpful? 0
  • +
  • -

#5 Valek   User is offline

  • The Real Skynet
  • member icon

Reputation: 544
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: $_SESSION as a multidimensional array

Posted 18 February 2011 - 08:47 PM

Take the quotes out from around your session variable echo.

echo $_SESSION['artchoice']['title'];

This post has been edited by Valek: 18 February 2011 - 09:08 PM

Was This Post Helpful? 2
  • +
  • -

#6 Kkarinisme   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 07-February 09

Re: $_SESSION as a multidimensional array

Posted 19 February 2011 - 06:29 AM

View PostValek, on 18 February 2011 - 08:47 PM, said:

Take the quotes out from around your session variable echo.

echo $_SESSION['artchoice']['title'];



Thank you so much. That did solve the problem. I always get confused as to where to use quotes and where not to use them.
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3802
  • View blog
  • Posts: 13,797
  • Joined: 08-August 08

Re: $_SESSION as a multidimensional array

Posted 19 February 2011 - 06:33 AM

As a rule of thumb never use them unless you know that you need to, or at least have a specific reason for using them. In this case, there's no need or reason for using them around a variable. Because that variable is an array, using them causes a problem.
Was This Post Helpful? 1
  • +
  • -

#8 Kkarinisme   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 07-February 09

Re: $_SESSION as a multidimensional array

Posted 19 February 2011 - 06:38 AM

View PostCTphpnwb, on 19 February 2011 - 06:33 AM, said:

As a rule of thumb never use them unless you know that you need to, or at least have a specific reason for using them. In this case, there's no need or reason for using them around a variable. Because that variable is an array, using them causes a problem.



Thank you.
Was This Post Helpful? 0
  • +
  • -

#9 Valek   User is offline

  • The Real Skynet
  • member icon

Reputation: 544
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: $_SESSION as a multidimensional array

Posted 19 February 2011 - 06:53 AM

Additionally, you can make a value in an array work inside of double quotes without breaking out to concatenate. For example, echo "Session Variable: {$_SESSION['myvar']}"; would work exactly the same as echo "Session Variable: " . $_SESSION['myvar'];.

This post has been edited by Valek: 19 February 2011 - 06:54 AM

Was This Post Helpful? 1
  • +
  • -

#10 Kkarinisme   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 07-February 09

Re: $_SESSION as a multidimensional array

Posted 20 February 2011 - 06:33 AM

View PostValek, on 19 February 2011 - 06:53 AM, said:

Additionally, you can make a value in an array work inside of double quotes without breaking out to concatenate. For example, echo "Session Variable: {$_SESSION['myvar']}"; would work exactly the same as echo "Session Variable: " . $_SESSION['myvar'];.


Thank you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1