You are here: Re: Problem filling an array « PHP Programming Language « IT news, forums, messages
Re: Problem filling an array

Posted by Pedro Graca on 01/19/06 22:17

Fernando Rodríguez wrote:
> I'm writing code to validate fields in a form before saving to a db. All
> the validating functions are in a separate script which is required. All
> the validating functions add an error message to an array if the data doesn't
> validate. I check if something went wrong with count($theArray).
<snip contents="code"/>
> Even though some fields do not validate, when I check the count($errors)
> I get 0. Nothing was added! =:-O
>
> What am I doing wrong? O:-)

The functions in the validating_fns.php do not access the $errors array
you defined in your main script; they access their own local variable by
the same name.

Either declare the $errors array global inside each function or pass it
as a parameter (I like this better)

/* declare $errors global */
function checkFirstName( $firstName ) {
global $errors;
if (empty($firstName)) {
$errors['first_name'] = 'Please enter your first name.';
}
}


/* pass $errors as a parameter (by reference, so that it can be changed) */
function checkLastName( $lastName, &$errors ) {
if (empty($lastName)) {
$errors['last_name'] = 'Please enter your last name.';
}
}

--
If you're posting through Google read <http://cfaj.freeshell.org/google>

 

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

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