One of the main problems faced by webmasters is saving time when it comes to creating websites. When starting a website, one may choose to create a navigation menu like the following to link to each page:
Which looks like: HomeAbout UsLinksContact UsCode:<a href="http://www.yoursite.com/index.htm">Home</a> - <a href="http://www.yoursite.com/about.htm">About Us</a> - <a href="http://www.yoursite.com/links.htm">Links</a> - <a href="http://www.yoursite.com/contact.htm">Contact Us</a>
So now, you have your 4 pages - and all is well. But, as the site expands you need to integrate a forum - which leaves you with 4 pages to update. No problem at all... But supposing the site had 100+ pages! That would be a very monotonous time consuming job.
This is where we bring in the php include command to really speed things up!
Create just the navigation menu on its own, and rename all of your pages to .php
In this case, the navigation menu will look like this:
Which looks like: HomeAbout UsLinksContact UsCode:<a href="http://www.yoursite.com/index.php">Home</a> - <a href="http://www.yoursite.com/about.php">About Us</a> - <a href="http://www.yoursite.com/links.php">Links</a> - <a href="http://www.yoursite.com/contact.php">Contact Us</a>
Save this file as "menu.inc" as we will be including it elsewhere - hence the .inc extension.
Now, open up your .php pages that the menu links to, and simply add the following code at the very top, to include the navigational menu on the pages:
The code is telling the php page, to "include" the menu.inc file - and it does!Code:<?php include("menu.inc"); ?>
Once this has been added, any changes in the menu.inc file are reflected across the whole site making updating a lot easier!
Why stop at menus? This can be utilised for adverts, footers, headers, templates and much much more!
I includes like this to do most of my PHP work. I don't see a point typing the same info over again and if you want to change it you would have to change it on each page. With include you change once.
Exactly the reason why I started using PHP! Of course once I discovered the powerful stuff it could do I never look back, I now build all websites in PHP.Originally Posted by Lop
Hope some strictly HTML users find this tutorial useful!
This is some PHP that I do know, I use the php include function on all of my pages. I have a global header and global footer, so I have to use it at least twice on all pages (but on some pages I have a third one).
PHP has a lot of great functions, but I haven't heard of a PHP include one before. But then again, I know very little about PHP.
I find this really helpful, does this obey the CSS or just show as default ?
can I put it inside a <div> ?
all php working?
I have recently started learning PHP again (stopped due to busy schedule) and from what I've understood, using .inc extension might not be the wisest thing to do. Using it for simple HTML files is ok, but if beginners forget that .inc's source code can be read by anyone, then they might include files that store very valuable PHP information. (password for database for instance)
So my suggestion to beginners is to create a new folder in your main path called "includes" and keep every file you include there. then just link it properly. (e.g. <?php include("includes/config.php"); ?>)
But since you're talking about creating the website layout, then you can also use .html extension. e.g. header.html, footer.html, menu.html etc.
Yes, it is often standard practise to add the .inc filetype to be forced through the PHP interpreter, however people often forget to do this or do not realize to do this after moving the script.
Be sure to read the updated FAQ || Health is achieved through 10,000 different steps.
A textual description can be only part of your question, be sure to provide sample results, errors and your platform in the appropriate forums while asking.
It's also wise to consider smarty for something like this, it's great for publicly released content too.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks