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 trialMohammad Rifqi
Courses Plus Student 3,218 Pointshelp : my code diplaying 10 shirts
<?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;
}
?>
Mohammad Rifqi
Courses Plus Student 3,218 Pointsfunction get_list_view_html($product_id, $product){
// build html output
$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;
}
Mohammad Rifqi
Courses Plus Student 3,218 Pointsi follow step by step from video and same code...
1 Answer
Jason Anello
Courses Plus Student 94,610 PointsHi Mohammad,
You have your echo statement inside the foreach
loop which is why you're getting multiple copies of the same shirts.
If you move it after the loop then you should be fine.
Mohammad Rifqi
Courses Plus Student 3,218 Pointsow yeah thanks, so embarrassing.....
Mohammad Rifqi
Courses Plus Student 3,218 PointsMohammad Rifqi
Courses Plus Student 3,218 Pointsscreen shot:
https://drive.google.com/file/d/0B_pfyGStAb_6YURnWG1lSVpvZkk/view?usp=sharing