Category Archives: codeigniter

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

 

<span style="color: #606060"> 1:</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">h4</span><span style="color: #0000ff">&gt;</span>Older Entries<span style="color: #0000ff">&lt;/</span><span style="color: #800000">h4</span><span style="color: #0000ff">&gt;</span>
<span style="color: #606060"> 2:</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">ul</span><span style="color: #0000ff">&gt;</span>
<span style="color: #606060"> 3:</span> <span style="color: #0000ff">&lt;?</span><span style="color: #800000">php</span> <span style="color: #ff0000">foreach</span> ($<span style="color: #ff0000">archive</span> <span style="color: #ff0000">as</span> $<span style="color: #ff0000">month</span>)<span style="color: #ff0000">:</span> ?<span style="color: #0000ff">&gt;</span>
<span style="color: #606060"> 4:</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">li</span><span style="color: #0000ff">&gt;</span>
<span style="color: #606060"> 5:</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">a</span> <span style="color: #ff0000">href</span><span style="color: #0000ff">=&quot;&lt;?php echo $month['year'] . &quot;</span>/&quot; . $<span style="color: #ff0000">month</span>[<span style="color: #0000ff">'month'</span>] ?<span style="color: #0000ff">&gt;</span>&quot;<span style="color: #0000ff">&gt;</span>
<span style="color: #606060"> 6:</span> <span style="color: #0000ff">&lt;?</span><span style="color: #800000">php</span> <span style="color: #ff0000">echo</span> $<span style="color: #ff0000">month</span>[<span style="color: #0000ff">'month'</span>] . <span style="color: #0000ff">&quot; &quot;</span> . $<span style="color: #ff0000">month</span>[<span style="color: #0000ff">' year'</span>] .<span style="color: #0000ff">&quot;(&quot;</span> . $<span style="color: #ff0000">month</span>[<span style="color: #0000ff">'total'</span>] .<span style="color: #0000ff">&quot;)&quot;</span> ?<span style="color: #0000ff">&gt;</span>
<span style="color: #606060"> 7:</span> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">a</span><span style="color: #0000ff">&gt;</span>
<span style="color: #606060"> 8:</span> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">li</span><span style="color: #0000ff">&gt;</span>
<span style="color: #606060"> 9:</span> <span style="color: #0000ff">&lt;?</span><span style="color: #800000">php</span> <span style="color: #ff0000">endforeach</span>; ?<span style="color: #0000ff">&gt;</span>
<span style="color: #606060"> 10:</span> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">ul</span><span style="color: #0000ff">&gt;</span>

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:

<span style="color: #606060"> 1:</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">li</span><span style="color: #0000ff">&gt;</span>
<span style="color: #606060"> 2:</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">a</span> <span style="color: #ff0000">href</span>=&quot;<span style="color: #ff0000">blog</span>.<span style="color: #ff0000">php</span>?<span style="color: #ff0000">year</span>=&lt;<span style="color: #ff0000"></span>?<span style="color: #ff0000">php</span> <span style="color: #ff0000">echo</span> $<span style="color: #ff0000">month</span>[<span style="color: #0000ff">'year'</span>] ?<span style="color: #0000ff">&gt;</span>&amp;month=<span style="color: #0000ff">&lt;?</span><span style="color: #800000">php</span> <span style="color: #ff0000">echo</span> $<span style="color: #ff0000">month</span>[<span style="color: #0000ff">'month'</span>] ?<span style="color: #0000ff">&gt;</span>&#x201D;<span style="color: #0000ff">&gt;</span>
<span style="color: #606060"> 3:</span> <span style="color: #0000ff">&lt;?</span><span style="color: #800000">php</span> <span style="color: #ff0000">echo</span> $<span style="color: #ff0000">month</span>[<span style="color: #0000ff">''</span><span style="color: #ff0000">month</span><span style="color: #0000ff">'] . &quot; &quot; . $month['</span> <span style="color: #ff0000">year</span> <span style="color: #0000ff">'] .&quot;(&quot; . $month['</span><span style="color: #ff0000">total</span>'] .<span style="color: #0000ff">&quot;)&quot;</span> ?<span style="color: #0000ff">&gt;</span>
<span style="color: #606060"> 4:</span> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">a</span><span style="color: #0000ff">&gt;</span>
<span style="color: #606060"> 5:</span> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">li</span><span style="color: #0000ff">&gt;</span>

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