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 trialBrian van Vlymen
12,637 Pointsaccounting for empty results 2 of 3 challenge task
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.'
I knew there is barely to pass it.
<?php
$recommendations = array();
?><html>
<body>
<h1>Flavor Recommendations</h1>
<?php if(!empty($recommendations)) { ?>
<ul>
<?php foreach($recommendations as $flavor) { ?>
<li><?php echo $flavor; ?></li>
<?php } ?>
</ul>
/* I added a paragraph */
<p> <?php else { echo 'There are no flavor recommendations for you.'}; ?> </p>
<?php } ?>
</body>
</html>
the error said BUMMER! try again any idea??
7 Answers
Hugo Paz
15,622 Points<?php if(!empty($recommendations)) { ?>
<ul>
<?php foreach($recommendations as $flavor) { ?>
<li><?php echo $flavor; ?></li>
<?php } ?>
</ul>
<?php } ?> // You forgot to close the If
/* I added a paragraph */
<?php else { ?>
<p> <?php echo 'There are no flavor recommendations for you.'; ?> </p>
<?php } ?>
David solution is correct, he just did not notice that the IF was not closed. I added it to the code.
David Omar
5,676 Pointsedit: included the closing brace from the if statement pointed out by Hugo Paz
I think its because you are putting the whole else statement between <p> tags when you need to put them around what your echoing out
<?php
$recommendations = array();
?><html>
<body>
<h1>Flavor Recommendations</h1>
<?php if(!empty($recommendations)) { ?>
<ul>
<?php foreach($recommendations as $flavor) { ?>
<li><?php echo $flavor; ?></li>
<?php } ?>
</ul>
/* I added a paragraph */
<?php } else { ?>
<p> <?php echo 'There are no flavor recommendations for you.'; ?> </p>
<?php } ?>
</body>
</html>
Brian van Vlymen
12,637 Pointsgotcha it
Kathy Schertz
3,032 PointsYou need to remove the curly brace that is right before the semi-colon.
Brian van Vlymen
12,637 Pointsyou code is wont let us to pass it... I believe you forget add the close curly bracket after the else, so I put my code here
</ul>
<?php else { ?>
<p> <?php echo 'There are no flavor recommendations for you.'}; ?> </p>
<?php } ?>
</body>
and it still said BUMMER! try again...
I have no idea whats wrong with it.
Mai Lon Ross
5,891 PointsA couple comments to the original. Switch the semi-colon ; and closing curly brace } right after the end quote ' Also put the else block after the if block.
For code clarity, I also included the paragraph tags in the echoed html.
else {
echo '<p>There are no flavor recommendations for you.</p>';
}
Sorry if my code shows up strangely.
Steven Walters
10,573 PointsWhy not stay in php?
<?php
if (!empty($recommendations)) {
echo '<ul >';
foreach ($recommendations as $flavor) {
echo '<li>' . $flavor . '</li>';
}
echo '</ul>';
} else {
echo '<p>There are no flavor recommendations for you</p>';
}
?>
David Omar
5,676 PointsDavid Omar
5,676 PointsOh yea good catch, think it would be better to put the closing brace on the same line as the else to have a little less code.