Forum Moderators: coopster

Message Too Old, No Replies

Best way to handle HTML content in templates?

         

aus_dave

4:15 am on Mar 1, 2004 (gmt 0)

10+ Year Member



I'm building a web site using PHP templates, storing several variables in a database e.g. $pagetitle, $keywords etc.

How do most people handle the HTML content of the page (say the text after the <h1> tags)? I was planning to create plain HTML files and then add the path to this file as another variable in the database, accessing it in the template via an include. I know it is possible to add the HTML direct to the database but I would find this more difficult to edit and work with.

I'm relatively new at this and just wanted to find out if my proposed method was acceptable :).

mikeb

11:53 am on Mar 1, 2004 (gmt 0)

10+ Year Member


The way I approach the database driven pages is as follows:

1. IN THE CASE OF A SINGLE RECORD PER PAGE
Imagine a page named myArticle.php

This page has a title, subtitle and body copy, all stored in the database as, say $title, $sub_title $body_copy. Your .php page will be something like this:

PHP Section:
<?
query database for correct story
While pulling out the record
set $title=database[$title]
set $sub_title=database[$sub_title]
set $body_copy=datbase[$body_copy]
?>

HTML Section
<html><head><title>My Article</title></head>
<body>
<h1><? echo $title?></h1>
<h2><? echo $sub_title?></h1>
<p />$body_copy
</body>
</html>

2. IN THE CASE OF MULTIPLE RECORDS (LISTING TYPE) PAGE

Imagine a page called "news_headlines.php"
This page pulls out a listing of all news articles in the database and presents them as a list. The fields might be $news_title, $news_subtitle

PHP Section:
<?
query database for correct stories
While pulling out the records{
set $title=database[$news_title]
set $sub_title=database[$news_subtitle]
display_block.="<tr><td><a href="myarticle.php?story_id=$story_id></a>$title</td><td>$sub_title</td></tr>"
}
?>

HTML Section
<html><head><title>My Article</title></head>
<body>
<h1>Latest Stories</h1>

<p /><? echo $display_block ;?>

</body>
</html>

This is just an approach and pseudo-code which assumes knowledge of php. Hope that answers your question,

cheers,

Mike

aus_dave

1:04 pm on Mar 1, 2004 (gmt 0)

10+ Year Member



Thanks Mike for the detailed reply :).

I am trying to avoid having the bodycopy stored in the database if at all possible. I would prefer to edit and preview the HTML locally, and then FTP it to a directory (storing /path/to/thisfile.htm in the database instead). If there is a good reason not to do it this way i.e. security etc. then I will add the bodycopy in the database and use a similar method to what you described ;).

mikeb

1:32 pm on Mar 1, 2004 (gmt 0)

10+ Year Member



Just to understand a bit better - do you just want to store 'pointers' to the relevant html pages as records in the database, and then use those references to call include the html, something like this:

<? do query on inlcuded pages from db?>
<html><head><title></title></head>
<body>
<p />
Welcome to whatever, tra la la...

<p />
<? echo include($includepage.htm) ;?>

</body>
</html>

Maybe you could expand a little (without giving too much away about your site if you're not keen) on what your site does, how it works etc.

cheers,

Mike

aus_dave

1:52 pm on Mar 1, 2004 (gmt 0)

10+ Year Member



Mike, the code in your last post is exactly what I want to do ;).

I know how to do the includes etc. but am unsure of the best way to handle the content (site is around 70-80 pages). I've found some references that mention storing body copy directly in the database is useful for building a local search engine, which I guess is one advantage. I think I could probably use XML for this too but that is a bit beyond me at the moment (I'm using valid XHTML so it might be straightforward to use XML once I get up to speed).

From what I can gather my proposed method works best for smaller sites, for larger sites you end up with 100s or 1000s of text files which defeats the purpose somewhat!

tulip

4:46 pm on Mar 5, 2004 (gmt 0)

10+ Year Member



aus_dave, I've been building a site that seems to apply just the method you've been wondering about. My site has a lot of images, and I did not want to place them in a database, so I ended up creating a lot of small html/inc files for longer text content as well and storing the pointers in the DB (rather than storing the actual texts in the DB, which is what I did with smaller bits of data). So far it has worked fine, but then I'm only dealing with max 100 or so files. A thousand files might be a bit more problematic, as you said.

aus_dave

1:09 am on Mar 9, 2004 (gmt 0)

10+ Year Member



Thanks tulip, it sounds like I am on the right track then :).

Trisha

1:23 am on Mar 12, 2004 (gmt 0)

10+ Year Member



I have not had a lot of PHP/MySQL experience yet, but ran into this same problem with a previous site. I ended up storing HTML in the database. I'm still not sure if that was the best way to do it though. I thought what's the point of using a database for the site if I'm going to end up with an html file for each article anyway? But then in the end, I write the article first in word, add the html, then put it in the database. So I still keep a file for each article in word anyway. I'd love to know how other people do this also.

I guess the ideal situation would be if each article was the same in overall structure - an introductory paragraph, 3 body para. and a conclusion para. for example. Then each of those 5 paragraphs would be in their own db field. There is no way I could get mine written like that though.

brucec

1:59 am on Mar 12, 2004 (gmt 0)

10+ Year Member



To mikeb,
I like your suggestion. However, I have been coding in MySQL for over 2 years and I suggest not to do it this way. Putting HTML into a single database field can kill the database free space. You can use a MySQL database type of TEXT which can give you megs of disk space per record.

I thought the original idea of adding HTML includes to the PHP is a good way to keep it down.

Just my 2 cents, but I liked your code above. :)

brucec

2:14 am on Mar 12, 2004 (gmt 0)

10+ Year Member



Trisha,
I think you have the right idea, but I suggest getting to know both the built-in functions in both MySQL and PHP. I always found that built-in functions are wonderful, because they do so much of the work for you and the functions manipulate the data for you and then you can put them into PHP variables with echo statements.

In your case with a different section of your article, I would not store "common" HTML in the database. Because, you can use up your disk space for the database very quickly.

Instead, if most of your HTML is for formatting, then I suggest using a linked CSS (Cascading Style Sheet) file and you can call it in the <HEAD> tags of your PHP. Trust me. :) I do it this way and it keeps my file size small.

So, using an externally linked cascading style sheet, you can store just plain content in your MySQL database and then like Mike B said in the second post, just use a PHP while loop to read the content from your database and build the articles on the web page that way.

Now, as Mike B said, you can do it this way with the PHP:
WHILE($ContentArray = mysql_fetch_array(Query Variable)) {
$Conclusion="<span class=Style Name>$ContentArray["Conclusion"]</span>";
$Header="<span class=Header Style Name>$ContentArray["Header"]</span>";

echo "$Header";
echo "$Conclusion";

You can throw in all the HTML that you wish inside the quotation marks of the echo statement.

}

and so on...

The HTML <span class="Style Name"> tag can call the formatting of your CSS styles in the style sheet

I promise you that once you get good at this, it runs like clock work

Like you said, you can really have just 5 fields in each record for an article and then format the web page with CSS. Remember, that CSS is a component of HTML, not PHP, but PHP works just like HTML without all the programming.

I hope this helps. I know this is a lot of stuff, but it works for me.