Page 1 of 1

Using variables as a column name

Posted: Tue Dec 05, 2006 7:53 pm
by utahfriend
HI there.

I am new to PHP and am having a problem. I have several column names in mySql that are named "page1", "page2", "page3", etc up to "page20". When I do an if statement, instead of calling it 20 times for each column name, is there any way to loop it so it works something like this:

Code: Select all

for ( $counter = 1; $counter <= 20; $counter ++) {
	echo '$page'.$counter;
	echo "<br>";
}
With the intention that "echo '$page'.$counter;" will display the contents of that field. When ever I try something like the above I get $page1 or $page2, etc.

Thanks for your help.

Posted: Tue Dec 05, 2006 8:08 pm
by John Cartwright
post the rest of your code, and what exactly is the problem?

Posted: Tue Dec 05, 2006 8:12 pm
by Zoxive
I believe you want Variable Variables..

Code: Select all

for ( $counter = 1; $counter <= 20; $counter ++) {
        echo ${'page' . $counter};
}

Posted: Tue Dec 05, 2006 8:34 pm
by utahfriend
That is exactly what I needed.... thank you so much!