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 trialKonrad Pilch
2,435 PointsPHP 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’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
miikis
44,957 PointsHey 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
2,435 PointsKonrad Pilch
2,435 PointsHI,
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.
miikis
44,957 Pointsmiikis
44,957 PointsSo "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:Let's say that the
href
attributes on each of the anchor tags leads to ashirts.php
file like so: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: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: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 theshirts.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:
Konrad Pilch
2,435 PointsKonrad Pilch
2,435 PointsAhaa, 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 ! ^^
miikis
44,957 Pointsmiikis
44,957 PointsNo problem man, I'm glad it helped. Good luck with your learning.