3 Replies - 13461 Views - Last Post: 06 May 2009 - 10:26 AM Rate Topic: -----

#1 skirkpatrick  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 18-February 08

How to increment a variable name in a while loop

Posted 06 May 2009 - 09:40 AM

I know I can increment /decrement a variables value using ++ or -- But is there a way to increment the variable name???

I am using a user filled form and GET and now need to do some addition calculations with the data.

$newnumber = 1;
echo "From input calculations: <br>";
while ($newnumber <= $ply) {
$newplytype = "$" . "newplytype" . $newnumber;
$plynumber = "ply" . $newnumber;
$SqInchnumber = "SqInch" . $newnumber;
$newplytype = $_GET[$plynumber] . "" . $_GET[$SqInchnumber];
echo "Ply " . $newnumber . " = " . $newplytype . "<br>";


$newnumber++;


Which produces:
From input calculations:
Ply 1 = 79374
Ply 2 = 79374
Ply 3 = 82374
Ply 4 = 81374
Ply 5 = 83374
Ply 6 = 83374

Which is correct - what I need to do now is capture that data into a stream of variables.

$ply1 = 79374;
$ply2 = 79374;
$ply3 = 82374;
$ply4 = 81374;
$ply5 = 83374;
$ply6 = 83374;

which would increment themselves with the values from the loop.

Any help or guidance is always appreciated.

Steve

Is This A Good Question/Topic? 0
  • +

Replies To: How to increment a variable name in a while loop

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2914
  • View blog
  • Posts: 10,093
  • Joined: 08-August 08

Re: How to increment a variable name in a while loop

Posted 06 May 2009 - 09:43 AM

You want to use an array:

$ply[] = $newplytype;



Then:

echo $ply[6];

produces:
83374

This post has been edited by CTphpnwb: 06 May 2009 - 09:45 AM

Was This Post Helpful? 0
  • +
  • -

#3 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: How to increment a variable name in a while loop

Posted 06 May 2009 - 09:46 AM

The array method he just showed you would be best...

if your insistent on creating new variables ( correct me if im wrong ) would be

${'ply'.$newnumber};
// would create $ply1, $ply2, etc......


Was This Post Helpful? 1

#4 skirkpatrick  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 18-February 08

Re: How to increment a variable name in a while loop

Posted 06 May 2009 - 10:26 AM

Both work great - I was trying to use the curly bracket way but could not get it to work.

Thanks

View PostRPGonzo, on 6 May, 2009 - 08:46 AM, said:

The array method he just showed you would be best...

if your insistent on creating new variables ( correct me if im wrong ) would be

${'ply'.$newnumber};
// would create $ply1, $ply2, etc......


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1