Forum Moderators: coopster
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 :).
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
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 ;).
<? 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
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!
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.
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. :)
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.