PDA

View Full Version : array error printing



bberry91
18 May 2011, 10:12 AM
i am printing out the fields that were left empty, or if

$error['fieldname'] = 'Flagged';
then i want to print the field name that is either incorrect, or empty. here is what i have at the moment:

<?
$reqfields = array('FName','LName');
$reqfieldnames = array('First name','Last name');

for ($i=0; $i<sizeof($reqfields); $i++) {
if (empty($i)) {
echo $reqfieldnames[$i];
}
}
?>

i feel like there is a easier way to do this... also, it only print out First Name. maybe print out the name or id of the field?
i could also check if the error of the field name at i is set to flagged... any ideas or suggestions are much appreciated!

resdog
18 May 2011, 12:30 PM
You can use an multi-dimensional array:



$reqfields = array(array('FName', 'First name'), array('LName', 'Last name');


If you want to add more info, you can do so. To add the id of the field (I'm assuming id's here):



$reqfields = array(array('FName', 'First name', 1), array('LName', 'Last name',22);


You access each data segment with brackets, i.e. -

$reqfields[0,0] - is equal to 'FName' - The first number is the position in the array (array('Fname', 'First name')), and the second number is the position of the interior array ('Fname').

Hope that helps.