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!

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

Matthew Root
Matthew Root
5,392 Points

Question about $_GET

Could someone walk me through the theory of this:

if (isset($_GET["id"])) { $id = $_GET["id"];

if (isset($catalog[$id])) { $item = $catalog[$id]; }

Thanks so much!

2 Answers

Kevin Korte
Kevin Korte
28,147 Points

Sure, the first one:

if (isset($_GET["id"])) { $id = $GET["id"]};

You're just saying if there is a get variable set, than set a variable called $id to the value of that.

Meaning, if you had a URL like so:

<?php
//http://mydomain.com?id=dxEAs45E7s

if (isset($_GET['id'])) { $id = $_GET['id']}
//$id = dxEAs45E7s

//http://mydomain.com?value=some%20great%20value

if (isset($_GET['id'])) { $id = $_GET['id']}
//$id doesn't exist

The second one has nothing to do with get variables.

if (isset($catalog[$id])) { $item = $catalog[$id]; }

Is just checking if $catalog is an array, and has key of $id, and if it does, than set a variable $item to the value of the key $id.

This would be something commonly used if you were looping through an array and wanting to pull out specific parts of it.

Matthew Root
Matthew Root
5,392 Points

Thanks buddy!
if (isset($_GET["id"])) So what you are saying is that this above means is that if there is an id at the end or the url treehouse-app.com/details.php?id=104 , that page will correspond to the $id in the data.php page. Which is what this: $id = $GET["id"]} means.

The $item is then referenced on a function page without an include. You don't have to include on the top of pages for it to recognized a variable set on another?

Kevin Korte
Kevin Korte
28,147 Points

Right, so in this case, $id would be equal to 104.

I have a feeling this question is relating to a course/quiz on here, and not random like I thought it was. Do you have a link to project you're referring to?

What it sounds like, is that you're getting the id value from the url, and than checking if that id is in an array, and if it is, setting another variable to the value of the id. I'm going to guess that $item ends up being in array itself, and that $catalog is a nested array where the key would correspond to the $id, and the value would be another array.

Matthew Root
Matthew Root
5,392 Points

the link is https://teamtreehouse.com/library/build-a-basic-php-website/listing-and-sorting-inventory-items/random-fun-with-arrays This was the original code: foreach($catalog as $id => $item) { }

but when putting the foreach into a array_rand the $item is then deleted and only $id is in the argument.

$random = array_rand($catalog,4); foreach($random as $id) { echo get_item_html($id,$catalog[$id]); }

Is the $item that is used in other parts of the code referring to: if (isset($catalog[$id])) { $item = $catalog[$id]; }