I use $errors to track any form errors.

But because there mabye any number of rows displayed in the form, $errors is coded as follows:

$errors['fnamee'][$i]="test of error"; //an example
$errors['lnamee'][$i]="test of error"; //an exmaple

(where $i starts at 1 and is incremented for each row)

and can result in an array looking like this:

[fnamee] => Array
    (
        [1] => test of error
        [2] => test of error
               ...etc.
    )
[lnamee] => Array
    (
        [1] => test of error
        [2] => test of error
               ...etc.
    )

What I would like to do, and can't figure out how to do it using count, is determine when $errros['anykey'][$i] is not empty. Again where $i equals an integer of the row I am processing at the time.

Answering this, will save me from checking each $errors key for that row to see if there is an error. I know if is probably easy, but I still getting a handle on arrays.

For example now I do something like this to check for errors for a row of data:

IF !(empty($errors['fnamee'][$i])) || !(empty($errors['lnamee'][$i]))) do or not do something...as you can see..if there are alot of fields in a row...well you know...

Thanks in advance...

    Sounds like you have an overly complicated method. Why not just start with an empty string, and then concatenate it with each error. Then just check if the string exists.

    unset($str);

    if( empty($_POST['name']) ) $str .= "Name is a required field.<br>";

    if( $str ) die($str);

      I don't think it overly complicated at all, given the functionality that I provide to the end user.

      given that I display the error by the individaul field, there maybe 1 to 100 rows of data. I don't want to re-write it...just want to know if there is a nice way to loop through the array looking for non-empty fields.

      😕

        Well, your choice...

        I don't totally understand what you are after. I think this is what you are after.

        for( $i = 0; $i < sizeof($errors); $i++ )
        {
           for( $j = 0; $j < sizeof($errors[$i]); $j++ )
           {
              if( empty($errors[$i][$j]) ) //do something.
           }
        }
        

        That will loop through every subelement of the array.

          In retrospect, I did make it overly complicated....displaying the errors by row, works fantastic, but checking for errors on a row..via this array...is well...kaput!....so I am going to keep them but just set a flag when an error occurs, and keep the $errors for displaying the message back on the form.

          Thank your for taking the time, and patience with me.

            Write a Reply...