473,472 Members | 2,148 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problem filling an array


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!
Jan 19 '06 #1
3 1489
This is not how I would do it, but, you need global $errors; in every
check function.

I would have used 1 function that checked them all at once.

Jan 19 '06 #2
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>
Jan 19 '06 #3
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" <fr*@easyjob.net> wrote in message
news:a3*************************@news.supernews.co m...

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!

Feb 12 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: luigi | last post by:
Hi, I am trying to speed up the perfomance of stl vector by allocating/deallocating blocks of memory manually. one version of the code crashes when I try to free the memory. The other version...
24
by: Bangalore | last post by:
Hi all, I have a problem in accessing elements using overloaded operator . Consider, const int SIZE=10; int FALSE=0; class Array { private: int x; public:
8
by: dbuser | last post by:
Hi, I need help on a problem, as described below. I am reading a file "input.txt"which has data like this: abc def gh izk lmnopq rst uvwxyz I am using fstream object to read the file and...
1
by: rkmoray | last post by:
I have created a Multi Dimentional array, but having problems filling it. int mCount=0; mCount=ds.Tables.Rows.Count; string arrayppsa = new string ; DataTable myDataTable=ds.Tables;...
9
by: Víctor | last post by:
Hello, I'm filling a array of System.Diagnostic.Process by using GetProcesses() method. Due to a retard on this method, I do the call using a function and it passing like a delegate to one...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
2
by: drdave | last post by:
Hi All, I'm filling an arraylist with other arraylists within a loop and within the first iteration and addition all is fine.. however when I clear my value holding temporary array I lose the...
1
by: Dale | last post by:
I have a user control on a Windows Form that, in its Load event handler, calls a method from a class library to initialize an array of objects that are then used to populate a ComboBox. When I...
3
by: sk.rasheedfarhan | last post by:
Hi , Here I am new user to C#, my problem is I have to use dynamic Array of objects. But I heard C# don't support ptrs (using managed code C# support). In short i initialized objects of 1000...
12
by: ab12 | last post by:
I'm trying to write a program in C that gets a shape outlined with asterisks from the user, and returns that shape filled with asterisks. It will also get the coordinates of a point inside the shape...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.