Build Your Own PHP Blog: Part 2

December 24th, 2010

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"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $title; ?></title>
<link href="style.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div id="divbody">
<div id="header"><h1><?php echo $header; ?></h1></div>

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>
</body>
</html>

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
$title = 'myPHPblog';
$header = 'Matt\'s Awesome PHP Blog';
require_once('./header.php');
require_once('./sidebar.php');
require_once('./footer.php');
?>

Okay, maybe it’s not all that stunning yet. Yet.

Build Your Own PHP Blog: Part 1

December 22nd, 2010

I’m going to be using PHP and MySQL to build my own WordPress-like blog application. I’m just going to build a bare bones blogging platform, and if I feel like adding some bells and whistles, I’ll worry about that later. To read this article and all subsequent ones, you should know some HTML, some PHP, and some SQL. If you don’t but would like to learn, w3schools.com is a fantastic place to learn for free. If I feel really ambitious, maybe one day I’ll put up my own tutorials. If you don’t like the w3schools format, I’m sure a google search for “PHP tutorial” will be very fruitful. So, since you’re still reading, let’s start to build your own PHP blog!

Before we dive in to writing any code, let’s take a second to see how everything is going to fit together. First of all, we’ll be using a database to store our posts and information about them such as the date they were posted and their category. Then we’ll make a page with a header image, a sidebar, and a post area. This page will pull up the last few posts from the database. This is the page that your visitors will see. Finally, we will need a secure page, an admin page, where the user can actually write and post the posts. I think this is as bare bones as you can get when creating a blog.

The best place to start is the database. Starting with the visible page won’t work because there is no place for the page to pull the posts from. Starting with the admin page won’t work because it has no place to store the posts without the database.

First, we need to create the database. I’ll be using phpmyadmin to create my database. Execute this SQL statement in phpmyadmin.

CREATE DATABASE my_php_blog;

Keeping with the minimalist mindset, we are only going to store a few things about the post. The title, the date, and the post content itself.

CREATE TABLE posts (
post_id INT NOT NULL AUTO_INCREMENT,
post_date DATETIME NOT NULL,
post_title TEXT NOT NULL,
post_content LONGTEXT NOT NULL,
PRIMARY KEY (post_id)
);

That’s all there is to it. Your database is ready to store posts. Having never done something like this on my Hostgator account before, I had to go through a number of steps to get to this point. I will update this post to include those steps in the near future.

The Benefits Of Exercise – Why You Can’t Afford Not To Exercise

December 11th, 2010

The benefits of exercise are available for everyone. Not just people trying to lose weight for new years. Not just for people trying to slim down so they look sexy in a bikini this summer. Not just overweight parents trying to live long enough to watch their kids grow up. It’s also for the guy that’s skinny as a rail and eats nothing but hamburgers. It’s for the dieters who think eating better is enough to lose some weight. It’s for me just as much as it is for you.

Everyone knows that exercise is good for you. It helps you burn calories, build muscle, run faster, jump higher, all that good stuff. But what if you don’t care about all that good stuff? What if you’re happy with your weight? What if you don’t want veiny muscles popping off you like a bodybuilder? What if you don’t care about running a 5K ever? Amazingly, the benefits of exercise go way beyond rippling abs. There are tons. Here are a few of the benefits of exercise I can remember off the top of my head.

Increased Mental Acuity

Exercise increases blood flow to the brain. If you’ve ever felt sluggish, you might notice that your brain feels sluggish too, and vice versa. I personally notice that thoughts come more freely to me after I work out, and they come with much greater clarity.

Staves Off Depression

Again, for me personally, this should be more than enough of a reason to work out. Exercise stimulates the release of a feelgood neurotransmitter, endorphins, in the brain. Not only that, but the more consistently you work out, the more self esteem you build for having the discipline to continue exercising. Of course the danger in that is missing a session and beating yourself up about it. I wouldn’t recommend doing that, but even if you do, just keep going back and those endorphins will make it worth your while.

The Best Cure For Stress

The same thing applies to overworking (stress) as underworking (depression). You may not think you have time to squeeze in a workout after such a stressful day, but the same endorphins that stave off depression also dissolve stress.

Increased Overall Health

Consistent workouts not only bring lower body fat and more muscle, they bring a host of great effects to your body. Every organ in your body functions more efficiently with exercise. Your bones become stronger with all weight-bearing exercise. Even your skin and hair will look better.

Increased Sex Drive and Stamina

Not been in the mood lately? People who exercise have a higher sex drive, and enjoy sex more than people who don’t. Working out may also be one step on dealing with self image problems if they’re there.

An Ounce Of Prevention

Is indeed worth a pound of cure. Frequent exercisers are less likely to die of every natural cause in the book. They’re less likely to contract diseases, less likely to get cancer, and are exempt from all the terrible consequences of obesity. If you want to live longer (and who doesn’t?) exercise is the way to do it.

Like I said before, these are the ones off the top of my head. There are plenty of other benefits of exercise. Next time you don’t feel like working out, go through this list and ask yourself if you can afford not to.

Back Up Your Databases

November 30th, 2010

After transferring this site to a new hosting service, I lost all my previous posts. After some minimal digging, I found that posts in WordPress are stored in a place called a MySQL database. So while I saved all the pages to my old site, I didn’t save any of the posts because I neglected to back up my database. I’m now much more familiar with these bad boys called MySQL databases, so this time, my posts are here to stay.

Since I have a blank slate to work with, I’m going to dedicate the first few posts to web programming. More specifically my current area of study: PHP. PHP is a very popular programming language that allows you to build interactive websites. I’m working through a wonderful book called Head First PHP & MySQL. I’m halfway through it right now, and I think I’m more than able to build a bare-bones, blog-type website that’s similar to (but much much lighter than) WordPress. I’ll be writing the code from scratch and posting it up for everyone to see. I hope you will enjoy it.