Dynamic Variables in a Loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Dynamic Variables in a Loop

Post 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
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Dynamic Variables in a Loop

Post 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).
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Dynamic Variables in a Loop

Post 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
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Dynamic Variables in a Loop

Post 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.
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Dynamic Variables in a Loop

Post 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" />
 
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Dynamic Variables in a Loop

Post by oscardog »

Thanks, worked a treat!

Out of interest, why do you not like that sort of coding? :)

Oscardog
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Dynamic Variables in a Loop

Post 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.
Post Reply