Functions and Array Issues.. Pulling my hair out!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Kedaeus
Forum Newbie
Posts: 3
Joined: Thu Aug 14, 2008 5:51 pm

Functions and Array Issues.. Pulling my hair out!

Post by Kedaeus »

My application does what I expected it to do the way I wrote it - however it's not what I want it to do.. I need to adjust the code and I don't know how.

I WANT to pass 3 variables to a function 1 of them being an array.

After filling out my web form and submitting it the script sets the style sheets for the page, checks for errors, prints error information (if any), and then pulls up the next logical form element (last step broken).

The problem is, instead of passing the error information as a variable the information is echoed and in order to maintain the look and feel to the site I have to create the headers (unique to the error information) BEFORE the error checks.. Example below: "..." is irrelevant information purposefully truncated.

Code: Select all

 
include ('includes/inc.funct.newvalid.php');
<style>
...
</style>
<fieldset class='error'>"; //I NEED TO REMOVE THIS AND PASS $ERR as array
//WHERE $ERR = ['number of error messages']['custom error message']['custom error message']
 
// Loop through the list, validate information as it's passed
foreach (array_keys($_POST) as $key)
    {
        $$key = $_POST[$key];
        $err = $err + VALIDATE($key, ${$key}); //Validate Data $err = # of err msgs returned from function.
        ...
 
The above code validates the posted $key (field name) and $$key (field data) through various functions.

The data is passed through a minimum of 2 functions during this call.. Represented below (all functions are formatted the same).

First Function (VALIDATE)

Code: Select all

 
function VALIDATE($label, $information)
{
    if($label === "FNAME" || $label === "MNAME" || $label === "LNAME") //self explanitory
        {
            switch ($label) // change label to human readable information
            {
                case "FNAME";
                $label = "<u>first name</u>";
                break;
            
                case "MNAME";
                $label = "<u>middle name</u>";
                break;
            
                case "LNAME";
                $label = "<u>last name</u>";
                break;
            }
            $err = NOTNULL($label, $information); // Make sure field isn't blank
            if(empty($err)) // if field is NOT blank
            {
                $err = NAMECHK($label, $information); // Make sure field meets criteria
                return $err; //return the number of error messages found
            }
            else{return $err;} //return the number of error messages found
        }
    elseif ...
 
The second function (is simple) however all my functions are laid out in this format.
I need to pass an array to the previous function from the main script, to this function, and change the echo statement to an array assignment..

SECOND FUNCTION (NOTNULL)

Code: Select all

 
/////////////////////////////////////////////////////////////////////////////////////////////////
function NOTNULL($label, $information)
{
    if(empty($information))
    {
        $err++;
        echo "<font color='red'>Error! You left your <u> $label </u> blank!</font><br />";
 
        return $err;
    }
    else{return;}
}
 
I tried utilizing $err as an array in the following way.

Code: Select all

 
$err['0']++;
$err['$err['0']'] = "<font color='red'>Error! You left your <u> $label </u> blank! </font><br />";
 
Obviously, this didn't work..

The problem is - the way I have it laid out right now I HAVE to print the fieldset (first code block) BEFORE I do my validation. Instead of passing the error messages through a variable array and printing the fieldset only IF there is an error. If everything passes without an error then I have a fieldset I don't want or need. If it fails I does what I want it to do. (Finishes the field set with the echo'ed information).

I need fresh eyes on my code because mine are worn out.. I'm not seeing what I need to do to reformat this and get it working as intended not as expected.

Kedaeus
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: Functions and Array Issues.. Pulling my hair out!

Post by marcth »

Looks like your having a small problem using arrays. You can read up on them on the php.netsite.

Code: Select all

$errors = array();
$errors[] = 'First Element';
$errors[] = 'Second Element';
 
foreach($errors as $key=>$error) {
  printf('$error[%s] is set to %s', $key, $error);
}
 

Code: Select all

$associativeArray = array();
$associativeArray['one'] = 'First Element';
$associativeArray['two'] = 'Second Element';
$associativeArray['three'] = 'Third Element';
 
foreach($associativeArray as $key=>$value) {
  printf('$associativeArray [%s] is set to %s', $key, $value);
}
Post Reply