The Archive List on the Right Here

Wednesday 19 October 2016

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:

  • $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. 

Labels: coding
No comments

First Post

Thursday 16 June 2016

I don't really have much to say, and I may not ever post here again. I'm just doing this to teach myself Laravel. But I can't just leave this page blank with nothing here, so I figured I should write something. 

Honestly, I don't much care for blogs. I don't really think my opinions are so great that everyone needs to read them and I certainly don't want to read everyone else's opinions about everything. I used to keep a blog on blogger where I would post random things I found, but I haven't updated it since 2009. 

But writing a little blog module seemed like a good way to learn so I made this. And it would be really silly if it was just a blank page with nothing here. 

Labels: test
4 comments

Archives