Jump to content

Store array in session array?


TheBrandon

Recommended Posts

Hello all,

 

I have an error handler that I need to append messages to (First name not right, Last name not right, etc)

 

I'm using a session array to handle all error messages titled GORB.

 

How come this code won't work?

 

$_SESSION['GORB']['message'][] = "First name wrong";
$_SESSION['GORB']['message'][] = "Last name wrong";

 

How can I get it to work? I already have the handler output written and functioning fine, I just need to get it to loop over an array of errors instead of just one.

 

Link to comment
https://forums.phpfreaks.com/topic/258910-store-array-in-session-array/
Share on other sites

then $_SESSION['GORB']['message'] has been defined as a string, not an array.

You can't "push" values onto a string.

Posting the relevant code would help.

 

Also, I wouldn't use the $_SESSION superglobal array to store simple errors, complete waste of resources.

like was stated, relevant code will help, but to append, you could always use .=

$_SESSION['GORB']['message'] = "";
$_SESSION['GORB']['message'] .= "First name wrong<br/>";
$_SESSION['GORB']['message'] .= "Last name wrong<br/>";

 

EDIT:

--but scootstah's answer below is better :)

Fantastic. Assigning it as an array first totally fixed it. Thank you all for your help.

 

Maybe a noob question but I honestly thought all $_SESSION values were by default arrays?

 

Also on that note, AyKay47, would you mind telling me the ideal alternative to passing the errors through the session? They have to be carried from one PHP file to another; would the alternative be logging IP session keys with error messages in the database then simply removing them once they are displayed?

Also on that note, AyKay47, would you mind telling me the ideal alternative to passing the errors through the session? They have to be carried from one PHP file to another; would the alternative be logging IP session keys with error messages in the database then simply removing them once they are displayed?

 

Typically errors are triggered within the executing script, so they don't need to be saved.

However, if this is necessary, use $_SESSION's

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.