Jump to content
Larry Ullman's Book Forums

Recommended Posts

Using Dreamweaver CC. Server working.

includes/header.html file works and css file works

Chapter 3 Script 3.6 and 3.8 I had to change (which I expected)

include ('includes/header.html'); to include ('../includes/header.html');

<?php include ('includes/footer.html'); ?> to <?php include ('../includes/footer.html'); ?>

to get the include to work. The style.css does not work. Tried changing the include statement but nothing works.

 

I think it's because there's an include calling an include??? Anyone have a solution?

Yes I know operator error :-)

Link to comment
Share on other sites

Hello, hwadler,

 

Paths are one of the real pains of web programming! Your best bet is to create constants for the different paths you will need for your projects. Let’s say my files are organised like this:

root_folder
    index.php
    includes // I actually use another, less obvious name
        config.inc.php
        header.php
        navigation.php
        footer.php
    css
        styles.css
    images
    js
    pages_folder
        some_page.php

        sub_folder
            some_other_page.php

 I define the following paths :

if ($_SERVER['HTTP_HOST'] == 'localhost:8888') {
    define('LIVE', FALSE);
    define('BDD', '/Users/my_account/htdocs/bdd_connect.php');

    define('BASE', 'http://localhost:8888/root_folder/');
    define('BASE_PAGES', BASE . 'pages_folder/');
    define('BASE_SUB', BASE_PAGES . 'sub_folder/');
} else {
    define('LIVE', TRUE);
    define('BDD', '/home/my_account/bdd_connect.php');

    define('BASE', 'http://www.domain.com/root_folder/'); // this folder
        // is probably called www or public_html
    define('BASE_PAGES', BASE . 'pages_folder/');
    define('BASE_SUB', BASE_PAGES . 'sub_folder/');
}

Then I create constants for the different paths that vary according to the folder I’m in when calling them:

if (dirname($_SERVER['PHP_SELF']) == '/root_folder') {
    define('BASE_CSS', 'css/');
    define('BASE_JS', 'js/');
    define('BASE_IMAGES', 'images/');
    define('BASE_INCLUDES', 'includes/');
} elseif (dirname($_SERVER['PHP_SELF']) == '/root_folder/pages_folder') {
    define('INDEX', '../index.php');
    define('BASE_CSS', '../css/');
    define('BASE_JS', '../js/');
    define('BASE_IMAGES', '../images/');
    define('BASE_INCLUDES', '../includes/');
} elseif (dirname($_SERVER['PHP_SELF']) == '/root_folder/pages_folder/sub_folder') {
    define('INDEX', '../../index.php');
    define('BASE_CSS', '../../css/');
    define('BASE_JS', '../../js/');
    define('BASE_IMAGES', '../../images/');
    define('BASE_INCLUDES', '../../includes/');
} else {
    define('BASE_CSS', NULL);
    define('BASE_JS', NULL);
    define('BASE_IMAGES', NULL);
    define('BASE_INCLUDES', NULL);
}

In the header.php file, I call the CSS files like this :

    <link rel="stylesheet" href="<?php print BASE_CSS . 'styles.css'; ?>">

This will work whether the header.php file is in index.php or in some_page.php or in some_other_page.php.

 

In the different files inside the pages_folder, I call the configuration file like this:

require_once('../includes/config.inc.php');

I call the header, navigation and footer files like this :

include(BASE_INCLUDES.'header.php');
include(BASE_INCLUDES.'navigation.php');
include(BASE_INCLUDES.'footer.php’);

In the different files inside the sub_folder, the call for the configuration file is different since the constants are not defined until the configuration file has been included:

require_once('../../includes/config.inc.php');

On the other hand, the header, navigation and footer files are called everywhere the same way since the constants can be used:

include(BASE_INCLUDES.'header.php');

I’m sure programmers better than I am manage things more efficiently, but this has solved all my problems with paths !

 

I hope this helps,

 

Emilie

  • Upvote 2
Link to comment
Share on other sites

Emilie, I think your advice is really good, but there are a couple of things I like to do differently, so I will mention those things here just to provide another perspective.

 

For one, I don't like to check $_SERVER properties to try to programmatically figure out which environment I'm in. I've seen that go wrong so many times, and there always seems to be something that you cannot control / account for that causes the whole thing to break.

As such, I generally define a $prod variable and set it to true in production and false in my dev environment.

Furthermore, you can put the $prod variable in a separate file and include it in your config file so that you don't have to edit your config file every time you move it from dev to production.

Basically, you create the prod.php file once for each environment and never touch it again after that, and it just works... always.

 

Also, I much prefer absolute paths for includes over relative paths. While absolute paths require a bit more typing (most of which is just copy-and-paste), they are, by their very nature, absolute, and not subject to change. Again, I think they provide a more rigid structure in a place where it's good to have that rigid structure.

By using absolute paths, you can define everything once for each environment and be done with it.

 

Of course, I understand the benefits of relative paths, etc., but after having worked on many projects, I have found that absolute paths almost always work better.

Link to comment
Share on other sites

 

I generally define a $prod variable and set it to true in production and false in my dev environment.

Thanks for the tip. I’ll try this in my own projects!

 

 

I much prefer absolute paths for includes over relative paths.

In theory, I agree. But I don’t know why, absolute paths usually don’t work for me for URIs. Absolute URLs work fine, absolute URIs don’t, and I really can’t understand why.

Link to comment
Share on other sites

I had a big issue with absolute URIs for the longest time too. For whatever reason, they can be a royal pain to fully understand, even though they aren't that hard to understand conceptually. In other words, I feel your pain.

 

Taking the time to understand them so that they can be properly utilized is quite beneficial though, I think.

Link to comment
Share on other sites

  • 2 weeks later...

Hope you all don't mind me jumping in.  I'm an absolute newbie to web programming and PHP. 

 

I'm having a problem getting the calculator program to work (3.5).  I inserted the code directly into NetBeans 8.0.1 and no go.  Based upon this discussion, I tried using the absolute path and I tried putting the include files in the same directory as the calculator file and commenting out the include.  No success.  The actual calculator portion works but there appears to be an error above.

 

This is what I get for the top of the program:

 

Total Estimated Cost

The total cost of driving ' . $_POST['distance'] . ' miles, averaging ' . $_POST['efficiency'] . ' miles per gallon, and paying an average of $' . $_POST['gallon_price'] . ' per gallon, is $' . number_format ($dollars, 2) . '. If you drive at an average of 65 miles per hour, the trip will take approximately ' . number_format($hours, 2) . ' hours.

'; } else { // Invalid submitted values. echo '

Error!

Please enter a valid distance, price per gallon, and fuel efficiency.

'; } } // End of main submission IF. // Leave the PHP section and create the HTML form: ?>

 

The bottom looks ok, i.e. everything from 'Trip Cost Calculator' and below is as it should be.

 

Trip Cost Calculator

Distance (in miles):

Ave. Price Per Gallon: 3.00 3.50 4.00

Fuel Efficiency:

 

Any help would be greatly appreciated.  I've tried everything I can but without the web knowledge, I'm stuck.

 

Thanks!

 

Nick.

Link to comment
Share on other sites

I thought that at first so loaded it directly into Firefox, i.e. 'File/Open File'.  I've done that often to test outside of NetBeans.  So it isn't NetBeans.  I even opened 'calculator.php' into my other editor 'notepad plus' which I really like on Linux then load into Firefox and still no go.

Link to comment
Share on other sites

What is really interesting is the comments are showing which, at first, indicated there was an open comment so everything was displaying until the next comment closing (multiline) but that wasn't the case either.

 

Best Regards,

 

Nick.

Link to comment
Share on other sites

This should provide additional clarity to what I've done thus far:  First, I'm running Windows Ultimate 64 on an Alienware 18 with 32GB memory.  I'm using XAMPP.

 

For the include statement, I went to windows explorer and copied as text the address line: C:\xampp\htdocs\HTMLtemplate\includes.

I pasted this in and added \header.html.  Result: no change.

 

I reversed the \ to / (UNIX/Linux style).  Result: no change.  That was my absolute address attempt.

 

I tried (based upon the start of the thread) './' before the includes hoping that ./ which = current directory would work. Result: no change.

 

I went into the 'header.html' and instead of loading it, copied the contents right into the file and modified calculator accordingly.  If this worked then it wasn't the header.html file (working to identify what isn't the problem).  Result: some change.  Question - in the beginning of chapter 3, header.html is created and part of that header contains the five buttons (Home Page, Calculator, etc.) plus 'Your Website', etc.  None of the above is shown in what should be the output, i.e. 'Total Estimated Cost' and 'Trip Cost Calculator'.  I've gone over the book from the beginning of chapter 3 up to the displays and nothing says (or indicates) that what is shown is a portion of what is actually seen. Ergo, I'm expecting the output to be exactly as shown in the book.  If that is correct, the header code is incorrect, i.e. more of the header should have been removed. But no one else is experiencing this and the book is 3 years old and the errata sheet indicates nothing so I can only conclude that the header code is correct and something else is the issue.

 

My apologies if I'm over thinking this - because the code should work, I'm starting to go a bit batty here.

 

Again, in advance, thanks!

 

Nick.

Link to comment
Share on other sites

I thought that at first so loaded it directly into Firefox, i.e. 'File/Open File'.

Hello, Nick,

 

This is exactly what you should NOT do in order to run a PHP file. Firefox is just a browser. It knows how to open and display the contents of HTML files, but on its own it doesn’t know what to do with a PHP file. For Firefox, a PHP file is just a text file, or a HTML file if your PHP is inside a HTML file, which is why it displays everything in this file, including your PHP code.

 

For your PHP code to be run as such, the PHP file must be sent to your local server (XAMPP), where the PHP interpreter will parse the code and send it back to Firefox (as HTML), for Firefox to display it.

 

Which is why Larry told you :

 

That would suggest that Netbeans isn't executing your PHP, probably because it's not being run through a URL.

 

This means you cannot just use File/Open in Firefox. If you do that, the address will begin with : file:/// This is the path on your computer. It tells Firefox where the file is, it doesn’t tell it that this file should be sent to XAMPP and the PHP interpreter for processing. For this to happen, the address in the browser must begin with http://. If you kept your htdocs folder inside the xampp folder (the default place for it), the address in the browser will probably begin with: http://localhost/xampp/. For instance, the "Welcome" file to XAMPP runs at this address : http://localhost/xampp/index.php. The index.php file is just inside your htdocs folder. Try to open this file using File/Open in Firefox (the path is probably: file:///C:/xampp/htdocs/index.php, if your xampp folder is right at the first level inside C:)… and see what happens ! Don’t worry. Your XAMPP installation is probably just fine. The problem is only that you opened the PHP file instead of running it through the PHP interpreter.

 

Try and read the beginning of chapter 1 again. Larry explains it very clearly… although I know it took me a long time before really understanding how things work between the local server, the PHP interpreter and the browser.

 

I hope this helps,

 

Emilie

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...