TheBrandon Posted March 14, 2012 Share Posted March 14, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/258910-store-array-in-session-array/ Share on other sites More sharing options...
TheBrandon Posted March 14, 2012 Author Share Posted March 14, 2012 Forgot to mention, it's giving me the error: <b>Fatal error</b>: [] operator not supported for strings in <b>/edit_Contact_Info_Process.controllers.php</b> on line <b>134</b><br /> Quote Link to comment https://forums.phpfreaks.com/topic/258910-store-array-in-session-array/#findComment-1327326 Share on other sites More sharing options...
AyKay47 Posted March 14, 2012 Share Posted March 14, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/258910-store-array-in-session-array/#findComment-1327328 Share on other sites More sharing options...
tbare Posted March 14, 2012 Share Posted March 14, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/258910-store-array-in-session-array/#findComment-1327330 Share on other sites More sharing options...
scootstah Posted March 14, 2012 Share Posted March 14, 2012 To illustrate what AyKay said... // this is incorrect $array = 'blah'; $array[] = 'more blah'; $array[] = 'even more blah'; // this is correct $array = array(); $array[] = 'more blah'; $array[] = 'even more blah'; Quote Link to comment https://forums.phpfreaks.com/topic/258910-store-array-in-session-array/#findComment-1327331 Share on other sites More sharing options...
TheBrandon Posted March 14, 2012 Author Share Posted March 14, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/258910-store-array-in-session-array/#findComment-1327346 Share on other sites More sharing options...
scootstah Posted March 14, 2012 Share Posted March 14, 2012 Maybe a noob question but I honestly thought all $_SESSION values were by default arrays? $_SESSION itself is an array, but its keys do not have to be arrays. Quote Link to comment https://forums.phpfreaks.com/topic/258910-store-array-in-session-array/#findComment-1327347 Share on other sites More sharing options...
AyKay47 Posted March 14, 2012 Share Posted March 14, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/258910-store-array-in-session-array/#findComment-1327360 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.