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

PHP: Displaying only four shirts

Can someone explain to me, in layman's terms, why this code:

$list_view_html = get_list_view_html($product_id,$product) . $list_view_html;

changes the order of the shirts being displayed?

I would imagine that the order of shirts would be determined by the way you call the $position variable that knows what number shirt you're on.

Using a blank variable called $list_view_html and then simply concatenating the function on it seems like voodoo to me. How and why does it work this way?

I can't seem to wrap my head around it.

Here's the full code for context:

<div class="section shirts latest">

            <div class="wrapper">

                <h2>Mike&rsquo;s Latest Shirts</h2>

                <?php include("includes/products.php"); ?>

                <ul class="products">
                        <?php 
                        $total_products = count($products);
                        $position = 0;
                        $list_view_html = "";
                        foreach($products as $product_id => $product) { 
                            $position = $position + 1;
                            if ($total_products - $position < 4) { //If this number here is less than 4, if there are fewer than 4 shirts left after this one, then we know we are looking at one of the last 4 shirts.
                            $list_view_html = get_list_view_html($product_id,$product) . $list_view_html;

                            }
                        }
                        echo $list_view_html; 
                    ?>                      
                </ul>

            </div>

        </div>

2 Answers

Gareth Borcherds
Gareth Borcherds
9,372 Points

The function, as you allude to above, is not meant to change the order in which the shirts are displayed, instead, this function is simply displaying the last 4 shirts out of all the shirts.

Basically, as the comment says for the if statement, you know you are looking at one of the last 4 shirts once that if statement passes as true, which then lets you use the last 4 as your latest 4 shirts.

As for the function and appending strings, all the function does is create a block of html code for each shirt, so you start with a blank variable, add the result of the function to it, which is the first shirt, and then you add that code to the result of the second function which is the second shirt and so on. All this is doing is basically creating one big output block of html string that eventually displays your 4 shirts.

Hello Gareth So why this approach of concatenating function result to the blank variable has not been implemented when displaying 8 t-shirts in the original t-shirt.php page?

Oh, i get it now.

In the case:

$list_view_html = get_list_view_html($product_id,$product) . $list_view_html;

It's simply placing the new shirt to the left side of the shirt that was added before them.

This is the logic.

The arrows here represent the order of the shirts, the Yellow was the first to be added, so it stays as first.

You must read left to right.

example:

Blank

Yellow <-- Blank

Grey <-- Yellow

blue <-- Grey, yellow

You are adding blue to the left side of Grey and Yellow because the code already loop this two before

Then:

Red <-- Blue, Grey, Yellow

same as

get_list_view_html($product_id,$product) . Blue, Grey, Yellow

or

get_list_view_html($product_id,$product) . $list_view_html;

the variable $list_view_html was empty at first, then Yellow was added to it, then Grey, Blue and so on.


So if you put $list_view_html before get_list_view_html($product_id,$product) it will work the other way around.

Blank --> Yellow

Yellow --> Grey

Yellow, Grey --> Blue

Yellow, Grey, Blue --> Red

same as

Yellow, Grey, Blue . get_list_view_html($product_id,$product)

After you have 4, the loop ends and you have those 4 shirts displayed as html.