Page 1 of 1

include_once function

Posted: Thu Dec 08, 2005 5:29 pm
by fagan
I'm prettty new to php. I'm attempting to use include_once to include the header & footer in all files on the site with those files in a folder - /includes. Clever, huh?

Anyway, the header & footer have either graphics in another folder (/images) or links to other pages in other folders. The function works fine, but the graphics do not show, & when I right-click to view the properties of the graphic, it is pointing to the folder where that particular page resides. The same with the links. All I want to do is have 1 header & 1 footer file so that only 1 or 2 files get changed, instead of bunches!

Please, how do I do this?


Here is the include_once example statement: <?php include_once '../../../../includes/_Header.html'; ?>

Here is a portion of the html code of the header:

Code: Select all

<table border="0" cellpadding="0" cellspacing="0">
  <tr> 
    <td
        <p><img src="com.gif" width="468" height="65" border="0" /></a></p>
    </td>
    <td
      <div><img src="web.logo.gif" width="100" height="75" border="0" /></a>
        <table>
          <tr>
            <td>for address/phone number information, pass mouse over the logo.</td>
          </tr>
        </table>
      </div>
    </td>
  </tr>
</table>

Here is the include_once example statement: <?php include_once '../../../../includes/_Footer.html'; ?>

Here is a portion of the html code of the footer:

Code: Select all

<table>
  <tr>
    <td><a href="../index.html">Home</a></td>
    <td><a href="../SiteMap.html">SiteMap</a></td>
    <td><a href="../info/Contacts.html">Contacts</a></td>
Our web site is hosted on a shared server, running apache, so I cannot make changes to php.ini.

This is driving me nuts! Hope I described this clearly. Probably not.

Thanks.

Burrito: Please use

Code: Select all

and

Code: Select all

tags when [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting PHP Code In The Forums[/url][/size]

Posted: Thu Dec 08, 2005 5:43 pm
by Burrito
when you include a file, you're treating it as if it were in the same location (directory-wise) as its parent. Therefore, if you are trying to use images, js sources, css files they must have a path that points (if using a relative path) as if they were on the same page as your parent page.

Posted: Thu Dec 08, 2005 8:36 pm
by fagan
Burrito, you triggered my mind. I can't get over how easy that solution was. Man, do I feel stupid.

Thank you.

Posted: Thu Dec 08, 2005 8:49 pm
by Jenk
think of the include/require functions like copy and paste. :)

Posted: Fri Dec 09, 2005 9:56 am
by pickle
I usually use absolute paths for referencing images - so it doesn't matter where the file is loaded from.

For example:

Code: Select all

<img src = "/images/goofy.jpg">
rather than:

Code: Select all

<img src = "../../images/goofy.jpg">

Posted: Fri Dec 09, 2005 10:20 am
by RobertGonzalez
Something I like to do is at the very beginning of my script declare the path to the current file relative to the home directory:

Code: Select all

<?php
$page_root_path = './'; // Root directory
$page_root_path = './../'; // Nested directory
?>
Then everywhere in your script reference the path var so that way you are always in the right place:

Code: Select all

<?php
echo '<img src="' . $page_root_path . 'images/my_image.jpg" />';
?>
This works great with includes also:

Code: Select all

<?php
include $page_root_path . 'includes/header.php';
?>
Hope it helps.

Posted: Sat Dec 10, 2005 10:09 am
by Jenk
should use a constant for that, and also use the absolute path:

Code: Select all

define('CURRPATH', realpath('.'));

echo '<img src="' . CURRPATH . '/images/blah.jpg"/>';