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
Giuseppe Elia Brandi
Front End Web Development Techdegree Graduate 69,630 PointsRefactoring the Codebase Separating Concerns: MVC - challenge 4 of 4
Good Day all;
I hope somebody can help me out with this new code challenge:
Since the flavors are now ordered by the number of likes, it would be nice to display the number of likes next to each flavor. Instead of showing just the flavor name (“Cookie Dough”), change it to show the flavor name, followed by a space, followed by a plus sign, followed by the number of likes (“Cookie Dough +976”). (Look through [flavors.php] to see the structure of the flavors array and to figure out how to access the number of likes.
The code is the following up to the 3rd task <?php
// INCLUDE MODEL FILE require_once("flavors.php");
// CONTROLLER CODE $flavors = get_flavors_by_likes(3);
My understanding is that I need to work again on the Controller Code by possibly using concatenation but it does not seem to work for me.
I have tried the following without any success:
$numbers_like = "likes";
$flavors = get_flavors_by_likes(3) "+" .$numbers_like;
I guess that I have not understood how to access the number of likes.
Looking forward to hearing from you soon.
Giuseppe
5 Answers
Kevin Korte
28,149 PointsLook in the flavors.php file.
Look in the flavors array (the first one).
Inside that array, we have after the keyword "name, another keyword "likes' with a value.
Now go back to the index.php file.
Scroll down to find the foreach loop.
We are loading what is stored in $flavors into $flavor, and then echoing that the string that is equal to the "name" keyword within the array in the flavors.php file.
So, this line in index.php
foreach ($flavors as $flavor) {
echo "<li>" . $flavor["name"] . "</li>";
}
We need to use concatenation, you knew that. If $flavor["name"] prints the name of the ice cream, wouldn't $flavor["likes"] print the amount of likes stored in the $flavor array?
So, reading the php code, we are echoing to the screen; a <li> tag, the value name of the ice cream from the $flavors array, a space, a plus sign, the number of likes from the $flavors array, and a closing </li> tag.
So it looks like this:
foreach ($flavors as $flavor) {
echo "<li>" . $flavor["name"] . " " . "+" . $flavor["likes"] . "</li>"
}
And that is it. It's all that needs to be changed. If you don't understand what just happened, ask. One of us can try to explain it further if you don't get it still.
Giuseppe Elia Brandi
Front End Web Development Techdegree Graduate 69,630 PointsThank you for your help Kevin, I think I do understand it but I probably need to go back and perfect some of my skills in PHP. Best Regards.
Giuseppe
Dinakaran K
8,613 PointsIts a really a good explanation.. Best regards Kevin..
Anthony Randazzo
13,072 PointsThis isn't working for me. I copied and pasted and it's not working.. not sure what's wrong.
Coders Unlimited
10,112 Pointsstruck on this task 3
Next, change the $flavors variable in index.php to contain the three most popular flavors. Right now, the controller code calls the function get_recent_flavors() in the model code to get the most recent flavors. (Look through flavors.php to find a function that returns the most popular flavors.)
<?php
// INCLUDE MODEL FILE require_once("flavors.php");
// CONTROLLER CODE $flavors = get_recent_flavors(3);
// VIEW CODE ?><html> <body>
<h1>Ye Olde Ice Cream Shoppe</h1>
<p>We sell amazing flavors of ice cream.</p>
<h2>Most Popular Flavors</h2>
<ul>
<?php
foreach ($flavors as $flavor) {
echo "<li>" . $flavor["name"] . "</li>";
}
?>
</ul>
</body> </html>
Anthony Randazzo
13,072 PointsAnthony Randazzo
13,072 PointsThis answer isn't working for me...
Steve Whitmore
13,408 PointsSteve Whitmore
13,408 PointsPut a semicolon at the end of the concatenated code:
echo "<li>" . $flavor["name"] . " " . "+" . $flavor["likes"] . "</li>";