You are here: Re: [PHP] can I do a for each here?? « PHP « IT news, forums, messages
Re: [PHP] can I do a for each here??

Posted by Jeff Schmidt on 03/17/05 17:02

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;
> }
>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация