Forum Topic: Php: Include Function

(1,225 views • 21 replies)

This topic is 1 page long.

<< < > >>
None

DannyIsOnFire

Reply To Post Reply & Quote

Posted at: 8/1/06 05:10 AM

DannyIsOnFire DARK LEVEL 19

Sign-Up: 04/14/05

Posts: 7,210

PHP: Main

In this tutorial you will be learning about the include function.
The include function is useful, if you have a common file that you want to use on a lot of pages.
Lets say you want to create some navigation links for your site, and use them on all of the pages of your site.
You could write out the HTML, and then copy it into every page, or you could use an unclude function.
This will save you a lot of time if you want to edit your navigation links, as you will only need to edit one file.

So for this example we will stick with the idea of navigation links.
First things first, create a page called navigation.html and in it, type out your links. Make sure you just type out the links, and dont put the <html> tags or <body> tags etc.

{a href="http://www.yoursite.com/index.php"}H
ome{/a}
{a href="http://www.yoursite.com/flash.php"}F
lash{/a}
{a href="http://www.yoursite.com/contact.php"
}Contact{/a}

Because of the BBS HTML formatting, you will need to replace { and } with < and >.

Now you have your navigation.html page with your links in.
Next go to index.php, and where you want the navigation links to be, put the following code:

<?php include("navigation.html"); ?>

Lets break the code down.

<?php - This tells the browser that where going to run a PHP script.
include("navigation.html"); - This tells the browser to display whatever data is in the file navigation.html.
?> - This closes the PHP script.

If you now check your index.php page, you should see your naigation links.
If you dont, it may be because you have placed the files in diferent directorys, or spelt something wrong.

The include function is a very simple, yet very effective peice of code.
Think of how much time you would have wasted if you had simply copied and pasted the navigation links into 30 pages, and then decided you wanted to add another link.
The include function allowd you to edit only 1 file, instead of 30.

Thats the end of the tutorial.
Im aware that its pretty simple, but people starting out with PHP will hopefully find it useful and time saving.

Any problems, tell me about them.

Brought to you by DannyIsOnFire.
www.dannyisonfire.com


None

WoogieNoogie

Reply To Post Reply & Quote

Posted at: 8/1/06 05:24 AM

WoogieNoogie LIGHT LEVEL 14

Sign-Up: 06/26/05

Posts: 3,284

For future reference, try typing this out next time :P

<A HREF="http://www.site.com">Linkage</A>

Just put the brackets inbetween I tags like so...

<I><</I>


None

SpamBurger

Reply To Post Reply & Quote

Posted at: 8/1/06 10:52 AM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

It was ok, but you could have talked about require(), require_once(), and include_once(). You could also have mentioned that include can be used as a language construct such as :

include 'file';

instead of a function such as :

include('file');

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

DannyIsOnFire

Reply To Post Reply & Quote

Posted at: 8/1/06 12:52 PM

DannyIsOnFire DARK LEVEL 19

Sign-Up: 04/14/05

Posts: 7,210

At 8/1/06 10:52 AM, SpamBurger wrote: It was ok, but you could have talked about require(), require_once(), and include_once(). You could also have mentioned that include can be used as a language construct such as :

include 'file';

instead of a function such as :

include('file');

Thanks, i was going to do require(), require_once(), and include_once, but i got lazy and went out into the "real world" instead =)


None

thecoshman

Reply To Post Reply & Quote

Posted at: 8/7/06 03:14 PM

thecoshman DARK LEVEL 11

Sign-Up: 06/11/06

Posts: 812

ok so I got two problems.

I presume that you menu file could infact be a .txt, as all it the include function dose is put that code in to the file as if it was always there.

secondly, I can get this to work for a menu file in the same folder as the page trying t load it, but if the page trying to load it is in a folder, how do i get it to link to the menu file.

I have treid the direct link, just putting the file name, and thte file name with a '\' infront of it. I just cant't get it to work.


None

DFox

Reply To Post Reply & Quote

Posted at: 8/7/06 03:16 PM

DFox LIGHT LEVEL 30

Sign-Up: 08/09/03

Posts: 9,474

At 8/7/06 03:14 PM, thecoshman wrote: I have treid the direct link, just putting the file name, and thte file name with a '\' infront of it. I just cant't get it to work.

If I understand what you're asking, which I might not, it would be like this:

include 'folder/file.txt';


None

Mister-Mind

Reply To Post Reply & Quote

Posted at: 8/7/06 03:19 PM

Mister-Mind EVIL LEVEL 07

Sign-Up: 07/01/06

Posts: 2,196


None

thecoshman

Reply To Post Reply & Quote

Posted at: 8/7/06 03:27 PM

thecoshman DARK LEVEL 11

Sign-Up: 06/11/06

Posts: 812

At 8/7/06 03:16 PM, DFox2 wrote:
At 8/7/06 03:14 PM, thecoshman wrote: I have treid the direct link, just putting the file name, and thte file name with a '\' infront of it. I just cant't get it to work.
If I understand what you're asking, which I might not, it would be like this:

include 'folder/file.txt';

ok i try to explain it with ascii art!?!?!

I can get it to work when you have your menu.html and homepage.php together, like so

-----------------------------
- Menu.html
- homePage.php
-----------------------------

but when i have to homepage.php in another folder like so, I cant get it to work

-----------------------------
- Menu.html
- -----------------------------
- - homePage.php
- -----------------------------
-----------------------------

I hope this helps clear up what i am trying to get at. I just cant work out how the

<?php
include('gar?')
?>

bit should go?


None

DFox

Reply To Post Reply & Quote

Posted at: 8/7/06 03:30 PM

DFox LIGHT LEVEL 30

Sign-Up: 08/09/03

Posts: 9,474

Ah, I think I see.

So you mean you want to drop back one directory, correct?

That would be:

<?php
include '../file.html';
?>


None

thecoshman

Reply To Post Reply & Quote

Posted at: 8/7/06 03:31 PM

thecoshman DARK LEVEL 11

Sign-Up: 06/11/06

Posts: 812

yes that is exaclty what im trying to do, now that you say it that way, it seems so obvius.

What if i wanted to drop back to the 'root' of my site, would i have to kepp going ..\..\.. etc, or dose that drop back to the start alredy?


None

DFox

Reply To Post Reply & Quote

Posted at: 8/7/06 03:32 PM

DFox LIGHT LEVEL 30

Sign-Up: 08/09/03

Posts: 9,474

At 8/7/06 03:31 PM, thecoshman wrote: yes that is exaclty what im trying to do, now that you say it that way, it seems so obvius.

What if i wanted to drop back to the 'root' of my site, would i have to kepp going ..\..\.. etc, or dose that drop back to the start alredy?

With ../ it takes you back one directory. So ../../ would take you back two. ./ would take you back to the root.


None

thecoshman

Reply To Post Reply & Quote

Posted at: 8/7/06 03:35 PM

thecoshman DARK LEVEL 11

Sign-Up: 06/11/06

Posts: 812

ok that dont seem to work for me.

if i use ../../menu.html i can get t to work with a file two folders deep, but ./menu.html dont work.


None

DFox

Reply To Post Reply & Quote

Posted at: 8/7/06 03:37 PM

DFox LIGHT LEVEL 30

Sign-Up: 08/09/03

Posts: 9,474

At 8/7/06 03:35 PM, thecoshman wrote: ok that dont seem to work for me.

if i use ../../menu.html i can get t to work with a file two folders deep, but ./menu.html dont work.

Well it depends on your directory structure. ./ can be taking you higher. But using ../../ is fine, but it could get kind of messy, which is why it's good to use the absolute path sometimes (that would require ./) rather than the relative path mean ../ which is relative to the file.


None

thecoshman

Reply To Post Reply & Quote

Posted at: 8/7/06 03:53 PM

thecoshman DARK LEVEL 11

Sign-Up: 06/11/06

Posts: 812

At 8/7/06 03:37 PM, DFox2 wrote:
At 8/7/06 03:35 PM, thecoshman wrote: ok that dont seem to work for me.

if i use ../../menu.html i can get t to work with a file two folders deep, but ./menu.html dont work.
Well it depends on your directory structure. ./ can be taking you higher. But using ../../ is fine, but it could get kind of messy, which is why it's good to use the absolute path sometimes (that would require ./) rather than the relative path mean ../ which is relative to the file.

well, the host i have, when i upload files using ftp, i upload them to a folder htdocs, but if i want to link to a file i leave that folder out.

eg, I have my index file in the htdocs folder, where it is ment to be, but the actually url to that would be, mysite.com/index.html, do you think i would need to go

./htdocs/menu.html for it to work? or is it just realy borked (my host that is)


None

DFox

Reply To Post Reply & Quote

Posted at: 8/7/06 03:57 PM

DFox LIGHT LEVEL 30

Sign-Up: 08/09/03

Posts: 9,474

At 8/7/06 03:53 PM, thecoshman wrote: ./htdocs/menu.html for it to work? or is it just realy borked (my host that is)

Yeah, you're going to have to specify the full path.

To see what it is, do this:

Make a new PHP file.

In it, put:
<?php
echo $_SERVER['SCRIPT_FILENAME'];
?>

That will show you the path to use.


None

thecoshman

Reply To Post Reply & Quote

Posted at: 8/7/06 04:01 PM

thecoshman DARK LEVEL 11

Sign-Up: 06/11/06

Posts: 812

ok, so what it returned made nooo sense to me

/var/www/virtual/projectcosh.byethost5.com
/htdocs/test.php

care to fill me in...


None

DFox

Reply To Post Reply & Quote

Posted at: 8/7/06 04:03 PM

DFox LIGHT LEVEL 30

Sign-Up: 08/09/03

Posts: 9,474

<?php
include '/var/www/virtual/projectcosh.byethost5.co
m/htdocs/file.html';
?>

Should work.


None

thecoshman

Reply To Post Reply & Quote

Posted at: 8/7/06 04:29 PM

thecoshman DARK LEVEL 11

Sign-Up: 06/11/06

Posts: 812

bargh, that dont work.

dont worry about it dude, I will just settle for using the ../ thing, it aint perfect but hay.

but yer thanks for getting me going with it!!! you been a grat help. If you want I can put a link to your site to say thanks.


None

DFox

Reply To Post Reply & Quote

Posted at: 8/7/06 04:32 PM

DFox LIGHT LEVEL 30

Sign-Up: 08/09/03

Posts: 9,474

At 8/7/06 04:29 PM, thecoshman wrote: but yer thanks for getting me going with it!!! you been a grat help. If you want I can put a link to your site to say thanks.

Nah, it's fine :) I just enjoy helping people, I don't expect a reward.

Anyway, that path should work. I'm not sure why it isn't :(


None

thecoshman

Reply To Post Reply & Quote

Posted at: 8/7/06 04:35 PM

thecoshman DARK LEVEL 11

Sign-Up: 06/11/06

Posts: 812

is there a way i can set where the 'root' of my site should be, thus letting me use the ./ thing, it would make sense if you can, cose then you could have one folder for content and one folderf for actaully pages.

well may be that aint that great, but can you set the root?


None

DFox

Reply To Post Reply & Quote

Posted at: 8/7/06 04:38 PM

DFox LIGHT LEVEL 30

Sign-Up: 08/09/03

Posts: 9,474

At 8/7/06 04:35 PM, thecoshman wrote: well may be that aint that great, but can you set the root?

I believe you would be able to set it in php.ini.

If you're using shared host the chances are you don't have access to php.ini.


None

Pilot-Doofy

Reply To Post Reply & Quote

Posted at: 8/7/06 06:35 PM

Pilot-Doofy NEUTRAL LEVEL 37

Sign-Up: 09/13/03

Posts: 12,274

At 8/1/06 10:52 AM, BEAR-A-TRON wrote: include can be used as a language construct such as

It's not that it "can be used" as a construct, IT IS a construct. No matter which syntax you use, it's still a construct. Same with echo. Even if I do:

echo ('Hello world');

It still doesn't become a function.


All times are Eastern Standard Time (GMT -5) | Current Time: 08:06 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!