Forum Moderators: coopster

Message Too Old, No Replies

Using a template to create pages with PHP and MySQL

         

Webbies

9:32 am on Apr 20, 2006 (gmt 0)

10+ Year Member



Ive been trying for ages to find information on how to use a template with php to include the main content which is stored in a database. I need detailed information with code examples how to do that and also information on how to store the content in the database (not how to import it but rather how it should be stored, as formatted HTML? a table collumn for page ID? A collumn for page title? Anything else?), how do I get it to load the right content into the template, how to do the navigational links etc...

If anyone could tell me how to do this or point me to somewhere with good information about this it would be much apreciated!

scriptmasterdel

10:47 am on Apr 20, 2006 (gmt 0)

10+ Year Member



Hi Webbies,

When i create a basic website for a client that requires a CMS i use a technique that we will call H-C-F (header content footer).

What i start of is with 4 files:-

Header file (usually header.php)
CM file (usually contentmanage.php)
Footer file (usually footer.php)

cm page (whatever you want but we will use index.php for now)

Now ... The reason why i use a header and a footer instead of template.php is that some pages you will be use will contain forms so this makes it easier to implement them into the site.

> Content Manage
In your content manage file you would have the mysql query / select statements and you would have to retrieve the filename from the current file you are on and select the data from the database that corrisponds with the page you are on ... like followed

<?
# explode the current path to get the filename
$filename = explode("/",$_SERVER['PHP_SELF']);

$select = “SELECT * FROM table where pagename = '”.$filename.”'”;
$result = mysql_query($select);
$row = mysql_fetch_assoc($result);

echo '<h1>'.$row['page_title_field'].'</h1>';
echo $row['page_content_field'];
?>

> Your Template
Say you have a website template like so

<div class=”header”>
This is my header
</div>

<div id=“ul_container”>
<ul id=”nav_container”>
<li class=”nav_link”><a href=””>link 1</a></li>
<li class=”nav_link”><a href=””>link 1</a></li>
</ul>
</div>

<div class=”content”>
this will be the content from the database
</div>

<div class=”footer”>
This is my footer content
</div>


You would place the top half into your header file (shown below)

<div class=”header”>
This is my header
</div>

<div id=“ul_container”>
<ul id=”nav_container”>
<li class=”nav_link”><a href=””>link 1</a></li>
<li class=”nav_link”><a href=””>link 1</a></li>
</ul>
</div>

<div class=”content”>

You would place the bottom half into your footer file (shown below)

</div>

<div class=”footer”>
This is my footer content
</div>

Then on a page that you want to be content managed (index.php )you would have code like followed

<?php
include('header.php');
include('contentmanage.php');
include('footer.php');
?>

All pages that you require to be content managed should contain:-

<?php
include('header.php');
include('contentmanage.php');
include('footer.php');
?>

Hope this is a little help a little,
Del

trillianjedi

11:55 am on Apr 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's a nice little example Del. I do this kind of thing all the time myself.

I would add to this and say that it can be advantageous, if it suits the scenario you have, to get the script to actually dump the entire page contents to filename.html and effectively create a static site rather than one that is DB driven.

My view has always been to only hit the database for reads when absolutely necessary. 99% of websites, that do not have some form of cache running in front of them, are extremely inefficient in this regard.

The majority of the time, for a content website, a database backend is not necessary. There are exceptions (highly active content sites/pages allowing comments/forums etc).

Hybrids are always a good idea - use the live DB on pages you have to, keep everything else static.


//Now create the HTML file

$filename = $whatever.".html";

if (!$handle = fopen($filename, 'a')) {

echo "Whoops! Cannot open file ($filename)";
exit;
}

$header = file_get_contents("/path/to/header.html");

$footer = file_get_contents("/path/to/footer.html");

//Write header to our file

if (fwrite($handle, $header) === FALSE) {
echo "Whoops! Cannot write header to file ($filename)";
exit;
}

# explode the current path to get the filename
$filename = explode("/",$_SERVER['PHP_SELF']);
$select = “SELECT * FROM table where pagename = '”.$filename.”'”;
$result = mysql_query($select);
$row = mysql_fetch_assoc($result);

fwrite($filename, '<h1>'.$row['page_title_field'].'</h1>');
fwrite($filename, $row['page_content_field']);

// Write footer to our file
if (fwrite($handle, $footer) === FALSE) {
echo "Whoops! Cannot write footer to file ($filename)";
exit;
}

fclose($handle);

//Update navigation page with new content page
etc...

TJ

webjourneyman

9:32 pm on Apr 24, 2006 (gmt 0)

10+ Year Member



"I would add to this and say that it can be advantageous, if it suits the scenario you have, to get the script to actually dump the entire page contents to filename.html and effectively create a static site rather than one that is DB driven."

Is this something most tutorials teach?

If I was doing this, and changed something in the header would I need to do something special to update the static pages or would it happen automaticly?

B.t.w. what is the quote tag you use here?