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 trialjlampstack
23,932 PointsWhere is $id pointing to???
I'm sure it was probably defined earlier, but i'm getting more confused and mixed-up as the lessons progress.
Can anyone let me know where this was defined and how it works?
Thank in advance!
3 Answers
Dennis Amiel Domingo
17,764 PointsYou're correct jaycode! Here's my code with some comments (I placed some comments so I would understand and remember the things I wrote here haha):
Here's the $_GET request in the details.php file
//this conditional code block sets an http request variable named "id", then checks if it is "set"
//it then stores it in the $id variable, then checks if the $catalog array has that requested key via $catalog[$id]
//it then stores it in the $item variable
if(isset($_GET["id"])){
$id = $_GET["id"];
if(isset($catalog[$id])){
$item = $catalog[$id];
}
}
Now here's the function that renders the html href link in the functions.php file
//this function takes in parameters, then renders the path to the image of the item from the array
//it also transforms that image into a link
//take note that the "id" part in the href details links to the "id" $_GET variable in details.php
function get_item_html($id, $item){
$output = "<li><a href='details.php?id="
.$id . "'><img src='"
. $item["img"] . "' alt='"
. $item["title"] . "'/>"
. "<p>" . $item["title"] . "</p>"
. "</a></li>";
return $output;
}
Now the $id numbers are the keys of the items in your $catalog array. I created my own items in my database at home. My first item, which is 0, points to the book "Rich Dad Poor Dad" by Robert Kiyosaki.
jlampstack
23,932 PointsI think I found the solution in the next video?
In the Address Bar you might see a URL with something like.... details.php?id=2
The id points to the individual details page.
A URL with an address extension of /details.php?id=15 for example, points to the book 7 Habits of Highly Effective People.
To summarize $id is pointing to the GET variable in the address bar.
Can anyone confirm if this is a correct explanation or if I've let anything out?
Csaba Kapus
Courses Plus Student 6,103 PointsHello jaycode,
I dont really into PHP yet but as far as i can see , based on this video. The $id is pointing to media_id. The media_id looks like an SQL column in the Media table. Since :
$results = $db->query("SELECT media_id, title, category, img FROM Media");