Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Dynamic PHP Variables

I am building a survey that will take give recommendations based on a person's input. I assign each person a unique URL that they can share socially in the hopes of bringing some social traffic.

I have assigned variables to each host. I run the answers through a series of switch statements, then depending on the answer, I give different variables points.

Not that hard for a one off survey. You can check out what I've been doing this week at my website. Here is a link to the main controller at pastebin

My end goal is to have a system that I, or my bosses, can login to. From the admin we can create a new survey, add some companies/products, create questions, then launch.

My question to you is this: Is there a way that I can dynamically assign a series of variables?

For example. On the admin end if I was to add three companies, such as Target, Wal Mart, and Toys R Us... Is there a way I could save these variables to a database then pull them out and manipulate them in a controller?

Thank you for any ideas and/or code suggestions!

3 Answers

Yes, with php it is quite easy to access and operate on several type of databases. MySQL and SQLite are the most common. You can check php.net documentation to see how to make this. Probably you will need to learn a bit of SQL language but don't be afraid of this. SQL, at least for basic things, is quite simple.

Hey Stefano, thanks for replying.

Please allow me to clarify. I do understand the basics of working with MySQL. I was looking more for a way to declare variables dynamically based on how many entries were in a database.

But after doing some more research, it looks like using an associative array or variable variables would be a good way to approach the problem.

I'm definitely interested in any other options.

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Definitely use an array for this. It doesn't even need to be an associative array; a simple indexed array will work fine. (Don't use variable variables. There aren't too many times where you'll want to use those; this is definitely not one of them! :~)

Let's say your PHP file that displays the survey is at survey.php. You want to have a query string parameter to indicate which set of options are available:

/survey.php?id=5

Then you'll want a table that has a row for every option with a column indicating which survey it goes with, something like this:

+------+-----------+
|survey|option     |
+------+-----------+
|5     |Target     |
+------+-----------+
|5     |Wal-Mart   |
+------+-----------+
|5     |Toys R Us  |
+------+-----------+
|6     |Sprouts    |
+------+-----------+
|6     |Whole Foods|
+------+-----------+
|6     |Publix     |
+------+-----------+

When you load the page, you get the survey ID from the web address and then you query the database to get all the options that match the survey and load them into an array.

Your code to display the survey and save the results no longer has to know what any of the options are; it can just interact with the elements of the array. That way, you can change the data in the database and the logic for the survey will stay the same ... even when the options change.

If you aren't sure how to work with arrays in PHP, I'd recommend the fourth stage of my most recent PHP course: Build A Simple PHP Application > Listing Inventory Items