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 trialJose Mora Hernandez
2,909 PointsUndefined index: 102 in C:\xampp\htdocs\shirt.php on line 5
I'm not sure what I'm doing wrong. Can anyone tell me what does that message mean? this is my code
<?php include("inc/products.php");
$product_id = $_GET["id"];
$product = $products[$product_id];
?><pre><?php
echo $product_id . "\n";
var_dump($product);
?></pre>
5 Answers
Jose Mora Hernandez
2,909 Pointswhat do you mean? why am I posting the codes? to seek some help from you guys if possible maybe that way the error message will disappear.
David Omar
5,676 PointsUndefined Index means your trying to access the array index that does not exist.
$product = $products[$product_id];
In this line your are assigning the variable $product to the value in the $products array with index of $product_id
Andreas cormack
Python Web Development Techdegree Graduate 33,011 Pointsits looking for an index that is not there. in your query string is the ID in caps can you should what your query string look like?? apart from that cannot see anything wrong with your code.
Jose Mora Hernandez
2,909 Pointseverything seems to be lowercase so i think I'm good in that part.
Jose Mora Hernandez
2,909 PointsWell this is my products.php coding page
<?php
$products = array();
$products [101] = array(
"name" => "Logo Shirt, Red",
"img" => "img/shirts/shirt-101.jpg",
"price" => 18,
"paypal"=> "XTP4CGPD626RN"
);
$products[102] = array(
"name" => "Mike the Frog Shirt, Black",
"img" => "img/shirts/shirt-102.jpg",
"price" => 20,
"payal"=> "CCDN6PD5PH7DJ"
);
$products[103] = array(
"name" => "Mike the Frog Shirt, Blue",
"img" => "img/shirts/shirt-103.jpg",
"price" => 20,
"paypal"=> "CT9LBHSJDL7MS"
);
$products[104] = array(
"name" => "Logo Shirt, Green",
"img" => "img/shirts/shirt-104.jpg",
"price" => 18,
"paypal"=> "4YPMZA4YC49S2"
);
$products[105] = array(
"name" => "Mike the Frog Shirt, Yellow",
"img" => "img/shirts/shirt-105.jpg",
"price" => 25,
"paypal"=> "T25RNN786DM46"
);
$products[106] = array(
"name" => "Logo Shirt, Gray",
"img" => "img/shirts/shirt-106.jpg",
"price" => 20,
"paypal"=> "2LCZD8F8TX7TY"
);
$products[107] = array(
"name" => "Logo Shirt, Turquoise",
"img" => "img/shirts/shirt-107.jpg",
"price" => 20,
"paypal"=>"7UCD7CUNU8XDN"
);
$products[108] = array(
"name" => "Logo Shirt, Orange",
"img" => "img/shirts/shirt-108.jpg",
"price" => 25,
"paypal"=>"VHX8VJXEAGGK8"
);
?>
Im also posting my shirts.php code as well I know is a lot to go through but man I'm breaking my head at the moment.
<?php include("inc/products.php"); ?><?php
$pageTitle = "Mike's Full Catalog of Shirts";
$section = "shirts";
include('inc/header.php'); ?>
<div class="section shirts page">
<div class="wrapper">
<h1>Mike’s Full Catalog Of Shirts</h1>
<ul class="products">
<?php foreach($products as $product_id=> $product) {
echo "<li>";
echo '<a href="shirt.php? id= ' . $product_id . '">';
echo '<img src="' . $product["img"] . '" alt="' . $product["name"] . '">';
echo "<p>View Details</p>";
echo "</a>";
echo "</li>";
}
?>
</ul>
</div>
</div>
<?php include('inc/footer.php') ?>
David Omar
5,676 PointsI don't get it what are you trying to do with this?
Jose Mora Hernandez
2,909 PointsYeah a little bit more clear now unfortunately though, on the video tutorials I don't think he has gone over that element yet.
David Omar
5,676 PointsDavid Omar
5,676 PointsOk I see your problem,
you are getting a error because you loaded up shirt.php but no $_GET variable was set. There is a function in php called isset to check whether its set or not
$_GET variables are set through the url bar, so to set $_GET['id'], the url has to be shirt.php/?id=107
Any clearer now?