Forum Moderators: coopster
<?php
include 'includes/header.php';
?>
add all of your body text, tables, pictures, etc. here (just regular html. cut and paste from your old page if you like)
<?php
include 'includes/footer.php';
?>
Next, create header.php and footer.php and place them in a folder called "includes". In the header file, add your page title and navigational links. In the footer file, add your copyright notice.
To create new pages keep using the above code.
When you want to update links in the header or the copyright date in the footer, all you have to do is change one file, not the whole site.
include $content;
in the middle. Then you just do this on every page.
$content = "pathtocontentpage.html";
include "template.php";
and voila, the template echo the page set to content.
[edited by: jatar_k at 1:35 am (utc) on Sep. 29, 2003]
<?php
if (!isset($title') )
$title = 'My Web Site';
print '<title>'.$title.'</title>';
?>
Then your not stuck with the same title on all your pages, because you can change it before the include like this..
$title = 'Contact Us';
include_once 'header.php';
I use this technique to change the title, section (h1) and page name (h2) on my pages.
can this type of dynamic generating also be combined with breadcrums
Just so happens that I have just published an automatic breadcrumb generator, see the Bag-O-Tricks II [webmasterworld.com] thread.
Would a MySQL or a flatfile database be the best way to go?
I would go with MySQL - it is well supported and it should always outperform a flat file db.
But don't create this kind of thing from scratch - there are plenty of existing Content Management Systems (CMS) available - many of them are open source and free to use.
--8<--
#!/bin/sh
header='/path/to/header.inc'
footer='/path/to/footer.inc'
echo "Content-Type: text/html"
echo ""
cat $header
cat << EOBODY
##
## --> content here - <--
##
EOBODY
cat $footer
--8<--
Course you will need to keep all your pages in a ScriptAliased directory (or set up your webserver some other way so that it parses the script). Fast though - low overhead!
[edited: forgot the heredoc, duh.]
Would a MySQL or a flatfile database be the best way to go?
If I had to pick though I would take mysql, more power involved. You can use db functionality in concert with of php functionality giving you the best of both worlds. If you are solely serving chunks of content it really doesn't matter too much.
I think that as things get more complex using built in sql functionality can take some of the weight from the scripting language.
it also prevents googlebot from reading just the headers [webmasterworld.com] on your server to save time and bandwidth
i know it is possible with SSI if you use the .htm or .html extension by chmodding each page to 754.
but how does one get around this for a dynamic php site? so that the header returned a correct last modified date according to the content and/or the last update of the header.inc and footer.inc?
ie.
contact.html is the page name and has the actual page content written into it. If the content of the page is updated the last-modified date will change.
contact.html includes the template(s) but the true page is contact.html.
I always use static page names for dynamic sites which is how the bots know about last-modified dates etc. This won't reflect changes to the site wide template but *shrug* there is never a perfect scenario. You could always change all your hard coded headers individually but that requires a lot more time.
A sidenote: Have you ever looked at what comes up in the browser if you directly request a .inc file? It is the code. I only use .inc.php or, for libraries, .lib.php and most of the time just .php and organized into directories that classify the files. This keeps users from being able to see my code.
so, you do what oilman does and use
or if you're on *nix just add AddType application/x-httpd-php .php .htm .html to your htaccess file and parse html and htm files for php and keep your current extensions
so that your html pages are parsed as php?
sorry for simple question, but this thread has got me thinking about an area in which i'd really like to know more
But don't create this kind of thing from scratch - there are plenty of existing Content Management Systems (CMS) available - many of them are open source and free to use.
And then put all content into a database for even more control. I love the concept of dynamic sites.
Say you have multiple paragraphs, headers, etc. How do you put the content into a database and split it up on the page? I understand how to query information from a database, but I just can't see the need for putting the content from each individual website page into a database to pull out~ How does this work?
madcat, I usually will have all the main body content formatted in the db(i.e. <p>s <h2>s, etc..). I will have a field for the title, which I will use for <title> and <h1>.
>>I just can't see the need for putting the content from each individual website page into a database
To me, the useful thing about storing the content in a db is that I only need one file to to display as many pages as I need. I can also change the layout in one shot, too. It keeps the file system nearly empty, whereas if you had a thousand page site it could get confusing.
I usually will have all the main body content formatted in the db(i.e. <p>s <h2>s, etc..). I will have a field for the title, which I will use for <title> and <h1>.
page 1 > company info
page 2 > service info
page 3 > contact info
Each page will have a few paragraphs, and each page will have different titles of course. Not to mention different sized headings. Specifically, what would your fields look like, and how would the content look within its MySQL table cell- for i.e., how would it be formatted?
I'm assuming that you have one .html page that acts as a template pulling in the various elements like menus, footers, logos and headers > I'm still a bit cloudy about the content portion including the <p>'s, <h1>'s, images and other elements that normally go withing that content...how they're laid out in the db.
Any direction is much appreciated ^
In your example case, which seems like a business e-biz-card type site, I wouldn't even go dynamic. But I'm sure you were just using a simple example. The type of site I would build dynamically is one that changes constantly or grows on a regular basis, like an e-commerce or news site.
I'll use another example to better illustrate the benefits. Suppose you sell, well of course, Widgets. You start out selling only green, blue and red widgets. So you set up your database like so:
Database Widgets:
table 1: cats //categories for main nav
Now you can build your main navigation from the database by asking for all listings in cats(SELECT cat_id FROM cats). So when you decide to add purple and teal widgets in the future, all you have to do is add a row for each in the db and all your pages' navigation menus will now show them as well. You will point all the nav links to the same file, with a parameter at the end which will tell the landing page what to display(i.e. href="widgets.com/products.php?cat=2")
Now products.php will get the variable($cat) you sent along in the url and query the db for any product that belong in that category. Personally, I use a combination of cat_name and product_name to build my <title> and <h1> tags, but you could easily add more fields to the db if needed.
So once you get to the main content, you just display the product_description field, which would be a text field and would have all formatting already done.
<p>beautiful green widgets for sale...featuring</p>
<ul>
<li>blah</li>
<li>super blah</li>
<li>and more!</li>
</ul>
<p>Get them while they're hot!</p>
Also, I name the associated image the same as the product_id, so I know I can reference it with the same variable I used to call the content.
Well, I need to go to bed...it's midnight here. I hope that made some sort of sense :) Also keep in mind that I'm no guru over here, so there may be flaws in my technique but maybe it'll help you picture the concept better.
Cheers,
Birdman