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

PHP

Using Root-Relative Web Addresses

I am following this video, 'Using Root-Relative Web Addresses'

My directories and files are currently like this

[shirts/receipt/index.php]

<?php
$pageTitle = "Thank you for your order!";
$section = "none";
require_once("../inc/header.php"); ?>

<div class="section page">
  <div class="wrapper">
  <h1>Thank you!</h1>
  <p>Thank you for your payment. Your transaction has been completed, and a receipt for your purchase has been emailed to you. You may log into your account at <a href="www.paypal.com/us" target="_blank" /> www.paypal.com/us </a> to view details of this transaction.</p>
<p>Need another shirt already? Visit the <a href="../shirts.php">Shirts Listing</a> page again.</p>
</div>
</div>

<?php include("../inc/footer.php"); ?>

[shirts/inc/header.php]

<html>
<head>
<title><?php echo $page_title; ?></title>
<link rel="stylesheet" href="/css/style.css" type="text/css">
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Oswald:400,700" type="text/css">
<link rel="shortcut icon" href="/favicon.ico">
</head>
<body>

<div class="header">

<div class="wrapper">

<h1 class="branding-title"><a href="/">Shirts 4 Mike</a></h1>

<ul class="nav">
<li class="shirts <?php if ($section == "shirts") { echo "on"; } ?>"><a href="/shirts.php">Shirts</a></li>
<li class="contact <?php if ($section == "contact") { echo "on"; } ?>"><a href="/contact.php">Contact</a></li>
<li class="cart"><a target="paypal" href="https://www.paypal.com/cgi-bin/webscr?cmd=_cart&amp;business=6MEZ5YZ2VQHRQ&amp;display=1">Shopping Cart</a></li>
</ul>

</div>

</div>

<div id="content">

The problem is that the receipt index.php cannot load the stylesheet from header.php unless I fix the path as below <link rel="stylesheet" href="../css/style.css" type="text/css"> But this causes the rest of pages unload the stylesheet!

2 Answers

Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,703 Points

can you post a screen shot of your directory layout. If you are using Sublime Text 2 - Use the left panel to expand your directory trees so am able to see the layout or if you list out how your directory is set up. Example:

ROOT_DIR
+contact
+css
 style.css
+img
+inc
header.php
+receipt

http://s11.postimg.org/cmxft3oo3/2013_07_08_2_56_42.png

Please check my directory structure out.

Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,703 Points

Ok, so I went ahead and downloaded the project files for the "Using Root Relative Web Addresses" section and then copy pasted your code in place of those two files you specified (receipt/index.php and the inc/header.php files). On my web server it seemed to load css for every page. Main page, shirts listing page, each individual shirt, and the receipt page. So this makes me think that your ROOT configuration on your web server is possibly one more folder back. This could cause an issue using /shirt.php and /css/style.css because its looking further back than it should.

So one way we can see where your ROOT directory is actually pointing to. We can use a PHP command and then echo it back out. If the ROOT directory is the shirts page. Lets say for example I had shirts as my ROOT then my php code would echo out like this.

C:/wamp/www/shirts//

You are on different OS and your folder structure may be different but it should have /shirts// at the end. If it does not, then shirts is not your ROOT directory.

so try adding this piece of code to your inc/header.php file : BEFORE the CLOSING "HEAD" tag

<?php define("ROOT_PATH",$_SERVER["DOCUMENT_ROOT"] . "/"); ?>

and then add this piece of code to your receipt/index.php file : HARD RETURN after the DIV class "WRAPPER" tag to create a new temporary line for it.

<?php echo ROOT_PATH; ?>

Save, then go back and refresh your receipt/index.php file and see what it returns, it should display plain text up and to the left of the "Thank You!". Let me know what happens and lets go from there.

We could of echo'd this out an easier way, but this is how I did it. So you could follow same steps to prevent from running into a error with php.

Chirs nice explanation.

I have similar problem.

My file in xampp are organize like this xampp/htdocs/root/shirts4mike

How to fix or tell server that my root folder is htdocs/root/shirts4mike? (Not just htdocs by default)