Welcome Guest, Not a member yet? Register   Sign In
Passing $_POST array to session and then back out again
#1

[eluser]jtotheb[/eluser]
I've got some checkboxes that have the name example[] so that the values can go into an array.

What i want to do is store that in the session so they can amend their choice later.

So in the controller we have:

$test = array($_POST['example']);
$this->session->set_userdata($test);

Then in my model i have:

foreach ($this->session->userdata('test') as $key => $value)
etc etc

It errors saying "invalid argument supplied foreach()"

The session data is being stored in the database and i can see it is in there, but i cant see how to get it out!

Any help would be very gratefully appreciated.
#2

[eluser]kbauman[/eluser]
Well, I guess my first question would be, what session are you using? CI, Native, etc. I assume CI. If so do you want to get the information back from the cookie, or the database? You say the information is being stored in the database, but I believe in CI sessions "$this->session->userdata(’test’)" would only retrieve the session information from the cookie, not the database. I suppose you need to make sure the info is being added to the cookie, if that's where you want to retrieve it from. Otherwise you'd need to query the db to get the info.
#3

[eluser]jtotheb[/eluser]
I did omit a few details didn't i! I hate it when i do that!

I'm using Obsession and I'm storing the data in the database.

Do i really need to connect to the database to get the data out? I thought obsession would have a method to extract it as it has a method to insert it.

Thanks for replying, i appreciate it.
#4

[eluser]kbauman[/eluser]
That's a good question. When I tried to look at the documentation, the site was unavailable. I would think you'd be right though. The wiki makes no mention of have to query the db, only shows the use of "set_userdata()" and "userdata()."

I personally don't use OBsession, so I'm probably not the best one to answer this.

Sorry...
#5

[eluser]jtotheb[/eluser]
That's alright, it's the thought that counts!

I should really have a look at the source code and work it out for myself i suppose.

Still, if anyone else knows then that would be very helpful. Thanks
#6

[eluser]Seppo[/eluser]
Doesn't anyone else note something weird?
Code:
$test = array($_POST[’example’]);
$this->session->set_userdata($test); // Here!

//Then in my model i have:

foreach ($this->session->userdata(’test’) as $key => $value)
You are not assigning any key to the userdata you are storing

Try this
Code:
$test = array('test' => $_POST[’example’]);
#7

[eluser]kbauman[/eluser]
That's what I saw at first, except he said that it was being entered into the db. What's the deal. Maybe not enough of the code is being shown?
#8

[eluser]jtotheb[/eluser]
Oh dear. How embarrassing.

That was exactly the problem, i forgot to assign the array to a key.

Thanks for pointing that out!

kbauman - it is being entered into the db. With OBsession, you set the storage method in the config and that is where it puts anything you set. The cookie only holds the id.

Thanks very much seppo, i could have looked at that for hours and not noticed....




Theme © iAndrew 2016 - Forum software by © MyBB