Click to See Complete Forum and Search --> : variables in variables?


Da Warriah
07-17-2003, 06:55 PM
alright, its me again with another question...

im working on a loop that produces a row of table cells with info in them...all the info is stored in variables numbered 1 to 10, like this:

$o1
$o2
$o3
...
$o10

now what i want to do is, inside that loop, cycle through the 10 different variables...but are variables inside variables allowed? heres the basic structure i have so far:

for($i = 1; $i <= $n; $i++){

$contents .= "
<tr>
<td>
<b>$o".$i.":</b>
</td>
<td class=\"bar\">
<img alt=\"Bar\" src=\"bar.gif\" width=\"$res".$i."\" height=\"7\" /> - $res".$i."% - ".$sofar['opt$i']." votes\n";
</td>
</tr>";

}

as you can see, $i is put in several times, including inside the key of an array...would this work? thanx in advance:)

Khalid Ali
07-17-2003, 11:58 PM
how about array inside of an array?

brendandonhue
07-18-2003, 07:12 AM
Yes they are. I didn't check if your script will work, but you can use variables with another variable.
Example

$o1 = 0;
$o2 = $o1 + 1;

Now the value of o2 is NOT $o1 + 1, the value of o2 is 1. When you point to a variable in another variable, the contents of the variable are actually copied, not just a pointer to the first variable.
Just a guess, but the same is probably true for an array.

Da Warriah
07-18-2003, 08:29 AM
so brendan, youre saying that something like this:

$contents = "<b>$o".$i.":</b>";

should work fine? so the first time through the loop, it should print out the value of $o1? alright then, ill try it out...otherwise ill try to work the code so $o is set to an array...it should work...

oh, another question: to set the values of an array, do you have to use the array() function? or can values simply be set like:

$o[1] = 8;
$o[2] = 5;

etc etc, without even touching the array() function?

Da Warriah
07-18-2003, 02:48 PM
well the variables in variables thing doesnt work, so i had to redo my entire section of code in order to implement arrays instead...then $o[$i] worked fine:D

but i have ONE last question, and then i think ill be done this script and it can go into my free script database (check out my sig - CrossFade Studios)...

alright, i have a checkbox:

<input type="checkbox" name="clear" value="clearpoll" />

now how do i access the value of $clear? i tried saying

if($clear == "clearpoll"){

but that doesnt seem to work...is there something im missing, or should i just do something like this?

if($clear != null){

edit: alright, using null works fine...thanx me:D

brendandonhue
07-18-2003, 03:21 PM
$_POST['clear']
or
$_GET['clear']

Da Warriah
07-18-2003, 05:30 PM
yeah i used $clear = $_POST["clear"]; just cause i find it easier to work with if i have to type it out a million times...the thing is that i wasnt sure what value it gave out when the checkbox was checked - for things like textboxes and such, the value is returned, but for a checkbox, both the name and value are already defined...anyways it works now, so problem is solved;)