“How to add archive functionality to your PHP app”

“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:

Omar Al Zabir www.phpfour.com somewherein bangla blog

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
image image

 

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 🙂

17 Comments

  1. nice post man. but you have missed the ignitted code ;). Missing your controller. As though the category is CI so if you mention controller and model code then lot of coders can save their time 🙂

    however, excellent post 🙂

  2. hello,brother the code u gaiven the archive is very useful to me and for all…
    But i need the same archive in another format..
    i.e first we display the year i.e 2009 ,once when we click on that year ,i need to display all the posts months wise along with count in the bracket,if we move to 2010 again same format..it will continue like this……..

  3. How can I fetching appropriate posts based on the passed archive information (year and month) with the clean URL?

    Can you send me a example to integrate in my blog?

    Thanks!

  4. @Thiago

    Hi Friend,
    Thanks for asking.

    First of all, you have to catch the month and year in two variables (suppose $month and $year). This step depends on how your framework implementing ‘clean URL’ and providing request variables.
    Then your query will be something like :
    $query = “SELECT * FROM blogs WHERE MONTHNAME(create_date) = ‘$month’ AND YEAR(create_date) = ‘$year'”;

  5. again…

    <?
    $dbh = mysql_pconnect($db_host, $db_user, $db_pass);
    mysql_select_db($db_name,$dbh);
    $query=”SELECT COUNT(id) total, MONTHNAME(create_date) month, YEAR(create_date) year FROM news_en GROUP BY MONTH(create_date), YEAR(create_date) ORDER BY create_date”;
    while($row = mysql_fetch_array($query)) {

    foreach ($row as $month): ?>

    <li>
    <a href=”news.php?year=<? echo $month[‘year’] ?>&month=<? echo $month[‘month’] ?>”>
    <?php echo $month[‘month’] . ” ” . $month[‘year’] .”(” . $month[‘total’] .”)” ?>
    </a>
    </li>

    <? endforeach; } ?>
     

Leave a Comment

Your email address will not be published. Required fields are marked *