Solved

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

Posted on 2008-06-13
2
454 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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

744 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now