Solved

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

Posted on 2008-06-13
2
457 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
calculated column 12 74
can windows 10 wamp send mail()? 3 32
PHPStorm debugging issues 1 21
hiding/removing php extension - best practice 4 24
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

911 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

16 Experts available now in Live!

Get 1:1 Help Now