Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Enhancing a Simple PHP Application Cleaning URLs with Subfolders Introducing Subfolders

shirts.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&rsquo;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

Finally 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"); 
Ken Haskins
Ken Haskins
299 Points

Thanks for sharing how you got this to work. I'd been trying to figure this out ALL DAY!

Sean T. Unwin
Sean T. Unwin
28,660 Points

ROOT_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' );
?>

Thank 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