|
Posted by Hendri Kurniawan on 04/20/07 05:23
Steve wrote:
> | Both of the code you posted did not work on my box (php5.2) unmodified.
> |
> | My initial thought will be using foreach on the array $numbers, but you
> | might have your own reason not to use it.
>
> i usually do. however, i'm prototyping something. here's what gives me the
> results...in case my free-handed post is why it doesn't work on your box.
> thanks for looking at it. sorry for the text-wrapping here...if you just
> backspace the wrap, it should be very easy to read/follow.
<SNIPPED CODE>
Umm.. again sorry if this is not the answer you are looking for.
This is what i came up with (see bottom of page)
What it basically does is that it replaces the array_walk to normal
function. Inside the function, instead of returning after correctly
validating the input, I've put "continue".
At the end i returned the errors array.
Hendri Kurniawan
<?
$inputs['Personnel'] = array(
'bodyTech' => 0 ,
'bodyTechAppr' => 0 ,
'paintTech' => 0 ,
'paintTechAppr' => 0 ,
'paintPreps' => 0 ,
'mechanicTechs' => 0 ,
'estimators' => 0 ,
'otherEmployees' => 0
);
$inputs['Facility'] = array(
'bodyShopLocation' => '' ,
'franchises' => '' ,
'totalSqFeet' => 0 ,
'productionSqFeet' => 0 ,
'totalWorkStations' => 0 ,
'totalPaintBooths' => 0 ,
'totalDetailStalls' => 0
);
function retrieveInput(&$value, $key, $source)
{
$default = 0;
if ($key == 'bodyShopLocation'){ $default = ''; }
if ($key == 'franchises'){ $default = ''; }
$value = isset($source[$key]) ? $source[$key] : $default;
}
function validateInput($value)
{
$inputName = '';
$maxValue = 999;
$errors = array();
foreach($value as $key=>$value) {
switch ($key)
{
case 'bodyShopLocation' :
if (in_array($value, array('', 'ON SITE', 'OFF SITE'))){ continue; }
$errors[$key] = 'BODY SHOP LOCATION must be either on-site or
off-site.';
continue;
break;
case 'franchises' :
if (in_array($value, array('', 'SINGLE', 'MULTIPLE'))){ continue; }
$errors[$key] = 'FRANCHISE TYPE must be either single or multiple.';
continue;
break;
case 'bodyTech' : $inputName = 'BODY TECHNICIANS';
break;
case 'bodyTechAppr' : $inputName = 'BODY TECHNICIAN APPRENTICES';
break;
case 'paintTech' : $inputName = 'PAINT TECHNICIANS';
break;
case 'paintTechAppr' : $inputName = 'PAINT TECHNICIAN APPRENTICES';
break;
case 'paintPreps' : $inputName = 'PAINT PREPS';
break;
case 'mechanicTechs' : $inputName = 'MECHANICAL TECHNICIANS';
break;
case 'estimators' : $inputName = 'ESTIMATORS';
break;
case 'otherEmployees' : $inputName = 'OTHER EMPLOYEES';
break;
case 'totalSqFeet' : $inputName = 'TOTAL SQUARE FEET';
$maxValue = 9999999; break;
case 'productionSqFeet' : $inputName = 'PRODUCTION AREA SQUARE FEET';
$maxValue = 9999999; break;
case 'totalWorkStations' : $inputName = 'TOTAL WORK STATIONS';
break;
case 'totalPaintBooths' : $inputName = 'TOTAL PAINT BOOTHS';
break;
case 'totalDetailStalls' : $inputName = 'TOTAL DETAIL STALLS';
break;
default : continue; break;
}
if ($value <= $maxValue) continue;
$errors[$key] = $inputName . ' must be a whole number between 0 and ' .
$maxValue . '.';
}
return $errors;
}
array_walk($inputs['Personnel'], 'retrieveInput', $_REQUEST);
array_walk($inputs['Facility'], 'retrieveInput', $_REQUEST);
$errors = array();
$errors = array_merge($errors,validateInput($inputs['Personnel']));
$errors = array_merge($errors,validateInput($inputs['Facility']));
echo '<pre>' . print_r($inputs, true) . '</pre>';
echo '<pre>' . print_r($errors, true) . '</pre>';
?>
Navigation:
[Reply to this message]
|