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
Warren Johnson
6,870 PointsCode Challenge: Separating Concerns: Models (2 of 3)
I could really use a hand with this one:
The task:
Next, let’s move the flavors array into that function. This task has two steps. (1) Move the code that creates the flavors array from [index.php] into the get_all_flavors() function in [flavors.php]. (2) Add code at the end of the function so that it returns the array of flavors back to whatever code calls it.
My code:
<?php
// ==== INCLUDE MODEL FILE ====
require_once('flavors.php');
******THE FOLLOWING CODE WAS MOVED TO *******FLAVOR.PHP
$flavors = array(
"Jalapeno So Spicy",
"Avocado Chocolate",
"Peppermint",
"Vanilla",
"Cake Batter",
"Cookie Dough"
);
?>
<h1>Ye Olde Ice Cream Shoppe</h1>
<p>Here’s a full list of our ice cream flavors.</p>
<ul>
<?php
$list_html = "";
foreach($flavors as $flavor) {
$list_html = $list_html . "<li>";
$list_html = $list_html . $flavor;
$list_html = $list_html . "</li>";
}
$list_html = get_all_flavors(); ***this is my code***
echo $list_html;
?>
</ul>
(flavors.php):
<?php
// MODEL FILE
function get_all_flavors() {
$flavors = array(
"Jalapeno So Spicy",
"Avocado Chocolate",
"Peppermint",
"Vanilla",
"Cake Batter",
"Cookie Dough"
)
};
?>
3 Answers
Randy Hoyt
Treehouse Guest TeacherBefore the refactoring, the $flavors variable was declared right here in the main file. It looks like you have moved it into the function correctly. (But did you also remove it from the main code?)
Remember, though, that the code outside of the function doesn't have any access to the variables inside the function. So the $flavors array is empty when the foreach loop goes through the $flavors array. You need to add two things:
- In the get_all_flavors() function, you need to return the flavors array.
- In the main code, before you go through the foreach loop, you need to call the get_all_flavors() function and put the return value into a new variable.
Does that help?
Barry Barnes
18,631 PointsThanks Randy. I was stuck too, but got it with your help.
Rafael Peralta Jr
21,879 PointsWow! Thanks Randy! Took me a good 20-30 minutes but I understand it.
Warren Johnson
6,870 PointsWarren Johnson
6,870 PointsHey Randy!, Thanks for your help. Although I got past the second part of the challenge(2 of 3), at (3 of 3), it seems that the variable ($flavors) doesn't have the correct value - the array items of course:
my Code (flavors.php);
<?php function get_all_flavors() { $flavors = array( "Jalapeno So Spicy", "Avocado Chocolate", "Peppermint", "Vanilla", "Cake Batter", "Cookie Dough" ); return $flavors; };
// MODEL FILE
my Code (index.php):
<?php
// ==== INCLUDE MODEL FILE ==== require_once('flavors.php');
?>
<h1>Ye Olde Ice Cream Shoppe</h1>
<p>Here’s a full list of our ice cream flavors.</p>
<ul> <?php get_all_flavors($flavors); *this is where I am attempting to return the value of the array* $list_html = ""; foreach($flavors as $flavor) { $list_html = $list_html . "<li>"; $list_html = $list_html . $flavor; $list_html = $list_html . "</li>"; } echo $list_html;
?> </ul>
Randy Hoyt
Treehouse Guest TeacherRandy Hoyt
Treehouse Guest TeacherYour function looks great! The issue is in how you are calling it. Remember the three parts to the function:
Let's say I want to call a function like
trim. The name of the function is trim, it receives one argument (the string I want to trim), and it returns the trimmed string. I would call it like this:$trimmed_string = trim($untrimmed_string);The variable inside the parentheses is the argument. Our function get_all_flavors does not receive any arguments. For functions that don't receive arguments, you would call them with empty parentheses like this:
$value = function_name();This will take the return value from function_name and put it in a variable named $value that our main code can now work with.
Does that help?
Warren Johnson
6,870 PointsWarren Johnson
6,870 PointsWow Randy! (LOL) - After your explanation, I had an, "if it were a snake, it would have bitten me" - AHA moment. Thanks once again.