Creating a blog from scratch with PHP - Part 5 Sidebar, Categories and Archives

David Carr

Demos Tutorials Blog PHP & MySQL

Table of Contents

Blog Series


This tutorial is extending the Creating a blog from scratch with PHP with that in mind I will only be covering new pieces of code and not the whole codebase.

This part will cover adding categories to posts.

Demo

admin demo: http://www.dcblog.dev/demos/simpleblog-sidebar/admin

username: demo
password: demo

First the templates will need updating to be in 2 column the first column will be called main and the second will be called sidebar.

Open style/main.css and replace #wrapper with:

#wrapper {
    margin:auto;
    width:960px;
}

#main {
    float:left;
    width:640px;
    margin-right:20px;
}

#sidebar {
    float:right;
    width:300px;
}

For all pages add the following structure:

<div id='main'>

    <!-- php code here -->

</div>

<div id='sidebar'>
    <?php require('sidebar.php'); ?>
</div>

Next create a new file called sidebar.php

Sidebar.php will contain all the sidebar content.

Latest Posts

To add a latest posts section loop through the posts table and set a limit for how many record to display

<h1>Recent Posts</h1>
<hr />

<ul>
<?php
$stmt = $db->query('SELECT postTitle, postSlug FROM blog_posts_seo ORDER BY postID DESC LIMIT 5');
while($row = $stmt->fetch()){
    echo '<li><a href="'.$row['postSlug'].'">'.$row['postTitle'].'</a></li>';
}
?>
</ul>

For categories it's the same as above query the cats table and loop through.

<h1>Catgories</h1>
<hr />

<ul>
<?php
$stmt = $db->query('SELECT catTitle, catSlug FROM blog_cats ORDER BY catID DESC');
while($row = $stmt->fetch()){
    echo '<li><a href="c-'.$row['catSlug'].'">'.$row['catTitle'].'</a></li>';
}
?>
</ul>

For archives loop through the posts table return the month and year for each month use a  GROUP BY to merge identical results.

In the loop create a link in the href needs a custom slug creating that will be a- then the month then the year for instance:  a-2-2014, a new rule will need to be added to the .htaccess file to point the link to archives.php 

<ul>
<?php
$stmt = $db->query("SELECT Month(postDate) as Month, Year(postDate) as Year FROM blog_posts_seo GROUP BY Month(postDate), Year(postDate) ORDER BY postDate DESC");
while($row = $stmt->fetch()){
    $monthName = date("F", mktime(0, 0, 0, $row['Month'], 10));
    $slug = 'a-'.$row['Month'].'-'.$row['Year'];
    echo "<li><a href='$slug'>$monthName</a></li>";
}
?>
</ul>

Next update .htaccess and add the following above on line 5

RewriteRule ^a-(.*)-(.*)$ archives.php?month=$1&year=$2 [L]

This will point a-3-2014 to archives.php?month=3&year=2014

Next create a new file called archives.php

Copy the content of index.php to this file.
Replace the query this the following, this will collect the data from a $_GET request then display all posts that match the date range.

//collect month and year data
$month = $_GET['month'];
$year = $_GET['year'];

//set from and to dates
$from = date('Y-m-01 00:00:00', strtotime("$year-$month"));
$to = date('Y-m-31 23:59:59', strtotime("$year-$month"));

$stmt = $db->prepare('SELECT postID, postTitle, postSlug, postDesc, postDate FROM blog_posts_seo WHERE postDate >= :from AND postDate <= :to ORDER BY postID DESC');
$stmt->execute(array(
    ':from' => $from,
    ':to' => $to
));

Upload the files, All done, the blog now has a sidebar with categories, latest posts and archives.

Laravel Modules Your Logo Your Logo Your Logo

Become a sponsor

Help support the blog so that I can continue creating new content!

Sponsor

My Latest Book

Modular Laravel Book - Laravel: The Modular way

Learn how to build modular applications with Laravel Find out more

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Learn Laravel with Laracasts

Faster Laravel Hosting

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.