Building Grids with PHP, the easy way

In my years as a developer, I have found myself building grids in html, always with a different number of elements per row. The first few times I solved this problem, I used the modulus (%) divided by the number of elements needed for the row, with an if statement to see if the row container should be closed or not.  This especially sucks if you are doing something recursively, so why not do it in a way that is easy to read, doesn’t require css hacks, and can take advantage of the row-fluid and span classes in bootstrap. Use array_chunk.

Isn’t that much easier to read?

Beginning NodeJS for PHP Devs: Introduction

I currently write a lot of PHP on the backend, and JavaScript on the front end. In the last couple of years getting increasingly into frontend javascript frameworks like BackBoneJS.

The PHP backend/BackBone frontend pattern works really well, building your logic and data into a PHP api which serves JSON to your JavaScript frontend is nice and solid. That shift led to writing more and more javascript to manipulate this data I was getting from PHP. And more and more code switching as I toggled my brain back and forth from PHP syntax to JavaScript. I often thought about what it would be like to jump in and write a nodejs app but didn’t really have the time.

After finishing Gary Vaynerchuk’s latest book, Jab, Jab, Right Hook. I was thinking about really starting to engage customers socially. The first attempt at this strategy was to get the conversation started on a stagnant Facebook Page and Twitter feed. My partner and I decided to ask a question of our current customers in the form of a facebook post, and a tweet and offering a prize to one of the folks who responded. We kicked off the query with an email, and a good number of people were gracious enough to respond. The time came to actually choose a winner, and being the engineer that I am, I couldn’t bring myself to do it manually.

Surely, there must be a tool out there for inputing a link to a social post, and choosing a random winner from the responders, right? Not that I found in a cursory google search. There are a few services that are paid and go far beyond what I wanted to do, which was take a link from a facebook post, a link from a tweet, combine the participants, dedupe them and choose a winner.

About the same time, this tutorial post on Easy Node Authentication came up in my twitter feed. Curiosity got the best of me, and I started the tutorial. Inside of 40 minutes, I had a local nodejs app that your could register with an email, connect the account to facebook, twitter, and google plus, with the data stored in a mongodb database. I knew I needed to build an app with this technology.

1 part idea, 1 part problem, 1 part technology, add inspiration and shake the crap out of it!

Six evenings later, I shipped my first nodejs app using the express framework, a mongodb database, that sends emails using mandrill, and sits on my shared webfaction hosting account, polish is provided by FlatUI. It’s an app that solves my problem, and hopefully the problem of a few other people.

Presenting the “Social Drawing” App, which takes a facebook post url, a tweet url, gathers the participants, dedupes and chooses a random winner. Saving the results so each combination can only be run once, and will email you a link to the results so you don’t forget.

It was a blast to write, and the first “toy project” (of dozens), that I have actually seen through to production. I intend to outline the actual build in the next few blog posts so stay tuned. Here’s part 2!

MAMP Pro /etc/hosts file problems – Simplified

Lately I have been using a lot of host aliases for testing sites locally using my MAMP pro install. Adding local sites that answer to their actual site names is amazing for a development environment, especially if you find yourself testing javascript based apis that require oauth. Like the facebook or twitter apis.

MAMP Pro does have a problem however with not always turning off the entries in the /etc/hosts files when you stop the servers, so you have problems accessing the real site. Fixing this requires opening the hosts file manually and commenting or uncommenting the desired entries. A tedious pain.

So I decided to write a quick shell script that would allow me to reset the hosts file with a quick terminal command. See the one line install command and usage below.

Shell script for cleaning/resetting /etc/hosts files messed up by MAMP Pro — Gist.