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 Integrating with PayPal Building the Shirt Details Page

Confused by several things...

I've watched the video several times, and what I would like help understanding follows:

On the shirts.php, we have the following code: <?php foreach($products as $product_id => $product)...The narrator, Randy Hoyt, then goes on to say: "3:59 We can modify our foreach loop to load the key of the array element 4:01 into a separate working variable using the double arrow operator" OK, we see that above.

The wording makes it seem like only the key of the arrayis loaded into $product_id whereas the value for a specific key is loaded into the variable named $product.

Next, we see the following code a few lines down: echo '<a href="shirt.php?id=' . $product_id . '">'; What is the totality of what's been assigned to "id"? Well, one might say whatever was assigned to $product_id, but what, exactly was that???

The mystery only deepens from here (at least for me). For example, hen we go to the next video in the series, we see the following code: <?php include("inc/products.php); $product_id = $_GET["id"]; $product = $products[$product_id];

So, I'm guessing the "id" (in $GET["id"]) is the shirt number, 101 for example. The next line, $product = $products[$product_id] is totally baffling to me.
I understand well enough that something is being assigned to $product. By what comes up momentarily, it looks like everything in the array fro a few products, i.e., name, img, price, and paypal. In other words, both the key and the value of the products array??? I'm confused here. But let me go over it as I understand it: It looks like both the arrays key AND value are being assigned to $product?? But how could this be? It looks link only the product id, as represented by $product_id would be assigned. Also, what is going on with the $products variable? I'm guessing that's related to the include("inc/products/php") where the product array exists? Plese help cause it's not exactly making sense at this point. Thanks so much! being assigned $product

1 Answer

To start, the $product variable is a multi-dimensional array. That means an array of arrays.

To get a value of a key from an array in an array is below.

<?php echo $products[101]['name']; ?>
<?php
$products = array();
$products[101] = array(
    "name" => "Logo Shirt, Red",
    "img" => "img/shirts/shirt-101.jpg",
    "price" => 18,
    "paypal" => "9P7DLECFD4LKE",
    "sizes" => array("Small","Medium","Large","X-Large")
);
$products[102] = array(
    "name" => "Mike the Frog Shirt, Black",
    "img" => "img/shirts/shirt-102.jpg",
    "price" => 20,
    "paypal" => "SXKPTHN2EES3J",
    "sizes" => array("Small","Medium","Large","X-Large")
);


// On the first run of the loop the value of $product_id will be 101.
// That value 101 and so on ( 102, 103) will be put in $product for a round in the loop. 

// This gives us access to the img, name, and whatever else is in that 2nd level array. 
// Its the same as echoing out $products[101]['name'];

<ul class="products">
    <?php foreach($products as $product_id => $product) { 
        echo "<li>";
        echo '<a href="shirt.php?id=' . $product_id . '">';
        echo '<img src="' . $product["img"] . '" alt="' . $product["name"] . '">';
        echo "<p>View Details</p>";
        echo "</a>";
        echo "</li>";
    }

</ul>
?>

$product is not used for the id in the URL because php would attempt to echo out the array which it cannot (Array to string conversion notice from the server) . Instead, the $product_id variable is used because it is only an integer and it gets concatenated into a string.

<?php echo '<a href="shirt.php?id=' . $product_id . '">';

// Outputs the string: '<a href="shirt.php?id=101>' ?>

// If we click the link above, we send this to the URL 'shirt.php?id=101' 

$product_id = $_GET["id"]; // This is now equal to the integer 101 that is pulled from the URL. 

$product = $products[$product_id]; // this would output the value of $products[101] . This is an array that cannot be echo'd 

echo $product['name']; // would give the same output as the lengthier variable value of  $products[ $_GET["id"] ]['name']

The key (101, 102, etc ) is being assigned in the order they are defined from first to last. The first round of the loop is accessing $products[101] .

Yes - $products array is from the included php file named products

Thank you for your efforts in trying to clarify all of that. It's still a little bit unclear, honestly, but I will do some experimentation to see if that helps.