PHP consecutive variables

arson_nick

OSNN Addict
Joined
19 Jul 2002
Messages
126
Is there a nice way to print out consecutively named variables like Track1, Track2, Track3 ?
 
I didn't try this, but if you used an array it would be easy:

PHP:
$variables = 10;
$i = 0;
while ($i < $variables) {
echo "$Track[$i]";
++$i;
}
 
That's genius, and I wish I'd thought of it. Makes reading the variables in a bit easier too. Thank you!
 
Your welcome, hardly genius, but I'll take it ;)

Oops, I had a booboo above, I'll edit it, I had ++$x where it should have been ++$i
 
OK, just like any code, I think that there are acouple things that need to be changed;

PHP:
$variables = 10;
$i = 0;
while ($i <= $variables) {
echo "$Track[$i]";
++$i;
}

See, now I changed the loop so its 'less than or equal to' so it will print all instead of skipping the last one. Another problem is that the array numbering starts at 0 and not 1. You could always start at
Track[1]=Track 1 value
and so forth and change $i=0 to $i=1.
 
arson_nick just doing

PHP:
$Trakc = array();

Will work fine.

j79zlr this:

PHP:
$variables = 10;
$i = 0;
while ($i <= $variables) {
echo "$Track[$i]";
$i++;
}

Note the $i++;, would in this example you gave, do the same as

PHP:
$variables = 10;
$i = 0;
while ($i <= $variables) {
echo "$Track[$i]";
++$i;
}

Even better code would be:

PHP:
foreach($Track as $value) {
echo $value;
}

This will let him loop thru the entire array, EVEN when its bigger than the 10 variables you had set it to :).

Check out http://php.net/foreach for more info.
 
Now, if you did not have it in an array, then this would help:

PHP:
$x = 0;
while (1) {
$var = "Track" . $x;
echo ${$var};
$x++;
$var = "Track" . $x;
if (${$var} == "") {
break;
}
}

This will loop thru variables like this:

$Track1
$Track2
$Track3
$Track4
etc ...
 
That's exactly what I was looking for. The array was giving me a lot of problems as far as passing it through a form. Thanks again.
 

Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,621
Latest member
naeemsafi
Back