Oct. 19, 2016, 10 p.m.
By : Eric A. Scuccimarra
It was a bit tricky for me to get the archive list on the right here working properly. It was easy to do in normal PHP, but I wanted to stay within a strict MVC model where all the processing is done in the controller and the view just displays the data. I was having a hard time figuring out how to properly group the items without setting variables in the view.
I ended up building an array with three nested levels in the controller. The array is as follows:
I start with my array with the $year as key and an empty array as the value. Then for each month I push on an array with $month as key and an empty array as the value. Then for each post I push on an array with two values - the post $title and $slug. This array is passed into the view.
To display it I just do three nested foreach loops:
Simple, clean and easy! Much simpler than the other ways to do this I found online.
There are no comments for this article.