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

abdul faiz mohd jayramie eng
PLUS
abdul faiz mohd jayramie eng
Courses Plus Student 4,478 Points

PHP arrays code challange confused by the question at 5/7. help!

im not quiet clear with the question, but i believe i did it accodingly by the instruction. dissapointly wrong. anyone? help please?

Here's the question:

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 </u>.

My Code:

<?php

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

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

</ul> 
?>

i thought it was a farely simple question, anything i left behind?

11 Answers

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

It looks like your code got swallowed up, so it's hard to tell exactly what you have typed. (You can preserve code in the forum by typing four spaces before each line of code.) I think I can see a couple of issues with the code for your foreach loop:

  • It is not contained within PHP tags.
  • It doesn't mention the array to loop through.

....

<ul>
    <?php foreach ($flavors as $flavor) { ?>
        <!-- HTML HERE -->
    <? } ?>
</ul>

In the next video, I do something very similar. Watch that video, and you should be able to see the code you need.

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Glad that helped!

I have heard similar feedback on that particular code challenge from a few other members, and I'm working on a revision to make it a little clearer how to mingle PHP and HTML code together like that.

abdul faiz mohd jayramie eng
PLUS
abdul faiz mohd jayramie eng
Courses Plus Student 4,478 Points

the code above helped me though.. i wonder why these code arent mention during the introduction video before the code challange. btw, thanks!

Gareth Redfern
Gareth Redfern
36,217 Points

I had the same issue, but this post helped straight away, thank you ;-)

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Thanks again for the feedback. I have added a new code challenge here that doesn't require mingling HTML and PHP code together yet. It just focuses on creating an array and a foreach loop.

I show how to generate list items inside a foreach loop in the next video, so I moved the original code challenge with the list items down after it.

I don´t understand whats wrong! please help-me! <?php

$flavors = array("chocolate","vanilla");

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

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Two issues:

  • You need to capitalize the names of the flavors: Chocolate, Vanilla
  • You are missing a closing PHP tag after the echo $flavor; command.

Does that help?

yes thank you Randy!

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

No problem. Keep rocking those PHP videos! :~)

Why the '<?' and '?>' surrounding the closing' }'? I am just confused by when and where the <? ?> and <?php ?> begin and end and how do should I know when <? ?> is needed? Does this just continue the earlier php when html needs to wrapped inside a php string?