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

Warren Johnson
6,870 PointsCode Challenge: Accounting for Empty Results (3 of 3): Feedback needed: Does this even look close??
Let's make sure that foreach loop executes correctly if the $flavors array is NOT empty. Add one element to the $flavors array with a value of 'Avocado Chocolate':
My Code:
<?php
$recommendations = array();
This is where I instantiated the array (Did I even have to do this???!
$flavors = array();
?> This is where I attempted to add the value to the array <?php $flavors[0] = "Avocado Chocolate"; ?>
<html>
<body>
<h1>Flavor Recommendations</h1>
<?php
if (!empty($flavors)) {
echo '<ul> <?php foreach($recommendations as $flavor) { ?> <li><?php echo $flavor; ?></li> <?php } ?> </ul>'; } else { echo '<p>There are no flavor recommendations for you.</p>'; } ?>
</body> </html>
I have tried every single placement of the code, and it still just DOES NOT WORK...
8 Answers

Matthew Floyd
7,413 PointsMy code for step 3 won't work, I've even gone so far as to use the code Randy supplied above, and even that will not work. Please advise!

Peter Hatzer
20,837 Pointshey, I was having trouble with this one also but I just figured it out your trying add the 'Avocado Chocolate' to the $recommendations = array() like so :
$recommendations = array('Avocado Chocolate');
Igor Prymak
13,590 PointsWow! This one really saves the day!

Mazen Itani
9,134 Pointsput this after creating the array:
$recommendations[] = "Avocado Chocolate";

Randy Hoyt
Treehouse Guest TeacherI apologize for the confusion! I said the array was called "flavors" in the text, but it's really called "recommendations." I'll get that fixed. I can tell you definitely understand the concept. The conditional should look like this:
if (!empty($recommendations)) {

Jamil Smith
7,620 PointsHey Randy. On this last challenge I erased the whole ELSE part of the conditional just so it displayed the "Avocado Chocolate" without the message of "There are no flavor recommendations for you". It let me pass this way, but is this the correct way of doing it?

Randy Hoyt
Treehouse Guest TeacherHmmm ... that doesn't sound right. Can you post your code here so I can have someone take a look?
Once you add an element to the array, then the array should no longer be empty. If the conditional is set up correctly, then the message shouldn't be displayed. It should look something like this:
<?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 } ?>
Does that make sense?

Samuel Barney
Courses Plus Student 1,094 Points@Randy I was using $flavors as well took me a while to figure out after I looked at this forum.

Web Admin
6,198 PointsI refactored the code so it wouldn't have to use so many opening and closing php tags like this:
<ul>
<?php
if (!empty($recommendations)) {
foreach ($recommendations as $flavor) {
echo '<li>' . $flavor . '</li>'
} else {
echo '<p>There are no flavors that match your search</p>'
}
} ?>
</ul>
It's not working. Is that because the code itself doesn't work or because the challenge wants me to write it the other way?

Dario Morbidi
33,072 PointsNo, because the test requests that the unordered list has not to be printed when the array is empty. The way you did always print <ul></ul>.

Cody Craig
10,326 PointsIs there a way to refactor it the way Web Admin tried to do? I am curious in this method as the one in the exercise is confusing to me.

Chhavy Mahoney
2,649 PointsThis code below has worked for me <?php $recommendations = array('Avocado Chocolate'); 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 } ?>