Page 1 of 1
Dynamic Variables in a Loop
Posted: Wed Sep 09, 2009 12:42 pm
by oscardog
Hello,
I have the following code:
Code: Select all
<?php $echoPlayerLoop = 1;
while($echoPlayerLoop <= $totalPlayers) {
$tempName = "team1p" . $echoPlayerLoop;
$tempName2 = "tempName"; ?>
<input type="hidden" name="<?php echo "team1player" . $echoPlayerLoop; ?>" value="<?php echo ${$tempName2}; ?>" />
<?php $echoPlayerLoop++;
}
$echoPlayerLoop = 20;
$echoPlayerLoop2 = 1;
while($echoPlayerLoop2 <= $totalPlayers) {
$tempName = "team2p" . $echoPlayerLoop2;
$tempName2 = "tempName"; ?>
<input type="hidden" name="<?php echo "team2player" . $echoPlayerLoop; ?>" value="<?php echo ${$tempName2}; ?>" />
<?php
$echoPlayerLoop++;
$echoPlayerLoop2++;
} ?>
And I want it to take whatever is stored in(for example) $team1p1 and put that as the value of the hidden field. I've looked high and low, all over the internet for the answer and whatever I do it either leaves the hidden field blank or, atm, echoes "team1p1".
The loops are separate, but similar, so if you can get the first to work then the second will follow suit.
Thanks for your help
Oscardog
Re: Dynamic Variables in a Loop
Posted: Wed Sep 09, 2009 1:05 pm
by Mirge
Obviously that isn't all of the code, but the little bit that you did paste is wrong. You're opening new <?php tags without closing the old ones... aka, you're using <?php tags where you don't need to be (inside of the loop).
Re: Dynamic Variables in a Loop
Posted: Wed Sep 09, 2009 1:06 pm
by oscardog
Mirge wrote:Obviously that isn't all of the code, but the little bit that you did paste is wrong. You're opening new <?php tags without closing the old ones... aka, you're using <?php tags where you don't need to be (inside of the loop).
What? I edit in dreamweaver so I can easily tell when I leave a PHP tag open, if you look all I open and close then the opening and closing PHP tags are fine, as it throws no errors, it's a deeper problem than that
Oscardog
Re: Dynamic Variables in a Loop
Posted: Wed Sep 09, 2009 1:08 pm
by Mirge
oscardog wrote:Mirge wrote:Obviously that isn't all of the code, but the little bit that you did paste is wrong. You're opening new <?php tags without closing the old ones... aka, you're using <?php tags where you don't need to be (inside of the loop).
What? I edit in dreamweaver so I can easily tell when I leave a PHP tag open, if you look all I open and close then the opening and closing PHP tags are fine, as it throws no errors, it's a deeper problem than that
Oscardog
Oh nevermind I see the closing ?> tags now.
Re: Dynamic Variables in a Loop
Posted: Wed Sep 09, 2009 1:14 pm
by Mirge
Let it be known that I do _not_ like this style of programming... BUT, here is the solution (partially--first loop only):
Code: Select all
<?php $echoPlayerLoop = 1;
$totalPlayers = 5;
$team1p1 = "t1p1 value";
$team1p2 = "t1p2 value";
$team1p3 = "t1p3 value";
$team1p4 = "t1p4 value";
$team1p5 = "t1p5 value";
while($echoPlayerLoop <= $totalPlayers) {
$tempName = "team1p" . $echoPlayerLoop;
$tempName2 = "tempName";
?>
<input type="hidden" name="<?="team1player" . $echoPlayerLoop;?>" value="<?=${${$tempName2}};?>" />
<?php
$echoPlayerLoop++;
} // end while loop
$echoPlayerLoop = 20;
$echoPlayerLoop2 = 1;
while($echoPlayerLoop2 <= $totalPlayers) {
$tempName = "team2p" . $echoPlayerLoop2;
$tempName2 = "tempName"; ?>
<input type="hidden" name="<?php echo "team2player" . $echoPlayerLoop; ?>" value="<?php echo ${$tempName2}; ?>" />
<?php
$echoPlayerLoop++;
$echoPlayerLoop2++;
} ?>
OUTPUT:
Code: Select all
$ php -q test.php
<input type="hidden" name="team1player1" value="t1p1 value" />
<input type="hidden" name="team1player2" value="t1p2 value" />
<input type="hidden" name="team1player3" value="t1p3 value" />
<input type="hidden" name="team1player4" value="t1p4 value" />
<input type="hidden" name="team1player5" value="t1p5 value" />
<input type="hidden" name="team2player20" value="team2p1" />
Re: Dynamic Variables in a Loop
Posted: Wed Sep 09, 2009 1:23 pm
by oscardog
Thanks, worked a treat!
Out of interest, why do you not like that sort of coding?
Oscardog
Re: Dynamic Variables in a Loop
Posted: Wed Sep 09, 2009 1:36 pm
by Mirge
oscardog wrote:Thanks, worked a treat!
Out of interest, why do you not like that sort of coding?
Oscardog
Readability & maintainability is gone.