You are here: Re: Perform the same action(s) on a variable number of POST variables « PHP Language « IT news, forums, messages
Re: Perform the same action(s) on a variable number of POST variables

Posted by peter on 06/15/07 13:36

>>
>>yup, treat $_POST as you would an array because it is an array
>>
>>example
>>foreach($_POST as $key => $item)
>>{
>>$$key = trim($_POST[$key]);
>>}
>>
>>key is a variable variable
>>
>>it would be the equivalent of this
>>
>>$apple = $_POST['apple'];
>>$orange = $_POST['orange'];
>>$pear = $_POST['pear'];
>>
>>etc..
>>
>>with the loop above you don't have to go thru the process of calling each
>>one out
>>it's done throu the loop - hope that makes sense
>>
>
> Thank you. I've never worked with foreach (Shock! Horror!) so it'll
> be interesting to see how I get along with it.

Do be weary of doing it exactly like that. you could inadvertantly overwrite
important variables. It would be a better option maybo to put them into a
new array much like:-

$clean = array();
foreach($_POST as $key => $item)
{
$clean[$key] = trim($item);
}

(there was no need to use the $_POST array within the foreach that the other
op provided)

if you do it the other way and you rely on say a variable called $logged_in
it would be easy for someone to inadvertantly overwrite that imporant
variable. Something else you could do is create a recursive function in case
there are arrays within the $_POST array.

 

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

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