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

Question about $_GET["id"]

Hilo,

I do not understand the command $product_id=$_GET["id"] because the array as follow does not include a key "id". So, I don't get how this command will be able to know that 101, 102, 103...etc are the ids.
$products = array(); $products[101] = array( "name" => "Logo Shirt, Red", "img" => "img/shirts/shirt-101.jpg", "price" => 18, "paypal"=>"9WAMX3PJFVR2C",
);

Thanks !

5 Answers

Hello,

There are two three types of arrays you need to worry about and that is: $_SESSION, $_POST, and $_GET. $_SESSION works only if you start a session via session_start();. What ever you put into the session array stays alive throughout all of the scripts until the session sends or if you destroy the piece of information you added. $_POST and $_GET are the same as it will transfer information between two pages. $_POST information gets passed in the background while $_GET gets passed via the URL.

If you have a URL www.domain.com?var1=a&var2=b&var3=c, you will have them available on that page via the $_GET['var1'], $_GET['var2'], and $_GET['var3']. In short, any information that gets passed through the URL can be accessed via the $_GET. You can also pass information from forms by making the form method equal to get: <form method="get">. I hope this helps you and if you need more help don't hesitate to ask.

Cheers!

Hello, if I get It right. The "id" you get on $_GET["id"] will be used to search on the database.

Like:

$product_id = $_GET["id"];
$query = "SELECT * FROM products WHERE id = $product_id"; // ps: just for example purposes, this is not safe, you must clean the input before using it in the query

Hello,

If I remember the lesson (since I did this a long time ago), when you choose a shirt from the listing page shirts.php, you will be sent to the shirt.php page using the id, for the shirt, in the URL: shirt.php?id=101. Then the shirt.php page will grab the id using the method you described.

Cheers!

Thank you! understood!

O, but I thought $_GET only looks for the info from the array? I think Im a little confused about the $_GET function .