Forum Moderators: phranque
Currently, the header and navigation menu elements of all my pages are static and only the changing content occurs within the center of the page. If I change any html in the header or menu I have to go back and change it on all pages. Quite a chore.
I know how I could call the header and menu with a Javascript function and create them as seperate .js files, but I'm thinking there has to be a better way.
Any suggestions?
Step 1: generate an "array" file (i.e. a separate PHP file which contains an associative array with all your filenames). Like this:
<?php
$myarray = array(
"one" => array("pageone.php"),
"two" => array("pagetwo.php")
);
?>
Step 2: Create your index.php file, which contains all of your static elements (company logo, navigation menu, footer, ...). The part which should be dynamic can then be filled in using the array and using the URL of the requested page.
For example, in this case your navigation menu should contain links towards "one" and "two", using these URLs: "index.php?cont=one" and URLs: "index.php?cont=two" (note that you can replace this "cont" thing with a variable name of your choice). When the user clicks "one", the page "index.php?cont=one" will be loaded. So he will get your index page, containing all static info, but you can then have PHP replace all dynamic info by using the URL. The variable "cont" you see in the URL will automatically be available in PHP, so you can put the following code in your index page where the dynamic info should come:
<?php
if(isset($_GET["cont"]{
require("arrayfile.php");
include($myarray[$cont][0]);
}
else{
include("main.php");
}
?>
Here's what happens:
1. The user enters your site. the URL is simply index.php, so no $_GET["cont"] variable is set. The "main.php" page will therefore be inserted where you put the above code.
2. The user clicks on the "one" link, which loads "index.php?cont=1". In this case, $_GET["cont"] is set ($_GET variables are taken from the URL), so the file "arrayfile.php", containing $myarray will be loaded, making $myarray available for use. Then, the script looks includes the file that corresponds to this:
include($myarray[$cont][0]);
In this case, $cont is set to "one", so he will look for $myarray["one"][0]); In arrays, the first element is accessed with the number "0", so the first element of the "one" array is included, which is the pageone.php file.
If you're not familiar with PHP, this may look a bit complicated, but it really isn't.
You can also see now that you can dynamically insert any page item you want, based on the URL, including the page's title and meta tags.
Hope this helps. :)
2. The user clicks on the "one" link, which loads "index.php?cont=1". In this case, $_GET["cont"] is set ($_GET variables are taken from the URL), so the file "arrayfile.php", containing $myarray will be loaded, making $myarray available for use. Then, the script looks includes the file that corresponds to this:
and it should be this:
2. The user clicks on the "one" link, which loads "index.php?cont=one". In this case, $_GET["cont"] is set ($_GET variables are taken from the URL), so the file "arrayfile.php", containing $myarray will be loaded, making $myarray available for use. Then, the script looks includes the file that corresponds to this:
;)
I used to have a static setup just as you describe, and initially converted to using includes by using SSI (Server Side Includes) and having my Apache server parse all htm documents as SSI (you could just as easily name the pages .shtm or .stm, instead.)
It was a relatively easy transition from there to doing PHP pages with PHP includes. I just use mod rewrite to rewrite requests for .htm or .html files to .php, *if* no .htm or .html file exists. (Saves me some broken links from related niche sites that are no longer updated, still linking to my now non-existant .htm/l files, but still serving up some visitors my way.)
Lots of ways to do it... You can even do it strictly in Dreamweaver using templates, though the projects I tried that on didn't convert me to it (others I worked with dug it, though.)
<?php include ('http://mydomain.com/includes/global_nav.inc');?>
Just wanted to point out that while Shannon's method will work famously, using the 'http' ends up throwing off web stats because it's your own domain sending an HTTP request for the include file. (server thinks it's an outside visitor, so you get a 'hit' for every time the include file is accessed).
Best way is to use a local path like so:
<?php include ('/home/www/yourdomain/includes/global_nav.inc');?> (or whatever the path to the file is)
Same deal if on windows, just replace *nix path with windows path to the file.
Just two cents :)
Robin
However, I've found Google does a reasonably good job with short query strings and then there's always mod rewrite (on Apache; I'm sure there's an IIS equivalent way to do it) to rewrite the URLs of truly hideous dynamic pages to make them more palatable... just look how successfully Amazon's own pages are indexed, among others. :)
where dynamic URL's was very much disliked by Google. If you have an old site with high pr maybe. I am in the middel of flattening all my sites and it has helped.
There has been much discussion on this very subject here at WebmasterWorld. I have opted to go with dynamically driven website for a recent project. The entire site is based on a few template pages that are reconfigured to fit the content as needed. The site appears as if it were static and looks like it has hundreds of pages of content - when in fact it does not. The only includes I have are for the header and nav areas. The rest of the data lies within a MySQL database. Some of it lives in another website's database. And while I don't have stats on how well the spiders do or don't like it (it hasn't been published yet) I strongly suspect they won't even know the difference.
And if they don't like it for some reason directly related to the fact that it is a dynamic content engine then I believe they will sooner or later. The SEs will recognize this methododology of building and maintaining a website is quite viable and in fact becomes necessary to manage a website with many hundreds of pages of content.
IMHO unless you are abusing the technology in some fashion (or doing something else the SEs don't like), you're website will be found and indexed fine.
<? include("header.php");?> ... and this:
http*://domain.com/script.php?name=value ...are two entirely different things (though often done together). The former appears static to Google, and you can even keep the .htm or .html extensions if you like (as has been mentioned). Only the latter looks dynamic to Google.
I'm 100% positive GG was referring to the latter scenario and not the former.