Jump to content

insert results from other php files


Technaut

Recommended Posts

okay this is hard for me but easy for you..

 

i have a main file index.php which has loads of code that I have written, I need the same code to be in loads of other files on the site.

 

The code includes, headers, menus, left column, right column etc. Basically the only differrent part on each of the files will be some content.

 

I was thinking that instead of having one big index.php file and having to copy and paste that code to loads of other files that I could make many small files such as header.php, footer.php, leftcol.php and so on.

 

Then once in each file I could somehow reference leftcol.php and that would always show.

 

The main reason I want to do this is if I change the code for the left column I'd rather just updated a leftcol.php file and this will flow through to all the other files instead of updating every file every time.

 

as I said, easy for you but hard for me :)

Link to comment
Share on other sites

  • 1 month later...

PHP has built in functions to help you in that regard, specifically for that task.

 

 

include, require, include_once, and require_once.  The difference are that require will not allow your program to continue, unless the file actually exists.  Include will try to include the file, but if it does not exist, it will skip it, and continue.  The _once option means that it will include the file only once no matter how many times you call it from other libraries.

 

So...

 

You could say..

 

require("header.php");

require("left_side.php");

require("footer.php");

 

Also, the files you are including could end in .html , they do not have to be .php files, unless you have scripts that need to be processed inside them.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.