Creating a PHP CMS – Part 1
So far, my experience with PHP has been doing things like making small adjustments to Wordpress plugins. So, I decided that it was time that I actually learn to use PHP. I thought that a good way to do this is by creating a simple CMS.
In this series of posts, I will go over the steps to create a simple CMS using PHP and MySQL. I will try to explain everything, but if there is anything you don't understand, w3schools has a pretty good PHP reference, or you can leave a comment and I will do my best to help you out.
I will be adding a new post in this series every Monday, Wednesday and Friday.
To create a CMS you will need
- A server with PHP and MySQL installed
- Something for viewing MySQL databases and executing queries, such as phpMyAdmin
Getting Started
The Database
First of all, you will need to create a MySQL database to store information for your CMS. Once you've created it, you need to add a user to the database.
After you've created the database and added a user to it, run the following SQL queries. If you're using phpMyAdmin, enter them under the SQL tab of your database.
CREATE TABLE pages ( id INTEGER AUTO_INCREMENT, title VARCHAR(150), body TEXT, date VARCHAR(10), PRIMARY KEY (id) );
This creates a table for storing the pages for the CMS. 'id' is a number that uniquely identifies each post. The AUTO_INCREMENT part means that each time you insert a record into the table, this number is one more than the last one. 'title' contains the title of the post. VARCHAR(150) means that it can hold a string up to 150 characters long. 'body' contains the main text for the page.
'date' stores the time that the post was written. It is 10 characters long because we will be storing the date in Unix time. Since it can only store 10 digits, it won't be able to store dates after the year 2038 problem. If anyone knows a way to fix that, please leave a comment to let me know how.
PRIMARY KEY (id) means that 'id' will be a unique identifier.
CREATE TABLE settings ( name VARCHAR(20), value VARCHAR(20) );
This creates a table for storing settings. It can hold a setting name, and a value for the setting. Each one can be up to 20 characters long. If you plan on expanding on the CMS later, you might want to set the values higher.
The Files
After we've setup the database, we need to create some files. First, create a directory on your server that will contain these files.
Create the following files, and leave them empty for now.
- index.php
- functions.php
- config.php
Now, create a directory called 'admin'. This will contain all of the files for the CMS administration.
In the admin directory, create these files.
- index.php
- new.php
- create.php
That's it for this part of the series. Be sure to subscribe to the RSS feed to receive updates on this series.
Hi, I hope so that this series will help me finally to understanding PHP & MYSQL.
When we can expect second part?
Kabarovsky: Part 2 will be posted tomorrow, and a new post in this series will be published every Monday, Wednesday and Friday.
Ok, tnx a lot!
You posted a nice post. This will really help to understand the PHP CMS development.
Thanks for the comments, everyone!
I know create database, tables etc. in phpMyadmin but where should go this code:
CREATE TABLE pages (
id INTEGER AUTO_INCREMENT,
title VARCHAR(150),
body TEXT,
date VARCHAR(10),
PRIMARY KEY (id)
);
..in one file for example database.sql or what?
Kabarovsky: If you're using phpMyAdmin, go to your database, and then under the SQL tab, enter the code into the box and click go. I will update the post to include this.
Tnx! I figure out everything in these two posts.. Really useful
The way to solve the year 2038 problem is to make 'date' the type of 'DATEIME', someting like 2009-08-18 21:23, and you can use the php function strtotime() to change the DATETIME type to UNIXTIME type to use for the function date().
That's all to your problem.
My English is poor, because my native language is Chinese.
Misspelling the word 'DATETIME', sorry about that.
Hua Chen: Thanks! I'll be sure to remember that.
Eric Bannatyne: You are welcome!
This is awesome! I can see where you're going with this - which is an effective yet simple full functional CMS for individuals. I am going to complete this entire series and create a site with it to see the full functionality of it.
I have been wanting to write a custom CMS for about a year, as I do many small projects for non-profits.
I hope you do. Building your own CMS can be a great learning experience (it was for me), and it's also a nice feeling to have something working that you put together yourself.
If you create a functional site with this, I'd love to see it when it's complete!