...

View Full Version : php include



lil9
06-08-2005, 05:43 PM
hi all,

im looking for a bit of php help with the include thing. im making a site which is going to have about 20+ pages and the menu/navigation will be exactly the same on each page.. but i dont want to have to edit every single page just to change 1link in the menu.

Ive been reading up on how i could do this and ive worked out how to do it partly.

i use this for the index page (http://www.lil9.com) ,

<?PHP
include("menu.htm");
?>
and that seems to work just fine.

But when i try to add the menu to a page in another directorie, for example, this page (http://www.lil9.com/generals/resolution1379.php), it just gives me aload of errors.

could any body tell me how to fix this please, thanks
steve

mattyod
06-08-2005, 05:57 PM
I'm guessing that your include is not pointing to the correct directory.

You need to do something like



<?
include ("../../menu.htm")
?>


or use an absolute reference such as



<?
include ("http://www.lil9.com/menu.htm")
?>


that should fix it.

lil9
06-08-2005, 06:17 PM
hmm neither of those made any difference :(

any other ideas?

gsnedders
06-08-2005, 06:55 PM
@readfile('menu.htm');

delinear
06-08-2005, 07:58 PM
Try it this way:


include('/home/lil9/public_html/menu.htm');

lil9
06-08-2005, 08:25 PM
nope still no luck sadly :(

i tried both those but it just brings up errors again.

does the menu HAVE to be in the same folder as the page that i want it to display on?

delinear
06-08-2005, 09:19 PM
Nope, if you use the format I gave you then you can have the menu file anywhere you like and just point to it from the various other directories. The tough part is that servers can be configured differently to have different paths and you've gotta figure out the right path for your server.

Try this method, create a php file with the following content, nothing else:


<?php
echo realpath('.');
?>

Put this file in the same directory as your menu file then go to the php file in your browser. It will tell you exactly the server path to the directory in which your menu file resides. Then just use that in your include statement using the format I gave you above. For instance, if the path says the following:


/usr/home/lil9/public_html

Then you would create an include like this:


include('/usr/home/lil9/public_html/menu.htm');

JamieR
06-08-2005, 09:50 PM
That's a bit on the weird side. An absolute url in an include has always worked for me, however I'm thinking that it must be down to the way that this guy's server is set up :p

CrAzY_J
06-08-2005, 10:32 PM
Is it in a subdirectory from the page you want to include?
if so:

<?php
include('/directory_name/file.php');
?>


if it's one directory up:

<?php
include('../file.php');
?>



Also, dont use a absolute url to much.
you can use $_SERVER['DOCUMENT_ROOT']

everah
06-08-2005, 10:47 PM
nope still no luck sadly :(

i tried both those but it just brings up errors again.

does the menu HAVE to be in the same folder as the page that i want it to display on?

No, your include files can be anywhere in your directory tree. You just have to know how to reference them.

This is just an example of how you could set your directory structure up, off the root folder:

ROOT
- /js - Javascripts
- /images - Images
- /includes - For all included files
- /etc, etc, etc - You get the picture.

As you develop your pages, call your included file based on where the calling file is and where the included file is. For example, lets says you are in a folder called /test and you are including a file from /includes. I would do something along the lines of this...


// I need to go down a folder to get to root
$page_root_path = "./../";

// Now I need to grab a file to include from the includes folder
include_once($page_root_path . "includes/file_to_included.php");


This should work just about anyhere in your scripts as long as you tell the $page_root_path var where you are in relationship to the root folder.

lil9
06-09-2005, 09:46 AM
Sounds good, il give that a try :thumbsup:

delinear
06-09-2005, 10:04 AM
That's a bit on the weird side. An absolute url in an include has always worked for me, however I'm thinking that it must be down to the way that this guy's server is set up :p

Most likely URL fopen wrappers are disabled in their PHP config. By default they're enabled so url formatted includes work but it's good practice not to use them if you want the code to be fully portable, an absolute server path works just as well and isn't config dependent :)

lil9
06-09-2005, 10:47 AM
thanks for all your help guys, its working now! :D


thanks again



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum