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 trialJoshua Bronkhorst
11,500 Points"Add one element to the $recommendations array with a value of 'Avocado Chocolate'." ?? How? (stage 5, challenge 3 of 3)
I have read all the other posts to the forum on this question, but none of them make sense to me! Could someone please give me a definitive answer as to what im doing wrong and what i must change.
Here is my code:
<?php
$recommendations = array(1);
?>
<html>
<body>
<h1>Flavor Recommendations</h1>
<ul>
<?php if (!empty($recommendations)) { ?>
<ul>
<?php foreach($recommendations as $flavor) { ?>
<li><?php echo $flavor; ?></li>
<?php } ?>
</ul>
<?php } else { ?>
<p>There are no flavor recommendations for you.</p>
<?php } ?>
</ul>
</body>
</html>
Hugo Paz
15,622 PointsI edited your post so its more readable. Robert Posted the correct answer.
2 Answers
Robert Walker
17,146 PointsHi Joshua,
What you need to do is actually pass the value of 'Avocado Chocolate' into the array like so:
$recommendations = array ('Avocado Chocolate');
Kevin Williams
Courses Plus Student 19,952 Points<?php
$recommendations = array();
$recommendations[] = 'Avocado Chocolate'; //this is a way to add one more element to the array
?>
<html> <body>
<h1>Flavor Recommendations</h1>
<?php
/* I found that adding additional closing and opening php tags around html markup a bit hard
to read so, I refactored the code like this: (note comments are not needed) */
if(!empty($recommendations)) //checks if array is not empty
{
echo '<ul>'; // start of unordered list
foreach($recommendations as $flavors) // loops through array displays list of array items
{
echo '<li>' . $flavors . '</li>'; // outputs each array item in html list item tag within the unordered list
}
echo '</ul>'; //end of unordered list
}
else
{
echo '<p> There are no flavor recommendations for you.</p>';
}
?>
</body> </html>
Robert Walker
17,146 PointsRobert Walker
17,146 Points.