Build a Blog or CMS with PHP

Tutorial Using PHP and MySQL to Create Web Publishing Platforms

© Guy Lecky-Thompson

May 28, 2008
First steps PHP programming tutorial using PHP with MySQL to create a simple blogging platform or content management system.

This article is designed to give the beginning PHP programmer a helping hand in designing a rudimentary content management system or blogging platform using PHP and MySQL. It is a useful insight as to how real CMS and blogs work, for those who wish to customize them, but also a good starting point for those developing their own custom solutions.

The three topics that will be covered are:

  • Defining the layout;
  • Creating appropriate modules;
  • Using PHP to access MySQL.

Having read the article, the reader should be able to build a very simple blog with:

  • A list of blog entries;
  • A main blog page;
  • Dynamically generated content.

If the reader is not familiar with PHP, they should read the Beginning PHP Programming article.

Before looking at the underlying PHP, it is necessary to define a simple HTML table into which the content will be placed. This can then be copied and pasted into the index.php file that will form the skeleton for the solution created by the reader.

The Layout

The basic layout is a three column one - with the list of articles on the left hand side (25% width), the main content in the center (75% width) and any dynamic content (Google AdSense, for example) in the right hand sidebar. The basic table would look something like the following:

<table width=100% cellspacing=0 cellpadding=0 border=0>
  <tr>
  <td width=25%><?php include("contents.php"); ?></td>
  <td width=75%><?php include("mainpage.php"); ?></td>
  <td width=25%><?php include("rhs.php"); ?></td>
  </tr>
</table>

The reader will notice that the above HTML fragment contains references to three PHP files, modules, which will do all the work of creating the content for the page.

Using Modules

It is best to use modules rather than inlining the code, because it keeps everything separate. Most CMS and blog platforms also contain a header(); and footer(); module as well as the others, so that specific information can be displayed.

With the modules specified above, though, there is enough to build the list of current articles, and present a list of links that can be clicked, display the main content, and build any specific dynamic content on the right hand side.

To do this, it is necessary to access the MySQL database.

Accessing the Database

In this example, it is assumed that there is a table in the database that contains each article, with an id, title, content, and date field. The first operation is to get a connection to the MySQL database:

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');

Clearly, the mysql_user and mysql_password entries will have to be replaced with applicable values. Once the $link is established, the code can then go on to select the appropriate records. In the first instance (in the contents.php file), a list of article titles is to be built. This is a two stage operation:

if (!mysql_select_db('blog_database', $link)) {
echo 'Database error';
exit;
}

Once the link to the database has been constructed, the query can be built, and run against the selected database:

$sql_statement = 'SELECT Id, Title, Content, Date FROM Blog_Entries LIMIT 0, 10';
$result = mysql_query($sql_statement, $link);

The next step is to build the list by scrolling through the result set. The code for this might look something like:

echo '<ul>';
while ($curr_row = mysql_fetch_assoc($result)) {
  echo '<li><a href="index.php?article=' . $curr_row['Id'] . '">' . $curr_row['Title'] . '<a></li>';
}
echo '</ul>';
mysql_free_result($result);

Similar code can then be used to build the main content, but with a subtle difference in the query and HTML generation:

$sql_statement = 'SELECT Id, Title, Content, Date FROM Blog_Entries LIMIT 0, 5';
$result = mysql_query($sql_statement, $link);
while ($curr_row = mysql_fetch_assoc($result)) {
  echo '<h1>' . $result['Title'] . '</h1>';
  echo '<p>' . $result['Content'] . '</p>';
}
mysql_free_result($result);

Both of the above will retrieve the first five, or ten, entries only. The first index can also be replaced with a parameter taken off the URL generated by the contents.php script above. This is easily done. The key is that the ?article= part of the URL places a specific value from the URL into the $article variable. This can then be reused in the code:

$sql_statement = 'SELECT Id, Title, Content, Date FROM Blog_Entries LIMIT ' . $article . ', 5 ';

Of course, if the programmer deletes an entry, then the above will not work until the Id field is re-indexed.

Building the rest of the code should be an easy task even for a beginning PHP programmer. The main task will involve putting data into the database, which can be done via the PHP interface to MySQL, or using validated forms and passwords.


The copyright of the article Build a Blog or CMS with PHP in PHP Programming is owned by Guy Lecky-Thompson. Permission to republish Build a Blog or CMS with PHP in print or online must be granted by the author in writing.




Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo