homepage Welcome to WebmasterWorld Guest from 54.161.147.106
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
A dynamic site in 2 minutes
no special skills required
dvduval




msg:1299267
 9:53 pm on Apr 17, 2003 (gmt 0)

For those who want a dynamic site, start by changing index.htm to index.php. Then use this code:

<?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.

 

jatar_k




msg:1299268
 9:59 pm on Apr 17, 2003 (gmt 0)

by the same token you can also put the header and footer code into a single page called template.php with

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]

dvduval




msg:1299269
 10:01 pm on Apr 17, 2003 (gmt 0)

And in ASP...

<%@ Language=VBScript %>
<% Option Explicit %>
<!-- #include virtual="/Includes/header.asp" -->
add your content here
<!-- #include virtual="/Includes/footer.asp" -->

dvduval




msg:1299270
 10:04 pm on Apr 17, 2003 (gmt 0)

Very cool, jatar_k!

Birdman




msg:1299271
 10:05 pm on Apr 17, 2003 (gmt 0)

And then put all content into a database for even more control. I love the concept of dynamic sites.

dvduval




msg:1299272
 10:28 pm on Apr 17, 2003 (gmt 0)

Does anyone know the basic formula for Cold Fusion? In other words can you produce the above example in Cold Fusion?

BTW, I don't see a forum for Cold Fusion.

It would also be cool to see a Java example or any other language for that matter.

marcs




msg:1299273
 10:35 pm on Apr 17, 2003 (gmt 0)

I've been doing something similar (using SSI) for quite some
time. Really speeds up creating new pages.

For those now familiar with doing the same using SSI :

title & meta stuff
<!--#include virtual="header.html" -->
content here
<!--#include virtual="footer.html" -->

oilman




msg:1299274
 10:40 pm on Apr 17, 2003 (gmt 0)

>>For those who want a dynamic site, start by changing index.htm to index.php

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

grahamstewart




msg:1299275
 10:51 pm on Apr 17, 2003 (gmt 0)

Slight modification: if you change your header file so that it has the lines..

<?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.

grahamstewart




msg:1299276
 10:53 pm on Apr 17, 2003 (gmt 0)

AddType application/x-httpd-php .php .htm .html

Be aware that on a very busy site this will cause unneccessary load on the server, as it has to parse all html files before serving them. Even the ones without any php in them.

Probably not a big problem in most situations though.

AmericanBulldog




msg:1299277
 10:58 pm on Apr 17, 2003 (gmt 0)

I am not real familiar with PHP but can this type of dynamic generating also be combined with breadcrums and if so how?

Thanks

Marcia




msg:1299278
 11:49 pm on Apr 17, 2003 (gmt 0)

>then put all content into a database for even more control

Would a MySQL or a flatfile database be the best way to go?

dvduval




msg:1299279
 12:05 am on Apr 18, 2003 (gmt 0)

And...to INCLUDE an example in another language (excuse the pun).

Here is Cold Fusion. Create index.cfm

Contents:

Title and metatags here
<CFINCLUDE TEMPLATE = "../includes/header.htm">
body here
<CFINCLUDE TEMPLATE = "../includes/footer.htm">

dvduval




msg:1299280
 12:16 am on Apr 18, 2003 (gmt 0)

And to finish off the languages, here is java (JSP):

<%@ include file="header.jsp" %>
content goes here
<%@ include file="footer.jsp" %>

grahamstewart




msg:1299281
 12:17 am on Apr 18, 2003 (gmt 0)

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.

korkus2000




msg:1299282
 12:19 am on Apr 18, 2003 (gmt 0)

Includes really are a time saver. If you have the ability you will save so much work.

mischief




msg:1299283
 12:23 am on Apr 18, 2003 (gmt 0)

Shell script!

--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.]

pageoneresults




msg:1299284
 12:57 am on Apr 18, 2003 (gmt 0)

Hehehe, let me throw a wrench in the gears. Here is the FP way...

<!--webbot bot="Include" u-include="/navigation/left.asp" tag="body" -->

Alright, come and get me! ;)

P.S. Feeding data from a MySQL database.

jatar_k




msg:1299285
 1:22 am on Apr 18, 2003 (gmt 0)

pageone, you should be ashamed of yourself. ;)

Would a MySQL or a flatfile database be the best way to go?

Marcia, either works well it depends on what you have available to you. I have used both and very good results both ways.

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.

jamie




msg:1299286
 4:04 pm on Apr 22, 2003 (gmt 0)

don't includes prevent the header from returning a valid last-modified date and prevent caching? meaning every user has to load the page freshly, regardless of when he last saw it?

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?

jatar_k




msg:1299287
 3:54 pm on Apr 23, 2003 (gmt 0)

the last-modified date would still be on the file that includes the headers and footers or the template page.

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.

jamie




msg:1299288
 6:19 pm on Apr 23, 2003 (gmt 0)

thanks jatar_k

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

jatar_k




msg:1299289
 6:23 pm on Apr 23, 2003 (gmt 0)

depends on the site but mostly, yes.

The same will stand true if you use a php extension.

madcat




msg:1299290
 6:16 pm on May 1, 2003 (gmt 0)

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.

Don't create what kind of thing from scratch? A website? If so, wouldn't it be better to create your own from scratch? If not, please elaborate...

And then put all content into a database for even more control. I love the concept of dynamic sites.

One thing I still can't grasp about this...

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?

Birdman




msg:1299291
 6:40 pm on May 1, 2003 (gmt 0)

>>Say you have multiple paragraphs, headers, etc. How do you put the content into a database and split it up on the page?

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.

madcat




msg:1299292
 2:18 am on May 2, 2003 (gmt 0)

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>.

Ok, so bear with me on this. Say I have three pages:

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 ^

Birdman




msg:1299293
 3:56 am on May 2, 2003 (gmt 0)

madcat,

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

  • field 1: cat_id //INT auto increment(a way to reference the cats)
  • field 2: cat_name //i.e. Red Widgets, Green Widgets, etc...
  • field 3: cat_text //intro text for each category

table 2: products
  • field 1: product_id //item number
  • field 2: prod_cat_id //what category it's in(cat_id)
  • field 2: product_name
  • field 3: product_description
  • field 4: price
  • field 5: stock //how many are in stock(optional, of course)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved