Link to home
Start Free TrialLog in
Avatar of Victor Kimura
Victor KimuraFlag for Canada

asked on

POST multi-array problem: How can I create a $_POST multi-array variable in a for loop?

I'm having some difficulties to create the following in a for loop. For example, the following:

if ( $c == 1 ) {
    $classEndDate =  $_POST['classDates_1']['classEndDate_1']['d'];
} elseif ( $c == 2 ) {
     $classEndDate = $_POST['classDates_2']['classEndDate_2']['d'];
} ...

and convert the above in a for loop as:

// the following does not work
for ( $x=1;$x<5;$x++ ) {
     // concatenate $classDates and append number
     $classDates = "classDates_";
     $classDatesConcatenated = $classDates.$x;
    // concatenate $classEndDate and append number
     $classEndDate = "classEndDate_";
     $classEndDateConcatenated = $classEndDate.$x;

  if ( $c == 1 ) {
    $classEndDate =  $_POST[$classDatesConcatenated][$classEndDate]['d'];
  } elseif ( $c == 2 ) {
     $classEndDate = $_POST['classDates_2']['classEndDate_2']['d'];
  } ...
}

The above for loop does not function and returns an array for $classEndDate rather than an single value.

I can concatenate the following succesfully if $_POST is not a multi array and in a for loop as:

// the following works
for ( $c; $c<5; $c++ ) {
     ...// concatenate $studentName and append number
     $varStudent = 'student_name_';
     $studentNameConc = $varStudent.$c;
    $studentName = $_POST[$studentNameConc];
}

Can someone tell me how I can fetch these multi-array $_POST variables in a for loop.
Avatar of Rurne
Rurne
Flag of United States of America image

1. What's your input form look like?

2. you're not concatenating anything in the second loop.  Each pass replaces $studentName and throws the value away (unless you're purposefully throwing it away fror the sake of example).

3.  In your for loop, you iterate over $x.  However, in your if/else block, you're referencing $c.  Where do you obtain the value of $c?
ASKER CERTIFIED SOLUTION
Avatar of Victor Kimura
Victor Kimura
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial