| | |
Basic PHP Includes
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
Basic Includes
Lets say you have a Web site with 10 or so pages, and you want to update the navigation. You don't want the hassle of updating every single page. That's where includes come in handy.
Your basic include will look like this.
[php]
<?php include ( 'includes/navigation.php' ); ?>[/php]
That's it! What I typically do is design a page as usual, then begin breaking sections up into includes. To use this effectively:
1) Find sections or tables that will repeat throughout the site.
2) Cut the section of code out and paste it into another file.
3) Save this file into an includes folder.
4) Where the original section was, insert your PHP inclue code, referencing the file name.
Let's say I want to use the highlighted code on multiple pages.
[php]
<table>
<tr>
<td>
This table will be on every page.
</td>
</tr>
</table>
<table>
<tr>
<td>
This is some other content.
</td>
</tr>
</table>[/php]
I'll include it like this.
[php]
<?php include ( 'includes/table.php' ); ?>
<table>
<tr>
<td>
This is some other content.
</td>
</tr>
</table> [/php]
Now if I change table.php later, it will change on every page automatically. It's just like using image tags, but instead of image files, you are including pieces of html. This can save you hours and hours.
Alan Hettinger is a freelance designer available for print, web, and multimedia projects.
Lets say you have a Web site with 10 or so pages, and you want to update the navigation. You don't want the hassle of updating every single page. That's where includes come in handy.
Your basic include will look like this.
[php]
<?php include ( 'includes/navigation.php' ); ?>[/php]
That's it! What I typically do is design a page as usual, then begin breaking sections up into includes. To use this effectively:
1) Find sections or tables that will repeat throughout the site.
2) Cut the section of code out and paste it into another file.
3) Save this file into an includes folder.
4) Where the original section was, insert your PHP inclue code, referencing the file name.
Let's say I want to use the highlighted code on multiple pages.
[php]
<table>
<tr>
<td>
This table will be on every page.
</td>
</tr>
</table>
<table>
<tr>
<td>
This is some other content.
</td>
</tr>
</table>[/php]
I'll include it like this.
[php]
<?php include ( 'includes/table.php' ); ?>
<table>
<tr>
<td>
This is some other content.
</td>
</tr>
</table> [/php]
Now if I change table.php later, it will change on every page automatically. It's just like using image tags, but instead of image files, you are including pieces of html. This can save you hours and hours.
Alan Hettinger is a freelance designer available for print, web, and multimedia projects.
Last edited by cscgal; Mar 14th, 2004 at 12:58 pm.
•
•
Join Date: Jan 2008
Posts: 136
Reputation:
Solved Threads: 19
Hi,
This is a topic I am having an issue with presently.
I have separated my HTML code from my PHP code, so that I now have a header, footer and navigation template, which will be used on every webpage. Then the content is the only aspect that will change from page to page (well, and some other parts such as the login/logout box, etc).
The issue I am encountering is that the links in my navigation menu are static and therefore cannot adapt if the menu is included in a page that is a directory deeper in my website. E.g.:
+ public_html
+ - index.php
+ - news
+---+ - index.php
+ - another dir
+---+ - index.php
Therefore, if I include the navigation in the index.php page in the base of the public_html directory, everything works fine. If I however include it in the index.php in the news directory, the links are all one level off.
I've considered two solutions for this, neither of which I like very much. The first is to use absolute file references, so the full path '/website/home/templates/navigation/navigation.tpl', but this could be a problem if I want to use the code somewhere else, where the structure is different. I wouldn't want to have to edit every link.
The other method I considered was to pass a parameter to my templating engine defining the directory structure offset, which is the appended to the beginning of any path before it is used. So for the base directory, this would be either an empty string, '', or '.' . For a directory deeper it would be 'dirname', and a directory higher './../' or '..' .
Again however, this solution looks messy. Can anyone suggest a better solution please?
If you've made it this far, thanks for reading. Please find a beer with your name on it in the bar
I know I need one.
R.
This is a topic I am having an issue with presently.
I have separated my HTML code from my PHP code, so that I now have a header, footer and navigation template, which will be used on every webpage. Then the content is the only aspect that will change from page to page (well, and some other parts such as the login/logout box, etc).
The issue I am encountering is that the links in my navigation menu are static and therefore cannot adapt if the menu is included in a page that is a directory deeper in my website. E.g.:
+ public_html
+ - index.php
+ - news
+---+ - index.php
+ - another dir
+---+ - index.php
Therefore, if I include the navigation in the index.php page in the base of the public_html directory, everything works fine. If I however include it in the index.php in the news directory, the links are all one level off.
I've considered two solutions for this, neither of which I like very much. The first is to use absolute file references, so the full path '/website/home/templates/navigation/navigation.tpl', but this could be a problem if I want to use the code somewhere else, where the structure is different. I wouldn't want to have to edit every link.
The other method I considered was to pass a parameter to my templating engine defining the directory structure offset, which is the appended to the beginning of any path before it is used. So for the base directory, this would be either an empty string, '', or '.' . For a directory deeper it would be 'dirname', and a directory higher './../' or '..' .
Again however, this solution looks messy. Can anyone suggest a better solution please?
If you've made it this far, thanks for reading. Please find a beer with your name on it in the bar
I know I need one.R.
Last edited by robothy; May 10th, 2008 at 7:04 am.
you could have different paths like
PHP Syntax (Toggle Plain Text)
$nav='/website/home/templates/navigation/'; $temp_base='/website/home/templates/'; $base_url='/website/home/'; // /website/home/templates/navigation/navigation.tpl include $nav.'navigation.tpl'; include $temp_base.'navigation/navigation.tpl'; include $$base_url.'templates/navigation/navigation.tpl';
![]() |
Similar Threads
- Dear Sir (Post your Resume)
- PHP Includes Apache 1.3.33 and a Mac (PHP)
- Test For Php Includes (PHP)
- PHP Includes??? (PHP)
- Multiple PHP INCLUDES (PHP)
Other Threads in the PHP Forum
- Previous Thread: if file exists
- Next Thread: Sending Emails
| Thread Tools | Search this Thread |
301 advanced apache api array autosuggest basics beginner broken cakephp class cms code combobox compression cron curl data database date datepart display dropdownlist dynamic email eregi error execution file files folder form forms function functions google head href htaccess html httppost if...loop image include includingmysecondfileinthechain ip javascript job joomla jquery js key library limit link login md5 menu mlm multiple mysql mysql_real_escape_string oop password paypal pdf pdfdownload php phpvotingscript problem procedure query random screen script search searchbox server session sessions smarty source space sql stored system table traffic tutorial unicode upload url variable video volume votedown web website youtube zend






