Re: Dynamic Array Index using Variable
| From: | Chris Lott | Date: | Tue, 10 Jan 2006 00:54:04 +0000 |
| Subject: | Re: Dynamic Array Index using Variable | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-228380@lists.php.net to get a copy of this message | ||
What I have is a form with 45 data fields... if any of a selected 35
of those fields is empty, I want to have the user go back. So I can't
loop over the entire _POST array. So I thought I would put the names
of the fields I *do* want to check into an array and check.
So this won't work:
> foreach($array as $key=>$value){
> if(empty($value)){
> $err_msg= "$key is empty";
> break;
Because the array is the names of the fields, and I want to check each
variable in _POST that matches. So instead of writing
if (empty($_POST['forma'])
if (empty($_POST['formb'])
etc
I would do something like
$fieldstocheck = array('forma', 'formb');
foreach ($fields as $thisfield) {
if (empty($_POST[$thisfield])) {
set the error flag;
}
But that doesn't seem to work.
Eventually I will use the array to associate error text:
$fieldstocheck = array('forma' => 'You must enter field A', 'formb'
=>
''Field B is your age in years and is required');
etc
c