Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-06-13
2
Medium Priority
?
466 Views
Last Modified: 2010-05-18
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.
0
Comment
Question by:Victor Kimura
2 Comments
 
LVL 9

Expert Comment

by:Rurne
ID: 21781145
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?
0
 

Accepted Solution

by:
Victor Kimura earned 0 total points
ID: 21800976
Hi Rurne,

Sorry, the code had typos. I wanted to change this:
  // concatenate $classEndDate and append number
     $classEndDate = "classEndDate_";
     $classEndDateConcatenated = $classEndDate.$x;

where variable $x is an incremental value of 1 in a for loop

I found out to concatenate properly even multi arrays is done like so:
$classEndDateConc = ${'classEndDate'.$x};

Thank you for your help though.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question