Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Build a Simple PHP Application Working With Functions Displaying Only Four Shirts

8 Shirts still displaying

Having some trouble with this video.. All is working OK up to this point but the modification to the foreach loop which is meant to only display 4 shirts is not working. Here is my code.. What am I missing?

                <?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;
                ?>

Thanks for any help!

1 Answer

Hi Nic,

That code doesn't seem to be the problem. I copied and pasted it into the Project Files I downloaded and everything worked. The only difference between your code and the downloaded code is on the last line of the foreach loop and that didn't make a difference. The code below is from Treehouse. Good luck.

Jeff

<?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 = get_list_view_html($product_id,$product) . $list_view_html;
        }
    }
    echo $list_view_html;
?>