Jump to content

accumulating $_POST SOLVED with our thanks in last post


niche

Recommended Posts

I thought this code would allow me to accumulate separate $_POST inputs, but it won't cycle on the first condition. Any ideas? Is there a better way to test for whether an array is set? code:

if (isset($curacc2)) {  echo "here1";    $curacc2 = array_merge($curacc2,$_POST);} else {  echo "here2";  $curacc2 = $_POST;}echo var_dump($curacc2) . '<br/>'; 

Link to comment
Share on other sites

I could just merge $_POST to $_SESSION and accumulate them there, but I'd like to send them to an array dedicated to only accumulating inputs to $_POST. Is there another "$_SESSION - type" array that's available in php? EDIT: What about the $_COOKIE array?

Link to comment
Share on other sites

Maybe the answer is to create a multidimensional in $_SESSION. If that's possible, how would I add $_POST as a different dimension in $_SESSION? I've never created a new dimension from an array.

Link to comment
Share on other sites

That's what I was thinking. Please see post #4. How do I do that?

Link to comment
Share on other sites

Thanks. EDIT: $_SESSION['post'] is the first position. What's the notation for the second?

Link to comment
Share on other sites

I don't know what you mean by "accumulate". That code copies everything that is in $_POST into $_SESSION['post'] and overwrites what is already there. If $_POST is empty, then $_SESSION['post'] will be empty. If that's not what you're trying to do, explain exactly what you're going for. If you want to have statements on multiple pages that all process $_POST, and you want to collect all of those values from all submissions into one array, then you do use array_merge for that.

if (!isset($_SESSION['post']))  $_SESSION['post'] = array();$_SESSION['post'] = array_merge($_SESSION['post'], $_POST);

Link to comment
Share on other sites

Thanks justsomeguy. Works like a charm. Though I've forgotten what the difference is between !isset() and isset()?

Link to comment
Share on other sites

Thanks. What's the offset for the second element in the $_SESSION['post'] dimension? I thought this would work, but it didn't:

if (isset($_SESSION['post'][2])) {  echo $_SESSION['post'][2];  echo "HERE"; }

Link to comment
Share on other sites

I suppose it's not. I thought you could access an indexed array through offsets. Is there a function to convert indexed to numeric or access an indexed with offsets?

Link to comment
Share on other sites

Indexed is numeric, associative is when the names are strings. You can use a foreach loop if you want to loop through the array and add the items to an indexed array. You can also use the array_values function to get just the values and lose the keys.

Link to comment
Share on other sites

Thank-you very much for your help justsomeguy and astraaron. array_values() did the final trick. I can begin to put multidimensional arrays under my belt.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...