Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

alex mattingley
alex mattingley
7,508 Points

Code Challenge: More Excitment with Arrays is the bain of my existence.

These are the directions for the challenge:

Right now, the unordered list does not show the flavors from our array. In the next few tasks, we'll change that. This task has two steps. (1) Add the start of a foreach loop with an opening curly brace after the opening <ul> tag but before the first opening <li> tag. The foreach loop should load each element from the flavors array, one at a time, into another variable named flavor. (2) Add the closing curly brace for the foreach loop after the final closing </li> but before the closing </ul>.

This is the original code that you start with:

    $flavors = array("Chocolate", "Vanilla", "Mint Chocolate");


?>
<p>Welcome to Ye Olde Ice Cream Shoppe. We sell <?php echo count($flavors); ?> flavors of ice cream.</p>
<ul>
    <li><?php echo $flavor1; ?></li>
    <li><?php echo $flavor2; ?></li>
</ul>

This is the code that I am inputing:

    $flavors = array ('Chocolate', 'Vanilla', 'Mint Chocolate');

?>
<p>Welcome to Ye Olde Ice Cream Shoppe. We sell <?php echo count($flavors); ?> flavors of ice cream.</p>
<ul>
    <?php foreach ($flavors as $flavor) {?>
       <li><?php echo $flavor;?><li>
    <?php}?>
</ul>

What am I doing wrong? I have also tried scaling back my answer a couple of steps, but that does not seem to work either. Any help would be appreciated.

6 Answers

It's not the php, it's the html. look at the <li> tags. It should be:

<li><?php echo $flavor; ?></li>
Kieran Riggall
Kieran Riggall
2,910 Points
<?php

    $flavors = array("Chocolate" , "Vanilla" , "Oreo");

?>
<p>Welcome to Ye Olde Ice Cream Shoppe. We sell <?php echo count($flavors); ?> flavors of ice cream.</p>
<ul>
    <?php foreach($flavors as $flavor) { ?>
    <li><?php echo $flavor; ?></li>
    <?php } ?>
</ul>

try this!

Looks like you're missing the '/' before your closing </li> tag.

alex mattingley
alex mattingley
7,508 Points

Yep, you were right, but I was missing an essential element of PHP as well.

alex mattingley
alex mattingley
7,508 Points

Yep, you were right, but I was missing an essential element of PHP as well.

Hi, I've been working on this deep dive. I'm past this part but not sure where the question is located. If you provide a link to the actual question I may be able to better help. I think the issue is this line:

 <li><?php echo $flavor;?><li> 

It's already inside <?php ?> tags check your use of echo command. You want to echo the < li > you've got it mixed in with your < li >

It's not the php. It's the html. The php is surrounded by two <li> tags instead of <li></li>.

Ugh. Screwed up the formatting of this reply and can't seem to edit it. See below.

alex mattingley
alex mattingley
7,508 Points

Thank you everyone for your help. This ended up being the correct answer:

    $flavors = array ('Chocolate', 'Vanilla', 'Mint Chocolate');

?>
<p>Welcome to Ye Olde Ice Cream Shoppe. We sell <?php echo count($flavors); ?> flavors of ice cream.</p>
<ul>
    <?php foreach ($flavors as $flavor) {?>
       <li><?php echo $flavor;?></li>
    <?php } ?>
</ul>

There were two issues in the code.

1) I was missing a space in the last PHP Command. I had it as <?php}?> and it needed to be <?php } ?> 2) The second issue was with my closing li tag. Instead of </li> I had <li> as the closer.

Jasmine Daniel
Jasmine Daniel
1,585 Points

OMG......... I had a semi colon after ($flavors as $flavor) like ($flavors as $flavor);

Grr....!!!