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

Introducing Arrays Challenge

Stuck on task 5 of 7...

This is what I have so far...I'm stuck on the foreach, I can't seem to get it right

<?php

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

?> <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>echo $flavor;</li> } ?> </ul>

12 Answers

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Cool; glad you got it working!

It sounds like you did most of steps 5 and 6 together during step 5, which is cool. I wanted to walk through adding the foreach loop a little more slowly. It sounds like you probably could have passed step 6 without making any changes at all. (You should have been able to leave the closing </li> tag in place the whole time.)

End of Step 5:

<?php foreach($flavors as $flavor) { ?>
    <li><?php echo $flavor1; ?></li>
    <li><?php echo $flavor2; ?></li>
<?php } ?>

End of Step 6:

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

Hi @Bradley

I think you're missing the HTML tags that were there at the beginning, a <ul> and an <li> tag. Email me at help@teamtreehouse.com if it doesn't wind up working for you :)

I had the HTML tags in there but they didn't copy over. I finally figured it out once I skipped the code challenge and watched the next video. This challenge seemed pretty tough for me, the first video didn't go into enough detail. Thanks for your response.

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Thanks for the feedback, Bradley! Was the difficulty in figuring out to intermingle the HTML tags with the PHP code?

My problem was I was trying to do this.

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

When I should have been doing this.

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

I think it is because I am still trying to wrap my head around php. The videos are great and they are really helping. Looking forward to finishing up the php project.

Thanks.

I'm stuck on this same question. I have:

<?php $flavors = array("Chocolate","Vanilla","Strawberry"); ?> <p>Welcome to ye olde ice cream shoppe. We sell <?php echo count($flavors); ?> flavors of ice cream.</p> <ul> <li> <?php foreach ($flavors as $flavor) { ?> <?php echo $flavor; ?> <?php } ?> </ul> Its reading how it should read but what did I do wrong???

The forum isn't showing my HTML but I have a UL and LI before for each and after as well and it's still not passing

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

(You can get the code to display properly if you put four spaces at the beginning of a line, like this.)

Type four spaces and then the code to get it indented

The code outside the foreach loop gets executed only once; the code inside the foreach loop gets executed once for each item in the array.

In this code challenge ...

  • You want one and only one <ul> tag (for the whole list). The <ul> tag should go outside the foreach loop.
  • You want three <li> tags (one for each flavor). The <li> tag should go inside the foreach loop.

Right now, the final HTML generated by your PHP code contains only one <li> tag instead of three.

Does that help?

I don't understand how to put the LI Tags within this String... <?php foreach ($flavors as $flavor ) { ?> and how would I incorporate the LI tags I did the UL tags right but I don't understand how to incorporate the LI tags within the PHP

I tried... (flavors as <li>$flavor</li><li>$flavor</li><li>$flavor</li>) and it doesn't show any text server side where to put the LI tags is my problem

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Like this:

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

I wrote the same thing and even copied your code, but it still doesn't work

Thanks man interesting challenge but #1 when I 1st wrote the code as you specified it said "3 is no longer passing" so I just started over which was good and 6 and 7 were weird you just remove the closing LI tag for 6, & add Cookie Dough and then add back the closing LI tag for 7. Interesting. Thanks- Ted