In Part 1 of build your own PHP blog, we set up the database and tables necessary to hold all of the blog data. I started on the admin section a few days ago, and it’s turning out to be a lot more code than I had originally thought. So I think I’ll start with the actual front end blog page first. The layout will be simple. There will be a header, a footer, a sidebar, and a content area. So step one is to create four new files:
- header.php
- footer.php
- sidebar.php
- index.php
Simple enough, right? Let’s start off with the header. I’ll start off with the code and explain it as I go along.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
Hopefully the DOCTYPE, html, and head tags are self explanatory. If not, stop reading immediately, and shoot on over to here: learn html. Next, we open two other tags, the <body> and <div id =”divbody”> tags, but we don’t close them. Don’t worry about that. We’ll get to that in the footer. Since the body must provide the background for the entire page, the divbody will be the container for all our content. That’s pretty much it for the header. Note the $title, and $header php variables. We have to define them prior to inserting the header.
Next, the footer. This one’s super easy:
</div> |
All we’re doing here is closing the divbody, body, and html tags. Why even bother putting this in it’s own separate file? It seems kind of wasteful, doesn’t it? There are a couple of good reasons to do so. The first is that we are going to include both of these files in index.php, and it makes more sense to anyone reading it to see include(‘./header.php’) paired with include(‘./footer.php’). Secondly, if you ever want to add something to the footer, like a copyright or a link to another blog, you only have to add it once. It makes it much easier to maintain your code.
Since sidebar.php is going to be a little more complicated, I’ll tackle that in the next section.
Now, we can simply throw all this into the index.php file, and it becomes a gorgeous masterpiece.
<?php |
Okay, maybe it’s not all that stunning yet. Yet.