Using variables as a column name

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
utahfriend
Forum Commoner
Posts: 34
Joined: Thu Nov 10, 2005 12:25 pm
Location: Bountiful, Utah

Using variables as a column name

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

post the rest of your code, and what exactly is the problem?
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

I believe you want Variable Variables..

Code: Select all

for ( $counter = 1; $counter <= 20; $counter ++) {
        echo ${'page' . $counter};
}
utahfriend
Forum Commoner
Posts: 34
Joined: Thu Nov 10, 2005 12:25 pm
Location: Bountiful, Utah

Post by utahfriend »

That is exactly what I needed.... thank you so much!
Post Reply