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

Konrad Pilch
Konrad Pilch
2,435 Points

PHP LESSON 1

Hello,

I jst want make sure that i get this part here:

Code:

<div class="section shirts page">

            <div class="wrapper">

                <h1>Mike&rsquo;s Full Catalog of Shirts</h1>

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

            </div>

        </div>

PHP:

$product_id = $_GET['id'];
$product = $product[$product_id];

So this means that on the product_id with $_GET we are making a variable that the name is $product_id and with $_GET['id']; we are getting or taking the id of the products ? in [] ? or what is this id stands for there? is it a made up or is it a build in that takes the id of array or something?

here is extra part might be used as well :

$products = array();
$products[101] = array(
    "name" => "Logo Shirt, Red",
    "img" => "img/shirts/shirt-101.jpg",
    "price" => 18,
    "paypal" => "9P7DLECFD4LKE"
);
$products[102] = array(
    "name" => "Mike the Frog Shirt, Black",
    "img" => "img/shirts/shirt-102.jpg",
    "price" => 20
);

1 Answer

Hey Konrad,

$product_id = $_GET['id'];
$product = $product[$product_id];

In the above, $_GET is a PHP Internal Function that gets the URL, of the current script/page, for a given key=value pair. In this case, if the URL is something like http://www.shirts4mike.com/shirts, then $_GET['id'] looks for an id=SOME-VALUE pair, that come after a "?" — like so: http://www.shirts4mike.com/shirts?id=101.

Subsequently, the 101 value (just an example value) is stored in the $product_id variable.

If I remember correctly, the whole point of doing this was to be able to dynamically access the contents of an associative array — i.e., if you have an array called "$product", you'd able to run $product[$product_id], which is the same thing as running $product[$_GET['id']], which (if, for example, the "id" happened to be equal to "101") is the same as running $product[101].

Hope that clears it up well. If not, try reading through the PHP Docs; they're pretty helpful.

Good Luck.

Konrad Pilch
Konrad Pilch
2,435 Points

HI,

Where is or what is and id? does the id come form this?

$products[101] = array

and the id of this is 101 but the PHP knows its id? im a bit confused by the code there with Randy.

So "id" is just the name of the key that happened to be used here— it's not an internal keyword nor does it have any special significance. Let me back up a little.

Let's say that on the index.php page there's a list of shirts that all have anchor tags like so:

<ul>
    <li><a href="#"><img src="image1.jpg"></a></li>
    <li><a href="#"><img src="image2.jpg"></a></li>
    <li><a href="#"><img src="image3.jpg"></a></li>
    <li><a href="#"><img src="image4.jpg"></a></li>
    <li><a href="#"><img src="image5.jpg"></a></li>
</ul>

Let's say that the href attributes on each of the anchor tags leads to a shirts.php file like so:

<ul>
    <li><a href="shirts.php"><img src="image1.jpg"></a></li>
    <li><a href="shirts.php"><img src="image2.jpg"></a></li>
    <li><a href="shirts.php"><img src="image3.jpg"></a></li>
    <li><a href="shirts.php"><img src="image4.jpg"></a></li>
    <li><a href="shirts.php"><img src="image5.jpg"></a></li>
</ul>

Well, we probably want the shirts.php page to display different contents for each page/shirt we click on right? We could just make a separate page for each shirt (blue-shirt.php, red-shirt.php etc.)... but that's a little retarded and frankly we're better than that. Instead, we give each of the shirts a unique query (a key/value pair), to pass along with the URL (shirts.php) — like so:

<ul>
    <li><a href="shirts.php?id=101"><img src="image1.jpg"></a></li>
    <li><a href="shirts.php?id=102"><img src="image2.jpg"></a></li>
    <li><a href="shirts.php?id=103"><img src="image3.jpg"></a></li>
    <li><a href="shirts.php?id=104"><img src="image4.jpg"></a></li>
    <li><a href="shirts.php?id=105"><img src="image5.jpg"></a></li>
</ul>

Why did we use "id"for the key of the query? Strictly for the fun of it...and because it makes life easier down the road. We could have just as easily chose "thingy1" for the key...but we'll exercise a little discretion here and not do that.

So now, anytime someone clicks on a shirt, not only are they taken to the shirts.php page, but, because of the URL (and the query contained therein), it becomes possible to know which shirt was clicked.

It's important to note that the values for each key (101, 102, 103 ...) are also up to us to decide. The reason we picked "101", for instance, is because the associative array that contains each shirts' information, ($product) has a key of 101, 102, 103 — like so:

$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")
);
$products[104] = array(
    "name" => "Logo Shirt, Green",
    "img" => "img/shirts/shirt-104.jpg",    
    "price" => 18,
    "paypal" => "YKVL5F87E8PCS",
    "sizes" => array("Small","Medium","Large","X-Large")
);
$products[105] = array(
    "name" => "Mike the Frog Shirt, Yellow",
    "img" => "img/shirts/shirt-105.jpg",    
    "price" => 25,
    "paypal" => "4CLP2SCVYM288",
    "sizes" => array("Small","Medium","Large","X-Large")
);

So, if we click on that first shirt in the list, we are taken to the shirts.php page, with a query passed along of ?id=101. Now, we could put a script on the shirts.php page, that gets the value of the key named "id", in the URL's query string, and stores it in a variable — like so:

$product_id = $_GET['id'];

Then, we could get the info for the shirt we clicked on (let's say from an array called "$products") and... do whatever we want with that info, like store it in a variable called "$product" — like so:

$product_id = $_GET['id'];
$product = $products[$product_id];
Konrad Pilch
Konrad Pilch
2,435 Points

Ahaa, i do understand it now more :) i will read it over again , and put it practice soon. Thank you very much for the answer . Its really clean and easy to understand . Randy makes this a little confusing sometimes : p

Thank you very much ! ^^

No problem man, I'm glad it helped. Good luck with your learning.