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 trialWarren Johnson
6,870 PointsCode Challenge: Accounting for Empty Results (1 0f 3) - Can't "SEE" what I'm missing here...
The following code contains a foreach loop that displays a list of ice cream flavor recommendations. It works great when the $flavors 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.
my Code:
<html> <body>
<h1>Flavor Recommendations</h1>
<?php $recommendations = array();
if($recommendations != "") { if (!empty($flavors)) { <ul> <?php foreach($recommendations as $flavor) { ?> <li><?php echo $flavor; ?></li> <?php } ?> </ul> } }
</body> </html>
3 Answers
Randy Hoyt
Treehouse Guest TeacherSorry for the confusion. The text should say "It works great when the $recommendations array has at least one element ... ." I must have changed the name of the array. :-( The conditional you want is this:
if (!empty($recomendations)) {
Does that help?
Jamil Smith
7,620 PointsRandy did you know if you put a "1" in the array () it passes you for this test? Does that make any sense?
<?php
$recommendations = array(1);
?><html> <body>
<h1>Flavor Recommendations</h1>
<ul>
<?php foreach($recommendations as $flavor) { ?>
<li><?php echo $flavor; ?></li>
<?php } ?>
</ul>
</body> </html>
Jamil Smith
7,620 PointsOk, too many hours struggling with this. I understand the 2nd and 3rd part of this challenge, but I can't get it right on the 1st part. I understand the:
if(!empty($recommendations)) {
but I can't get past that. I tried:
$recommendations = array(); if(!empty($recommendations)) { echo '<ul>'; foreach($recommendations as $flavor) { echo '<li>'.$flavor.'</li>'; } echo '</ul>'; }
Beyond frustrated. Thank you in advance.
Jamil Smith
7,620 Pointsthe <ul> and <li> didn't show up between the "
Jamil Smith
7,620 Pointsuhh, the unordered list and list items won't show up in code.
Randy Hoyt
Treehouse Guest TeacherIt looks like you have made a lot of changes to the code. Here's what it should look like at the end of step 1:
<?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>
<?php } ?>
</body>
</html>
Jamil Smith
7,620 PointsHaha, of course it was that simple. Thank you for the quick response Randy!
Warren Johnson
6,870 PointsHey guys, I hate to tell you this, but...Jamil, YOUR POSTS ARE BEING SENT TO ME!!! - Warren Johnson.. AND... RANDY ,...YOUR RESPONSES TO JAMIL ARE FILTERING TO ME AS WELL!???
Jamil Smith
7,620 PointsThat's just how the forum works Warren. You will see all replies.
Randy Hoyt
Treehouse Guest TeacherWarren Johnson, Once you post a comment, you are automatically subscribed to the whole thread. If you want, you can come back to the thread and click Unsubscribe at the top to stop receiving notifications.
Hope that helps!
Warren Johnson
6,870 PointsWarren Johnson
6,870 PointsThanks Randy!