Victor Kimura
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']['cl assEndDate _1']['d'];
} elseif ( $c == 2 ) {
$classEndDate = $_POST['classDates_2']['cl assEndDate _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[$classDatesConcaten ated][$cla ssEndDate] ['d'];
} elseif ( $c == 2 ) {
$classEndDate = $_POST['classDates_2']['cl assEndDate _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.
if ( $c == 1 ) {
$classEndDate = $_POST['classDates_1']['cl
} elseif ( $c == 2 ) {
$classEndDate = $_POST['classDates_2']['cl
} ...
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[$classDatesConcaten
} elseif ( $c == 2 ) {
$classEndDate = $_POST['classDates_2']['cl
} ...
}
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?