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
Cedric Bell
10,713 Pointsshirts.php on Shirts 4 Mike is not working correctly.
After following the instructions for listing the shirts, its not showing up. No images, content, etc..Nothing. Help.. Here is the code:
<?php
$products = array();
$products[101] = array(
"name" => "Logo Shirt, Red",
"img" => "img/shirts/shirt-101.jpg",
"price" => 18
);
$products[102] = array(
"name" => "Mike the Frog Shirt, Black",
"img" => "img/shirts/shirt-102.jpg",
"price" => 20
);
$products[103] = array(
"name" => "Mike the Frog Shirt, Blue",
"img" => "img/shirts/shirt-103.jpg",
"price" => 20
);
$products[104] = array(
"name" => "Logo Shirt, Green",
"img" => "img/shirts/shirt-104.jpg",
"price" => 18);
$products[105] = array(
"name" => "Mike the Frog Shirt, Yellow",
"img" => "img/shirts/shirt-105.jpg",
"price" => 25
);
$products[106] = array(
"name" => "Logo Shirt, Gray",
"img" => "img/shirts/shirt-106.jpg",
"price" => 20
);
$products[107] = array(
"name" => "Logo Shirt, Turquoise",
"img" => "img/shirts/shirt-107.jpg",
"price" => 20
);
$products[108] = array(
"name" => "Logo Shirt, Orange",
"img" => "img/shirts/shirt-108.jpg",
"price" => 25,
);
?><?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) {
echo "<li>";
echo '<a href="#">';
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') ?>
3 Answers
León Nicolás Acosta
728 PointsHi Cedric, I dont know anything of php but i think you could write a not necessary comma in the last instance of your shirt list.. Look:
"price" => 25, );
After 25.
I hope you can solve it!
Best regards! León
Leonardo Hernandez
13,798 PointsThis code is OK. I think the problem may have to do with something else.. You're on your local machine right? local host?
Double check that you are viewing localhost/PROJECT_FOLDER_NAME/shirts.php in your browser address bar.
I am going out on a limb to say that the problem has to do with your project's directory and/or file names.
Leonardo Hernandez
13,798 PointsAre you getting a php error at the top of the page?
Cedric Bell
10,713 PointsHi Leonardo....So yes I am on my local machine...and I did check the address bar its reading: localhost/shirts.php only...No type of error..When I did input path in between localhost & shirts.php, I received a error..Ugghh! I love the challenge though..lol
Nathaniel Blackburn
957 PointsI have refactored (cleaned up) your code and tested this, I hope this will help...
<?php
// Include the header
include('inc/header.php');
// Declare your variables
$pageTitle = 'Mike\'s Full Catalog of Shirts';
$section = 'shirts';
// Define your products
$products = array(
'101' => array(
'name' => 'Logo Shirt, Red',
'img' => 'img/shirts/shirt-101.jpg',
'price' => 18
),
'102' => array(
'name' => 'Mike the Frog Shirt, Black',
'img' => 'img/shirts/shirt-102.jpg',
'price' => 20
),
'103' => array(
'name' => 'Mike the Frog Shirt, Blue',
'img' => 'img/shirts/shirt-103.jpg',
'price' => 20
),
'104' => array(
'name' => 'Logo Shirt, Green',
'img' => 'img/shirts/shirt-104.jpg',
'price' => 18
),
'105' => array(
'name' => 'Mike the Frog Shirt, Yellow',
'img' => 'img/shirts/shirt-105.jpg',
'price' => 25
),
'106' => array(
'name' => 'Logo Shirt, Gray',
'img' => 'img/shirts/shirt-106.jpg',
'price' => 20
),
'107' => array(
'name' => 'Logo Shirt, Turquoise',
'img' => 'img/shirts/shirt-107.jpg',
'price' => 20
),
'108' => array(
'name' => 'Logo Shirt, Orange',
'img' => 'img/shirts/shirt-108.jpg',
'price' => 25
),
);
?>
<div class='section shirts page'>
<div class='wrapper'>
<!-- Page title -->
<h1><?php echo $pageTitle; ?></h1>
<!-- Loop though your products array -->
<ul class='products <?php echo $section; ?>'>
<?php
/**
* Here you can loop though each of the products using foreach
* I have also added the index of the product incase you need it
*/
foreach($products as $index => $product) {
echo '<li>';
echo '<a href=\'#\'>';
echo '<h3> '. $product['name'] .' </h3>';
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'); ?>
León Nicolás Acosta
728 PointsLeón Nicolás Acosta
728 PointsOhh,, and i think you have relocate your question to "php". Best regards! León
Cedric Bell
10,713 PointsCedric Bell
10,713 PointsI will give it a shot Leon...Thanks for the reply