|
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.
>
[Back to original message]
|