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
Gaetan Masson
5,829 PointsBuilding the Shirt Details Page
Hi there, I have a question on part of the code from the Building the Shirt Details Page lesson:
$product_id = $_GET['id']; ?>
Where does the 'id' comes from?
I understand it's the key corresponding to the product, but where did we defined it?
Was it in shirts.php with the line:
echo '<a href="shirt.php?id=' . $product_id . '">';
or
<?php foreach($products as $product_id => $product) {
I'm a bit confused on this part (I'm really new at PHP) and sorry if my English is not perfect, it's not my native language...
Gaetan.
2 Answers
Juan Ferreira
47,991 PointsGaetan the id is passed as a GET variable from within the URL string of the website. For example if the URL is http://www.shirts4mike.com/shirt.php?id=107 then $_GET['id'] would equal to 107 because id was the variable assigned and was passed this value via the URL. Just remember that to the left of the ? mark would be the server path of the php being executed. Anything to the right of the ? would be the variables assigned and passed by the GET variable.
Gaetan Masson
5,829 PointsHi Juan,
Thanks for the answer.
I now have understood how it works, I didn't think it was as easy as setting up an <a href></a> with a variable in the link to set a variable in a web address.
Cheers.