“An archive refers to a collection of records, and also refers to the location in which these records are kept. Archives are made up of records which have been created during the course of an individual or organization’s life.” – Wikipedia.
Archive is an essential feature of a well defined blog system. If you have a blog in blogger or wordpress, you’ll get built-in archive feature there. But if you are making a blog yourself or any similar application, you might need to add this function yourself. These are some ideal example of archives:
I’ve done it for one of my app and here I am telling you the time saving tip: creating an archive function in 3 easy steps.
1. Generate organized archive data: Generally, archives are created on create date of contents. Let’s say, your blogs table has a field named create_date which holds the timestamp of the post. Now, you can get your archive data in very organized format with this simple yet powerful query (it’s in MySQL):
SELECT COUNT(id) total, MONTHNAME(create_date) month, YEAR(create_date) year
FROM blogs
GROUP BY MONTH(create_date), YEAR(create_date)
ORDER BY create_date
Note that you’ll need to use where clauses if there are any condition. The output of your query should look like this:
Query to retrieve archive data | The result |
2. Use the archive data: After you have retrieved the archive data, you can display them in any sidebar of your application. A sample code can be as follows:
1: <h4>Older Entries</h4>
2: <ul>
3: <?php foreach ($archive as $month): ?>
4: <li>
5: <a href="<?php echo $month['year'] . "/" . $month['month'] ?>">
6: <?php echo $month['month'] . " " . $month[' year'] ."(" . $month['total'] .")" ?>
7: </a>
8: </li>
9: <?php endforeach; ?>
10: </ul>
So, what’s done here? Well, it displays the archive sets with an appropriate link. Note that the link are created in “clean URL” style, i.e. yourdomain.com/2007/july – if you are using an MVC framework like CodeIgniter or CakePHP, you may find it easy to integrate. You can of course create it in the old style, by changing the above code in this way:
1: <li>
2: <a href="blog.php?year=<?php echo $month['year'] ?>&month=<?php echo $month['month'] ?>”>
3: <?php echo $month[''month'] . " " . $month[' year '] ."(" . $month['total'] .")" ?>
4: </a>
5: </li>
3. Display related posts: And now you’ll need to handle the fetching of appropriate posts based on the passed archive information (year and month), but that’s something that only you can figure out, as I don’t know what you have in there.
Happy Blogging…Happy Archiving 🙂