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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to dynamically call for vars?? 1

Status
Not open for further replies.

Kharas

Programmer
Jan 30, 2007
39
0
0
MX
Greetings

I've run across a particular predicament:

I am building a website for some pc gaming team and while doing a sub section of the MATCHES area, problems arose.

If each team has 4 players, and the match ends when either side runs out players by playing best out of one, you can have anything between 4 rounds miniumum (4-0) and 7 rounds total (4-3).

I don't want to have a static page for each possible round number (4-5-6-7), and thus the question.

Each row is basically like this:

Player_1_Race - Player_1_Name - Playere_1_Score - Enemy_1_Score _ Enemy_1_Name - Enemy_1_Race

<img src="<?php echo $row_matches_detail2['r1']; ?>" width="35" height="11" />
<?php echo $p_1; ?>
<?php echo $left_1." : ".$right_1; ?> (The results mixed with some code of my own to have the 'winner side' get a highlight/bold style to emphasize it)
<?php echo $e_1; ?>
<img src="<?php echo $row_matches_detail2['re1']; ?>" width="35" height="11" />

I wanted to count the total score for each side to determine the factual number of rounds and then based on that, make a do while loop until the data is pasted the correct X number of times.

I however, have not been able to find a way to dynamically load static vars.

For instance:

I'd like to get $row_matches_detail2['re1'] to change its number 1 inside the simple quotes to number 2 for the second time the loop goes through, and then to 3 and so forth, thus effectively placing the desired data and stopping before reaching Rounds that never existed.

Thanks in advance
 
why not use a counter together with an isset() call.

alternatively why not use a foreach on the array element.
 

Sorry if I misunderstood your need, but is it what you're looing for?

Code:
$row_matches_detail2["re" . $num]
 
Thanks, Sleidia's solution worked just fine.

I feel dumb but I swear I had tried to insert a var within the [] and it hadn't worked then. I guess I must of made a mistake =/

Whatever the case, your help is greatly appreciated.
 
Oh, I'm sorry, I was slow to pick this up.

Dynamically loading the query values $row_query['field'] works, but what about...

$var_1

And making that dynamically become $var_1, $var_2, etc.??
I haven't quite figured how to have the code 'reinterpretate' it as a var after the dynamic number has been concatenated to the initial var name.
 
I'm not sure exactly what you're trying to accomplish, but you could use variable variables to achieve this effect. For example:
Code:
$var_1 = "foo";
$var_2 = "bar";
$var_3 = "baz";
# Loop prints out "foo bar baz"
for ($i = 1; $i <= 3; $i++) {
   $somevar = "var_".$i;
   echo $$somevar." ";
}
However, it seems to me that it might be clearer to just put the values in an array. That way, instead of worrying about how to make $var_1 into $var_2, you could just use $vars[1] and $vars[2].
 
Thanks again. Wow, I didn't know you could do that. I really like the simplistic approach.

Help is much appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top