include_once function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fagan
Forum Newbie
Posts: 4
Joined: Thu Apr 15, 2004 3:18 pm

include_once function

Post 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]
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
fagan
Forum Newbie
Posts: 4
Joined: Thu Apr 15, 2004 3:18 pm

Post by fagan »

Burrito, you triggered my mind. I can't get over how easy that solution was. Man, do I feel stupid.

Thank you.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

think of the include/require functions like copy and paste. :)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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">
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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"/>';
Post Reply