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 trialUmair Yaquoob
7,500 Pointsempty results Challenge 3/3
Question "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'."
I'm confused don't know what's wrong. here my code below:
<?php
$recommendations = array();
?> <html> <body>
<h1>Flavor Recommendations</h1>
<?php if(!empty('Avocado Chocolate')) { ?>
<ul>
<?php foreach($recommendations as $flavor) { ?>
<li><?php echo $flavor; ?></li>
<?php } ?>
</ul>
<?php } else { ?>
<?php echo '<p>There are no flavor recommendations for you.</p>'; ?>
<?php } ?>
</body> </html>
7 Answers
Giovanni CELESTE
20,961 PointsHi Umair,
Did you add correctly your flavor in $recommendations
?
For what I see from your code, your if
is testing the 'Avocado Chocolate', shouldn't it test the $recommendation
instead?
For adding a new flavor, write something like this $array[] = $newElement ;
.
Hope it helps you.
Umair Yaquoob
7,500 Pointsthis code below passed on the 2 tasks it just on final one I'm confuse:
<?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 } ?>
Giovanni CELESTE
20,961 PointsHi again,
What is your question? Why are you confused?
Umair Yaquoob
7,500 PointsThis is the question i'm not show where I am meant to put the Avacado Chocolate?
"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'."
I'm confused don't know what's wrong. here my code below:
<?php
$recommendations = array();
?>
<h1>Flavor Recommendations</h1> <?php if(!empty($recommendations)) { ?> <ul> <?php foreach($recommendations as $flavor) { ?> <li><?php echo $flavor; ?></li> <?php } ?> </ul> <?php } else { ?> <?php echo '<p>There are no flavor recommendations for you.</p>'; ?> <?php } ?>
Giovanni CELESTE
20,961 PointsLook again at my previous comment. You have to add your new flavor to the array $recommendations.
Umair Yaquoob
7,500 PointsThat's what I have done but it still wrong here my code below:
<?php
$recommendations = array('Avocado Chocolate');
?><html> <body>
<h1>Flavor Recommendations</h1>
<?php if(!empty($flavors)) { ?>
<ul>
<?php foreach($recommendations as $flavor) { ?>
<li><?php echo $flavor; ?></li>
<?php } ?>
</ul>
<?php } else { ?>
<?php echo '<p>There are no flavor recommendations for you.</p>'; ?>
<?php } ?>
</body> </html>
Giovanni CELESTE
20,961 PointsOkay Umair, let's go back to what you have done.
$recommendations = array('Avocado Chocolate');
You set you array $recommendations and add the flavor 'Avocado Chocolate'. That's good. Then
<?php if(!empty($flavors)) { ?>
Here you make your condition with a $flavors, but where did you get this $flavors? What is it? For Php for now, you only have a $recommendations, no $flavors. That's when your php didn't understand what you meant. Is it ok for you now?
PS: If I remember correctly, it is a good behaviour to code your array like this, and separate HTML from PHP (but that works as well with what you did).
$recommendations = array();
$recommendations[] = 'Avocado Chocolate';
<?php } else { ?>
<p><?php echo 'There are no flavor recommendations for you.'; ?></p>
<?php } ?>
Umair Yaquoob
7,500 PointsThank you for your help I figured it out.