Re: can I do a for each here??

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Yeah, after hitting the send button, I looked again at what I sent and realized something else. Having already caused PHP to put the stuff into an array, by the way I suggested constructing the form, you can eliminate the foreach loop, and just use the assignment operator to get PHP to copy the array from $_POST to $_SESSION (although, you might want to do some validation code, in which case you probably do want to use the foreach block, so that you have a chance to validate each of the submitted values, and the keys [sometimes it's usefull to validate the keys, to check to see if someone has forged a form submission with values other than what they are supposed to be using]).

But, assuming you aren't worried about validating, you could do like this:

$_SESSION['age'] = $_POST['age'];

But, honestly, I would still use the foreach loop, check the values of $name and $age to make sure they are legal, and in the correct format, and then assign them individually, as before.

Jeff

Jeff Schmidt wrote:

I would be tempted to do the following.

First, I would setup the html form so that the text boxes are named something like 'age[Andrea]', 'age[Bob]', etc. When the form submitted, this will give you an array, accessible as $_POST['age'] (or $_GET['age'] depending on whether you used POST or GET form submission method).

This array would look like ("Andrea" => "25", "Bob" => "13", etc).

Then, I would use the following:

foreach($_POST['age'] as $name => $age)
{
  setcookie("cookie[$name]", $age);
}



[snip]


With that in mind, I would alter my above code to be the following:

foreach($_POST['age'] as $name => $age)
{
  $_SESSION['age'][$name] = $age;
}


-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux