The Archive List on the Right Here
Par Eric Antoine ScuccimarraIt 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:
- $year => [
- $month =>[
- 'title' => $title
- 'slug' => $slug ]
- ]
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:
- foreach($array as $year => $months)
- // output $year and the corresponding HTML for the collapse panels
- foreach($months as $month => $posts)
- // output $month and the HTML for the collapse panels
- foreach($posts as $post)
- // output $post['slug] and $post['title']
Simple, clean and easy! Much simpler than the other ways to do this I found online.
Libellés: coding