You are here: Re: [PHP] Problem with header in an if SOLVED « PHP « IT news, forums, messages
Re: [PHP] Problem with header in an if SOLVED

Posted by Jochem Maas on 03/25/05 23:56

Jay Blanchard wrote:
> [snip]
> /* check for errors */
> $arrCheckItems = array(
> 'Name',
> 'Dept',
> 'Level',
> );
> /* check for blanks */
> foreach($_POST AS $key=>$value){
> if(in_array(niceName($key), $arrCheckItems)){
> $errorsReported[] = notBlank($key, $value);
> }
> }
>
> Even if the return from notBlank is nothing, $errorReported[] gets set
> for each item passed in the foreach loop. I have tried using conditional
> returns in the function, but to no avail. Perhaps my logic is screwed on
> this.
> [/snip]
>
> I changed to the following code and it solves the problem ...
>
> foreach($_POST AS $key=>$value){
> if(in_array(niceName($key), $arrCheckItems)){
> $nbr = notBlank($key, $value);
> if(!('' == $nbr)){
> $errorsReported[] = $nbr;
> }
> }
> }
>

<?

// one less if :-)...

foreach($_POST AS $key=>$value){
if(in_array(niceName($key), $arrCheckItems) && !('' == ($nbr = notBlank($key, $value)))) {
// error to report.
$errorsReported[] = $nbr;
}
}

// ...also array_filter() with only 1 arg (array) is handy for remove empty vals

$errorsReported = array();
foreach($_POST AS $k => $v){
if(in_array(niceName($k), $arrCheckItems)) {
$errorsReported[] = notBlank($k, $v);
}
}

if (count($errorsReported = array_filter($errorsReported))) {
// error msgs to report!
}

?>

quote from manual:

"If the callback function is not supplied, array_filter() will remove
all the entries of input that are equal to FALSE. See converting to
boolean for more information."

I do like array filter - also very handy in conjunction with create_function() :-)

rgds,
jochem

> Before I was directly assigning the value of the return from the
> notBlank function to $errorsReported. Now something only gets assigned
> to $errorsReported if there is something to assign.
>

 

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

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