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 trialMarcin Lipski
9,947 PointsHow doest $_GET"id" variable know that "id" is a "product_id"?
I the past tutorials we made the keys from arrays loading into working variable called $product_id. When the $GET variable sends request for id to the browser, does it literally scan the browser entry for part of the text, that says "id="? I hope my question makes sense.
Cheers Marcin
5 Answers
Ayan Ozturk
3,881 Points$_GET is a PHP super global which is parsing Browser URL and scanning&retrieving given parameter by name.
For example if your URL is like: http://example.com?my_param=somevalue
Using PHP and $_GET, it is possible to fetch that value like this:
<?php
$myParam = $_GET['my_param'];
?>
Here is some more information about $_GET: http://php.net/manual/en/reserved.variables.get.php
beheerdnl
2,139 PointsDo you mean this?:
<?php
$product_id = $_GET['id'];
?>
Marcin Lipski
9,947 PointsYes. Now with regards to your code snippet - How does GET request know, what value to put between square brackets? I think, it literally scans the browser entry for piece of text like this. blah blah blah - ohh... there it is "id=...". Match-Hurray!! I just would like someone to confirm, my understanding is ok.
Cheers Marcin
Aaron Munoz
11,177 PointsJust to explain this to others in the context of the course.
Earlier in this course you programmed your shirts.php file to set a GET variable in the url (around line 17 or so)
echo '<a href="shirt.php?id=' . $product_id . '">';
When you put a question mark(?) after the url, you can set a variable and assign(=) it a value.
That $product_id variable comes from your array in your products.php file.
If you look at your url every time you click on a different shirt, the id(or whatever you want to call the variable) changes to it's corresponding id set in the array. PHP knows the $_GET action is to look for that variable.
So, in a nutshell this is what you did:
set key in the $products array>>>assigned the key as a value to the variable $product_id>>> assigned the $product_id as a value to the GET variable in your url>>>
And that's how you make dynamic pages work! Hope that made sense!
beheerdnl
2,139 PointsYou must know with key is in the url query string. If you want to use another key you have to update your script.
http://example.com/?page=edit&id=5&array[]=a&array[]=b
<?php
$page = $_GET['page']; // edit
$id = $_GET['id']; // 5
$array = $_GET['array'];
echo $array[0]; // a
echo $array[1]; // b
?>
Marcin Lipski
9,947 PointsHi Nick Thanks for immediate answers. Commencing - PHP has to know, which part of the url query string is a key, loaded previously into browser from the working $product_id variable?
Cheers M
Marcin Lipski
9,947 PointsMarcin Lipski
9,947 PointsThanks Ayan. Nice one.
Marcin