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

Posted by Jim Michaels on 02/12/06 06:35

are you accessing $errors as a global variable?
<?php function f(){$z=1;}$z=0; f(); print $z; ?>
0
<?php
function f(){global $z;$z=1;} $z=0;f();print $z; ?>
1

you can also access the variable as $GLOBAL['z'] from within f().

"Fernando Rodrνguez" <frr@easyjob.net> wrote in message
news:a33bd8413ab78c7eb45fad217ec@news.supernews.com...
>
> Hi,
>
> 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).
>
> Here's my code:
>
> ---------------------------------------------------------------------------
> // An array to keep all the error messages
> $errors = array();
>
> // get the checking functions (which use $errors)
> require('validating_fns.php');
>
> // Process errors only if the submit button has been pressed.
> if (!empty($_POST['Submit'])) {
>
> // Each time there's an error, add an error message to the error array
> // using the field name as the key.
> //checkFirstName( $_POST['first_name']);
> checkLastName( $_POST['last_name'] );
> checkEmail( $_POST['email_address'] );
> checkEmailAgain( $_POST['email_address'], $_POST['email_address_2'] );
> checkPhoneNumber( $_POST['phone_number']);
> checkAddress( $_POST['address_line_1']);
> checkCity( $_POST['city'] );
> checkZipCode( $_POST['zip_code'] );
> @checkCountry( $_POST['country'] ); // It might not be set, if there was
> nothing selected in the combobox
> @checkState( $_POST['state_or_province'] ); // It might not be set, if
> there was nothing selected in the combobox
> checkPassword( $_POST['password'] );
> etc...
> ---------------------------------------------------------------------------
>
> validating_fns.php looks like this:
>
> ---------------------------------------------------------------------
> <?php
>
> // Functions that check all fileds
>
> // first and last name
> function checkFirstName( $firstName ) {
> if (empty($firstName)) {
> $errors['first_name'] = 'Please enter your first name.';
> }
> }
>
> function checkLastName( $lastname ) {
> if (empty($lastName)) {
> $errors['last_name'] = 'Please enter your last name';
> }
> }
>
> etc....
> ---------------------------------------------------------------------
>
> 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:-)
>
> Thanks!
>
>

 

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

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация