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

Confused about shirt listing for shirts4mike on the homepage

Hello,

After watching - http://teamtreehouse.com/library/build-a-simple-php-application/working-with-functions/displaying-only-four-shirts - I'm a little confused about what is actually happening, here is the code:

<?php include("inc/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) {

$list_view_html = get_list_view_html($product_id,$product) . $list_view_html; } } echo $list_view_html; ?> </ul>

I can kind of see what is happening and I understand most of it, but what exactly is $list_view_html = get_list_view_html($product_id,$product) . $list_view_html; doing? I don't understand why we concatenate the $list_view_html at the end of that line of code??

It's probably really obvious but I just need someone to explain it to me :)

Thanks,

Adam

7 Answers

I have an answer! After some searching and getting jeered at from those b-holes on stackoverlow, it all makes sense.

So when you do $output = $output . $number the first time, it will only contain one number. For example, 1.

The second time you get the $output = $output . $number, $output is the number 1 and your placing the next number afterwords, so you end up with 1 and 2. Lather rinse repeat until you hit 4.

But when you do $output = $number . $output after $output contains the number 1, you are placing the new $number value BEFORE the $output each time so you end up with a 2 behind the 1.

It's so deceptively simple....

Hi Nicholas,

That helps abit, the part im stuck with is, if it says <?php $list_view_html = get_list_view_html($product_id,$product) . $list_view_html; ?> it will display shirt numbers 8,7,6,5 in that order, but if it says <?php $list_view_html = $list_view_html . get_list_view_html($product_id,$product);?> it displays shirt numbers 5,6,7,8 in that order instead? My issue is I can't see why, the get_list_view_html function code is:

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

} ?>

I'm just completely lost as to how the $list_view_html variable determines which order the shirts appear in, if that makes sense.

Thanks, Adam

Adam,

$list_view_html is essentially the listing of items pulled from the products.php script that was included, but I imagine you understood that already.

It's really hard to get specific about what going on because both of the named functions do not show their code. I can tell you the reason for the concatenation is because the get_list_view_html() function is generating a new block of code containing a new item and by concatenating it with the previous value of $list_view_html, it's able to stack another block of code onto the previous ones for the number of times the loop will run. This way it will dynamically generate items provided the criteria of the foreach loop is met.

Does that make any sense?

In theory, it shouldn't determine the order. Period. The order that is set up in the products array shouldn't change based on an arbitrary flip flop of what gets added to the $list_view_html.

Is there any more code that you haven't posted yet? What exercise is this on? I am just as stumped as you are :p

Hahaha okay better example:

<?php

$numbers = array(1,2,3,4);

$total = count($numbers);

$sum = 0;

$output = "";

$i = 0;

foreach($numbers as $number) {

$i = $i + 1;

if ($i < $total) {

    $output = $number . $output;

}

}

echo $output;

?>

Now, when this code is ran, it generates 321, but, if I change it so the $output line says:

$output = $output . $number;

It will generate 123, and I don't see why? It might just be me missing something obvious xD

Thanks,

Adam

Ah! Makes much more sense now. Still a bit strange how it all works but I'm sure ill get used to it! :)

Thanks for your help!

  • Adam

Question answered by Moderator here:

https://teamtreehouse.com/forum/php-displaying-only-four-shirts

;p