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 trialErnst van Megen
3,541 Pointsshirts.php is not displaying anything, code-review needed
Hello Coders,
Can you please review my code below? There should be an error somewhere and I can't find it...
<?php
require_once("../inc/config.php");
require_once(ROOT_PATH . "inc/products.php");
?><?php
$pageTitle = "Mike's Full Catalog of Shirts";
$section = "shirts";
include(ROOT_PATH . '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 get_list_view_html($product_id,$product);
}
?>
</ul>
</div>
</div>
<?php include(ROOT_PATH . 'inc/footer.php') ?>
With the following htaccess:
RewriteEngine On
RewriteRule ^shirts/$ shirts/shirts.php
Please note I can not do /shirts/shirts.php because it doesn't work with MAMP.
If I replace the php code with a <h1>test</h1> the header gets displayed. When I add the PHP code nothing happens.
Is this maybe because of ROOT_PATH?
Thanks.
2 Answers
Ernst van Megen
3,541 PointsFinally got it to work!
<?php
define("BASE_URL", "http://localhost:8888/shirts4mike/");
define("ROOT_PATH", $_SERVER["DOCUMENT_ROOT"] . "/shirts4mike/");
// with
<?php
require_once("inc/config.php");
// and
include(ROOT_PATH . "inc/header.php");
Sean T. Unwin
28,690 PointsROOT_PATH should be defined. When using a development server with this project you could define it similar to:
<?php
// this will depend upon drive letter and directory structure
// Notice the escaped backward slash at the end of the string
define( 'ROOT_PATH', 'C:\mampp\htdocs\treehouse\php\shirts4mike\\');
// then you can use:
include( ROOT_PATH . 'inc/header.php' );
?>
If doing this on a production server you can define ROOT_PATH
as 'www.example.com'
.
You could also use __DIR__
. If you do you need to add a slash to the call directory:
<?php
include( __DIR__ . '/inc/header.php' );
?>
Ernst van Megen
3,541 PointsThank you for your suggestions Sean. I've tried both posibilities but failed. BASE_URL is set. Config file is loading. But the BASE_URL is not echoing.
Example:
<?php define ('BASE_URL', "http://localhost:8888/shirts4mike/');
$output = $output . '<img src="' . BASE_URL . $product["img"] . $product["name"] . '">';
// Returns
"img/shirts/shirt-101.jpg"
// Without the BASE_URL
Ken Haskins
299 PointsKen Haskins
299 PointsThanks for sharing how you got this to work. I'd been trying to figure this out ALL DAY!