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

PHP

Having trouble with Challenge task 5 of 7 PHP array (foreach). Please Help!

Challenge task 5 of 7 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>.

My Code answer:

<?php

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

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

What am I doing wrong?

For some reason my code cut off on the posting and I cant put my full code, what is going on?!

If you don't mind taking a look at Build a Simple PHP Application - Listing Inventory Items - More excitement with Arrays - challenge 5 of 7.

Thanks!

4 Answers

Aaron,

It's hard to tell because of the formatting of your post, but I see a few potential problems:

Looks like you're missing a semi-colon at the end of your call to count($flavors).

I think you'll also need to echo that out, like so:

We sell <?php echo count($flavors); ?> flavors of ice cream.

Later, you're trying to open a new php block inside of another (once you've opened a php block with <?php, as long as you haven't closed it with ?> you don't need to open a new block for other php operations). So, instead of this:

<?php foreach($flavors as $flavor) { <?php echo $flavor; ?> }

it should read:

<?php foreach($flavors as $flavor) { echo $flavor; } ?>

It looks like for the purpose of this challenge, though, all they're looking for is an empty foreach loop, like this:

<?php foreach($flavors as $flavor) { } ?>

You'll probably add code between the curly brackets in the next step.

Hope this helps!

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Aaron;

Your code markup is indeed all chopped up, but I don't see the <ul>and <li> tags in your code. The challenge is looking for a foreach loop between the <ul></ul> tags. To point you in the correct direction it will look something like:

<ul> 
    <?php foreach ($flavors as $flavor) { ?>
                <!-- HTML with <li> tags goes here -->
  <?php } ?>  
</ul>

Happy coding,

Ken

Thanks guys, I've figured it out. I think I've been coding a little too long. I just misread the question. Thanks again guys!

I'm not sure exactly where I went wrong either, my code looks identical, but after copying Shawns code above (3rd block) it went through ok.