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 Build a Simple PHP Application Working With Functions Displaying Only Four Shirts

Mohammad Rifqi
Mohammad Rifqi
Courses Plus Student 3,218 Points

help : my code diplaying 10 shirts


                    $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
Mohammad Rifqi
Courses Plus Student 3,218 Points
function 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;

1 Answer

Hi 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.