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

Spen Taylor
Spen Taylor
13,027 Points

Showing a product without a "For Each" loop.

Today I've been stuck on displaying a product on a personal project site of mine without having it in a For Each loop, which has made me question my entire understanding of what is actually happening in the For Each loop!

My loop is the same as in the PHP project track and is as follows :

(in "store.php")

<?php 
foreach ($products as $product_id => $product) { 
echo getListViewHTML ($product_id,$product);
} ?>

(in "products.php" which contains all product details and functions)

<?php

function getListViewHTML($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;
};

Am I right to think that the "For Each" is just spitting out values for the functions $product_id and $product arguments? The first instance passing 101 to $product_id and [the entire product array] to $product ?

I'd love it if somebody could just give me a little bit of clarity on this! I'd also love more if somebody could explain how to show 1 product without using a "for each".

Thanks, Spen

1 Answer

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Hey Spen,

(1) You can access an individual element in an array with its key, like this:

$products[101]

(That's what we do in the Building the Shirt Details Page video.)

(2) Let me walk you through an example from one of the first videos on arrays to see if this helps clarify exactly what the foreach loop does:

flavors = array("Vanilla","Chocolate","Strawberry");

foreach($flavors as $flavor) {
    echo $flavor . "\n"; 
}

The first line creates an array. The foreach loop then goes through the array, one element at a time, and executes a block of code for each element. The code inside the foreach loop (echo $flavor) will be executed three times because there are three elements in the array.

As the foreach loop goes through all the elements in the array, it will take the element it is currently looking at and load it into a working variable named $flavor. The first time the code inside the foreach loop is executed, the $flavor variable has a value of "Vanilla" because that's the first element in the array. The code inside the foreach loop echoes out "Vanilla" to the screen on this first pass through the loop.

The loop continues to the second element. The $flavor variable now has a value of "Chocolate" because that's the second element in the array. The code inside the foreach loop echoes out "Chocolate" to the screen on this second pass through the loop.

The loop continues to the third element. The $flavor variable now has a value of "Strawberry" because that's the third element in the array. The code inside the foreach loop echoes out "Strawberry" to the screen on this third pass through the loop.

Does that help?

Spen Taylor
Spen Taylor
13,027 Points

Hi Randy and thanks for your reply :)

I think I just confused myself a bit here as now that you've written it like that I remember the video! I've been busy working on my site for the past couple of eveneings and managed to give myself a much better understanding of what is actually happening (using echos now and again to show the values that were being passed around).

I'm now having a headache trying to pass a PHP variable to Javascript function (again as an argument) and have the argument make up part of a CSS ID... my brain hurts!

Thanks again! Spen