473,804 Members | 3,123 Online
Bytes | Software Development & Data Engineering Community
+ 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('valida ting_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'] );
checkPhoneNumbe r( $_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_provi nce'] ); // 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($firstNa me)) {
$errors['first_name'] = 'Please enter your first name.';
}
}

function checkLastName( $lastname ) {
if (empty($lastNam e)) {
$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 1514
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($firstNa me)) {
$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($lastNam e)) {
$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.ne t> wrote in message
news:a3******** *************** **@news.superne ws.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('valida ting_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'] );
checkPhoneNumbe r( $_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_provi nce'] ); // 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($firstNa me)) {
$errors['first_name'] = 'Please enter your first name.';
}
}

function checkLastName( $lastname ) {
if (empty($lastNam e)) {
$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
2977
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 seem to work. I would appreciate someone to comment on this. Version 1 (crashes on deallocating) #include <iostream>
24
2281
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
5232
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 writing into a dynamic array. My problem is that the array shows extra z and probably because of this further processing gives run time error in borland compiler. Can you please tell me, if the problem is related to handling end-of line , how do i do...
1
3247
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; foreach(DataRow myDataRow in myDataTable.Rows) {
9
1764
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 thread. I've defined the array like private at class level, but when thread terminates, array is void (I've checked that array isn't void before thread delegate terminate). Can somebody help me? I believed that by declaring vars like private in a...
2
4457
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 */ #include <time.h>
2
1757
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 values already in the output array.. the code.. '******** LOOP OVER THE PROVINCE ID VALUES ********************
1
1641
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 run the app, the method runs just fine. The class library returns the array of objects and the ComboBox is loaded. The problem I am having is when I try to open the form that contains the user control in the designer. Instead of the form, I get...
3
2639
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 and I am using is upto 10 or less. Because of that I find 990 reset of them as un initialized objects, when I extract the information from the Object it will throw an exception also and I feel unnecessarily I am wasting of memory. So I need dynamic...
12
4354
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 from which to start filling. I need to use recursion here. for example, to be clear: input: (ignore the line, think of that as blank space) ***** *___* ***** coordinates are (2,1) /*its an array so numbering will start from 0*/ output...
0
9708
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9587
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10588
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10085
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9161
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5662
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4302
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 we have to send another system
3
2998
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.