Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

incrementing array index

Status
Not open for further replies.

effennel

Technical User
Oct 15, 2002
60
CA
I am trying to access the content passed via $_POST in a loop. What I'm trying to get is this:
Code:
 $insAmt_1 = $_POST"['1ins1']";
 $insAmt_2 = $_POST"['1ins2']";
 $insAmt_3 = $_POST"['1ins3']";

then

 $insAmt_1 = $_POST"['2ins1']";
 $insAmt_2 = $_POST"['2ins2']";
 $insAmt_3 = $_POST"['2ins3']";

etc.

What I tried and get parse error with is:
Code:
for ($i = 1; $i <= 8; $i++) {
 $insAmt_1 = $_POST"['".$i."ins1']";
 $insAmt_2 = $_POST"['".$i."ins2']";
 $insAmt_3 = $_POST"['".$i."ins3']";
 ...
}

Any suggestions?
FnL
 
Try this:

for ($i = 1; $i <= 8; $i++) {
$insAmt[$i] = $_POST[$i.'ins1'];

}
 
Sorry, misread your code:
Code:
for ($i = 1; $i <= 8; $i++) {
 $insAmt_[$i] = $_POST['1ins'.$i];
 
}
To get the 2ins results either do another loop:
Code:
for ($i = 1; $i <= 8; $i++) {
 $insAmt_[$i] = $_POST['2ins'.$i];
 
}
Or put a loop around your first loop:
Code:
for($a = 1; $a < 3 ; $a++)
{
  for ($i = 1; $i <= 8; $i++) {
   $insAmt_[$i] = $_POST[$a.'ins'.$i];
 
   }
}
 
Not sure why you'd want to set and then change the variables for though.

( note for site admins - "WHEN CAN WE HAVE AN 'EDIT POST' button! )
 
OK.

Looks good. Thanks for your help Dweezel!

Cheers
FnL
 
Is there anyways that the variable

$insAmt_[$i]

be available as
$insAmt_1
$insAmt_2
$insAmt_2
etc..

outside the loop?

 
The code that I posted puts all of the results into an array called '$instAmt_'. So if you wanted to access the variables you could use $instAmt_[0], $instAmt_[1], $instAmt_[2] etc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top