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
Le Var Range
Front End Web Development Techdegree Student 18,011 PointsAdding Search Controller & View , can't answer the 2nd quiz question, the adding the else statement.
<?php $recommendations = array(); ?>
<html>
<body>
<h1>Flavor Recommendations</h1>
<?php foreach($recommendations as $flavor) { ?>
<?php if (empty($flavor) ) { ?>
<?php echo "<ul>"; ?>
<li><?php echo $flavor; ?></li>
<?php } else {?>
<?php echo "<p>There are no flavor recommendations for you.</p>"; ?>
<?php echo "</ul>" ?>
<?php } ?>
<?php } ?>
</body>
</html>
This is what I have so far.
2 Answers
Alex Heil
53,547 Pointshi Le Var Range ,
from the question on task 2: If the array is NOT empty, the foreach loop will execute. But if it IS empty, we should display a message. Add an else block to the conditional that displays the following message in an HTML paragraph tag: 'There are no flavor recommendations for you.'
it says that we ONLY want to see the paragraph if the array is empty.
in your current code you placed the else statement inside the unordered list, which means it will still be printed.
but let's perhaps walk through step 1 and 2 another time completely, so that you have a full picture:
Task1: The following code contains a foreach loop that displays a list of ice cream flavor recommendations. It works great when the $recommendations array has at least one element, but we end up with some extra HTML when we don't have any recommendations. Write a conditional that makes sure the unordered list tags and the foreach loop get executed only if the array has at least one element.
<?php if (!empty($recommendations)) { ?>
<ul>
<?php foreach($recommendations as $flavor) { ?>
<li><?php echo $flavor; ?></li>
<?php } ?>
</ul>
<?php } ?>
as you see I started the if-statement before the ul-tag, and inside the conditional I checked if the array IS NOT empty. if that condition is met, means if the array HAS something in it then the ul will be displayed on the page.
now let's move to task2: we now want to show a paragraph ONLY when the array is empty and don't want to show the ul at all. so we put our else to our first global check like so:
<?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 } ?>
hope that helps and have a nice day ;)
Le Var Range
Front End Web Development Techdegree Student 18,011 PointsThanks a lot!
Muhammad Mohsen
Courses Plus Student 10,843 PointsMuhammad Mohsen
Courses Plus Student 10,843 PointsHi Le,
Can you post the question please?