CodeIgniter Forums

Full Version: Blog archive repeating
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]murphy2006[/eluser]
Hello,

I am working on an archive function for my blog system and I am almost finished.
There is only on small problem. Repeation.

For each post there is a new January 2009 so it looks like this:

Archive
January 2009
January 2009
January 2009

I want it to only show each month once.

Model:

Code:
$this->db->select('pos_timestamp');
$this->db->distinct();
$this->db->from('fe_posts');
return $this->db->get();

Controller:

Code:
$data['archives'] = $this->Frontend_model->getArchive();

View:

Code:
<h1>Archive</h1>

&lt;?php foreach($archives->result() as $archive): ?&gt;

<p>&lt;?php echo anchor('archive/'.substr($archive->pos_timestamp,0,4).'/'.substr($archive->pos_timestamp,5,2),date("F Y ",strtotime($archive->pos_timestamp))); ?&gt;</p>

&lt;?php endforeach; ?&gt;

How can I stop the repeation?

Thankful for all help

El Forum

[eluser]nikefido[/eluser]
they are not distinct because your timestamp (or datetime) data type in the database is storing a time along with the date.

There are few ways to go about this problem. Here is a quick description of one.

This is less "dynamic" but you can just create a function that checks for posts within the last 6 months (implementation of this depends on your data type in the database - timestamp vs datetime vs whatever else).
It would be something like:


1) - get today's date
$today = getdate(); //or "date();" - see php.net docs for best way

2) match this month and work backwards as many months as you want (you can make this a setting in your admin panel!)

3) Create a link that leads to a controller that checks a date in your URL and (example: http://www.mypage.com/archive/bydate/01/2009 - where this would be January 2009 - or whatever implementation you want to use for your URL)

This way you don't have to read from the database until absolutely necessary and you can give admins some control as to how many months back they want the archive to work