Re: can I do a for each here??

From: Date: Thu, 17 Mar 2005 15:02:04 +0000
Subject: Re: can I do a for each here??
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-210986@lists.php.net to get a copy of this message
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; }


« previous php.general (#210986) next »