Jump to content


Photo

Session Variable Array


  • Please log in to reply
8 replies to this topic

#1 barkster

barkster
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 08 September 2006 - 10:00 PM

How do I put a array into a session variable?? I have a text field that is stored in as a comman seperated values and I need to dump it into a session and then acess the session like print $_SESSION['Names'][0] I tried this but it doesn't work.

$_SESSION['Names'] = array(split(",",$_POST['names']));

I was trying to access at $_SESSION['Names'][0] but I get "array"

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 08 September 2006 - 10:08 PM

Do a
<?php
echo '<pre>' . print_r($_SESSION['Names'],true) . '</pre>';
?>
to see what you put into that session variable. Then you should be able to figure out how to retrieve it.

Ken

#3 jefkin

jefkin
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 08 September 2006 - 10:11 PM

Maybe a better solution is to store your array data with serailize()/unserialize()

saver:

$_SESSION['names'] = serialize($names);

....

reader:

$names = unserialize($_SESSION['names']);

....


HTH

Jeff

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 08 September 2006 - 10:22 PM

Don't forget to put

session_start();

at top of script if you are going to be using $_SESSION
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 08 September 2006 - 10:24 PM

PS

$_SESSION['Names'] = explode(",",$_POST['names']);
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 08 September 2006 - 11:37 PM

To jefkin:
You don't need to serialize any data that is put into a session variable, since all data is automagically serialized when the session array is stored.

Ken

#7 jefkin

jefkin
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 08 September 2006 - 11:45 PM

To jefkin:
You don't need to serialize any data that is put into a session variable, since all data is automagically serialized when the session array is stored.

Ken


Izat so!  ;D

learn something new every day!

Thanks Ken.

#8 barkster

barkster
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 09 September 2006 - 01:51 AM

Cool, wondered how you actually saw that, here is what I got so I assume I have to access it as multidimensional array$_SESSION['Names'][0][0]??  Why is it an array of arrays?

Array
(
    [0] => Array
        (
            [0] => 10
            [1] => John
        )

)

Thanks for all the suggestions, I'll try a the explode option also.

#9 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 09 September 2006 - 08:37 AM

Why is it an array of arrays?


Beacause your code "=array(split()) " makes it so.

split() (or explode which is slightly more efficient in this case) returns the array you need. You are then placing this array into the first element of an array by placing array(..) round it.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users