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 trialPrakhar Patwa
11,260 PointsDoubt in shirt.php page coding
/* I have understand what get and post do...i have a doubt in shirt.php page, we have declared */
$product_id = $_GET['id'];
$product = $products[$product_id];
/*
In the shirts.php if we want the key of array so we did
*/
foreach($products as $product_id => $product)
/*
$product_id we define to access our keys in shirts.php, but in shirt.php we made a variable $product_id and set it equal to the $_GET["id"], can we give a different name rather then $product_id?
and in second variable $product we set it equal to the $products["$product_id"]; why we have add $products as it is not our working variable we have define it in the products.php in our include file
in short please explain me
*/
$product_id = $_GET['id'];
$product = $products[$product_id];
/* as simple as possible */
1 Answer
Vedran Brnjetić
6,004 PointsIn short,
$products is an array that contains all of our products.
In reality, $products would be loaded with products from a database, or any other source if needed.
<?php
$product_id = $_GET['id'];
$product = $products[$product_id];
In your own code, $product_id can be named differently if you wish, but I am not sure would it pass the test in this example.
By setting the $product_id to what we GET from url, we select a single product from the $products array to $product variable in order to be able to access its details and use them easily throughout the page.