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 Redirecting Invalid Shirt IDs

Elena Paraschiv
Elena Paraschiv
9,938 Points

Can someone explain me in detail this part?

I don\t understand how this is working. Can someone explain me the steps of this part:

$product_id = $_GET ["id"];
$product = $products[$product_id] ;

4 Answers

Vedran Brnjetić
Vedran Brnjetić
6,004 Points

Basically, for that to work, the following must be preset:

Simplified version You must have an associative array - a list of products, identified by their id.

$products=array( 1 => "product 1", 2 => "product 2", 3 => "product 3");

via get method you get the product id which you store into the $product_id.

$product_id=$_GET["id"];

Then you take the associated product from your $products array by using the id, which is the key in this case.

$product=$products[$product_id];

So if I enter shirt.php?id=2 in my address bar, my $product should evaluate to "prouct 2".

This is as simple as I managed to explain, please tell if you need anything further explained.

Elena Paraschiv
Elena Paraschiv
9,938 Points

In the last part "Then you take the associated product from your $products array by using the id, which is the key in this case."

the keys are the ones before the => sign , meaning 1 2 and 3 ?

Vedran Brnjetić
Vedran Brnjetić
6,004 Points

Yes, exactly. Associative array is an array where values are paired with reference keys so you can access them programatically without having to search through all the values. Just like an index in a database.

$assoc_array = array("key1" => "value1", "key2" => "value2");

The key can either be an integer or a string. The value can be of any type. You can read more about php arrays here: http://php.net/manual/en/language.types.array.php

In this particular case, you can download the project files related to that video and you will find that the products are stored in the "products.php" file which is included in the "shirt.php".

$products is an associative array, but it has been set a bit differently; the numbers in [] are the keys, or product ids in this case:

$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")
);
$products[103] = array(
    "name" => "Mike the Frog Shirt, Blue",
    "img" => "img/shirts/shirt-103.jpg",    
    "price" => 20,
    "paypal" => "7T8LK5WXT5Q9J",
    "sizes" => array("Small","Medium","Large","X-Large")
);
Elena Paraschiv
Elena Paraschiv
9,938 Points

Thank you Vedran. Now I understand this part

Vedran Brnjetić
Vedran Brnjetić
6,004 Points

You are most welcome, here, I went to further investigate the matter by examining the project files from the video. Hope this clears it up even further.

Happy coding

Elena Paraschiv
Elena Paraschiv
9,938 Points
so 
$products[101]
$products [102]
and so forth are nested inside $products = array (); so it is multiple arrays inside an array

But thenif  ''the numbers in [] are the keys""
"name"
"img"
"price" and so forth aren't also keys?


So the ? is
$products[101] = array                  ~ [101]- is a key or is it a product id?

if in  "name" => "Logo Shirt, Red",   ~ "name" is a key ?
Vedran Brnjetić
Vedran Brnjetić
6,004 Points

Yes, they are also keys. As I said before, key can be an integer or a string, so "name" can be a key.

what you have here is a multi-dimensional array. $products is an array of arrays. Each product is an array. Each product also contains an array of sizes.

$product=$products[101];

101 is both the key, and the product id, that's the magic of it, so we can use the method from your question to quickly find the product we are looking for.

Now the $product is also an array and we can get its properties:

$name=$product["name"];
$price=$product["price"];
$sizes=$product["sizes"];

Now the $name will be "Logo Shirt, Red" and the $price will be 18. $sizes will be array("Small","Medium","Large","X-Large")

Elena Paraschiv
Elena Paraschiv
9,938 Points

That\s pretty cool stuff. I always thought engineering looks like magic. Excited to learn more now!

Vedran Brnjetić
Vedran Brnjetić
6,004 Points

glad to hear that =) Keep up the good work.

Elena Paraschiv
Elena Paraschiv
9,938 Points

thanks. You too ! Happy coding :)