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
Thiago van Dieten
17,059 PointsDisplaying the correct 4 shirts...but not quite
Hello,
So i tried to follow Randy's explanation but it seems i made a mistake:
So I'm getting the latest 4 shirts but as you can tell, I'm not getting the right order or amount. It might be because it's late but i just can't seem my head around this problem. I would really appreciate it if someone could tell me what i did wrong. :(
Here's the code from index.php and products.php
index.php
<?php
$total_products = count($products);
$position = 0;
$list_view_html = "";
foreach($products as $product_id => $product) {
$position = $position + 1;
if($total_products - $position < 4) {
$list_view_html = $list_view_html . get_list_view_html($product_id,$product);
}
echo $list_view_html;
}?>
products.php
<?php
function get_list_view_html($product_id, $product) {
$output = "";
$output = $output . "<li>";
$output = $output . '<a href="shirt.php?id='.$product_id. '">';
$output = $output . '<img src="' .$product["img"] . '" alt="' .$product["name"]. '">';
$output = $output . "<p>View details</p>";
$output = $output . "</a>";
$output = $output . "</li>";
return $output;
} //the whole products array code is below
Thanks for reading!
3 Answers
Randy Hoyt
Treehouse Guest TeacherYou are echoing the $list_view_html variable inside the foreach loop. That shouldn't happen until after the foreach loop, after all the products have been looped through.
Here's what's happening:
- After it gets to the first shirt to display, it adds it to the variable and then echoes it out. That's the Yellow shirt.
- Then it gets the next shirt and adds it to the variable and then echoes it out again: this time it has the Yellow shirt and the Gray shirt. (That's why there's two yellows in a row, once from the first pass and then again in the second pass.)
- The third time through the foreach loop it displays three more: the Yellow, the Gray, and the Turquoise. (That's six total.)
- The fourth time through, it displays four more: the Yellow, the Gray, the Turquoise, and the Orange. (That's ten total.)
At the end of the foreach loop, your variable has the correct four shirts. Those are the last four displayed on the page right now. You are just displaying the contents of the variable on each pass instead of waiting until the end.
Does that help?
Randy Hoyt
Treehouse Guest TeacherDon't be embarrassed. I never recognized the pattern myself: it looked random to me. I had to think through the code for a bit before I figured out the problem, and only then did I understand the pattern. :~)
Hope you're enjoying the course!
Thiago van Dieten
17,059 PointsThanks, it sure did help! I am a bit embrassed though for not noticing either the shirt pattern or that i had echo'd in the foreach loop.
