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 trialAdam Allen
896 PointsNotice: Undefined variable: products in C:\xampp\htdocs\shirts.php on line 18
I keep getting this error no matter ho wmany times i re-write the code.
Notice: Undefined variable: products in C:\xampp\htdocs\shirts.php on line 18
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\shirts.php on line 18
Here is my code from the shirts.php
<?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">
<!--This will loop round the array elements inproducts and put them into
another variable called product then will execute the block of code
for each individual product-->
<?php foreach($products as $test => $product) {
//makes a lst item in php format
echo "<li>";
//mkae a link with php
//have to use single quotes as there are
//"" in the code
echo '<a href="/shirt.php?id=' . $test .'">';
//Make a img src in php
//reference to the img attribute
// in the array
echo '<img src="' . $product["img"] . '" alt="' . $product["name"] . '">';
// html code in php format
echo "<p>View Details</p>";
//closing html tags
echo "</a>";
echo "</li>";
}
?>
</ul>
</div>
</div>
<?php include('inc/footer.php'); ?>
Help please! thank you!!!
Marc Davison
6,191 PointsSame here, but Cameron Bourquein , your comment made me realize that I probably hadn't hit my best friend (Ctrl+S) on the products file. Did that and everything worked fine.
Thanks!
2 Answers
Daniel Le Maty
754 Pointsi think it should be
<?php foreach($products as $product) {
Shawn Gregory
Courses Plus Student 40,672 PointsHello,
Are these problems occurring when you are just viewing the page with no shirts or when you have chosen a shirt in the shirts page and going to this page. Notices and warnings aren't that bad and does not stop a page from working, it's just warnings you that there might be problems in the future if you don't fix it. You will get these notices and warnings if you go to shirt.php without any shirts chosen as there are no shirts to go through the foreach loop. It's going to happen that way and unless you disable the warnings and notices, you will get those notices and warnings when you come to the page without a shirt chosen. To fix this problem, you can disable the notices and warnings in the php.ini file or by just added a @ in front of those variables as @ is an error suppression symbol. Hope this helps.
Cheers!
Cameron Bourquein
4,927 PointsCameron Bourquein
4,927 PointsI was having exactly the same issue when I navigated from the Home page to the Shirts page. As it turns out I had two copies of "products.php", only one of which had the completed code, and it was not in the "inc" directory, the one without the completed code was. Correcting this fixed that warning.