Magento 1.7: The Beginning

In spinning up a new store for keepandcastle.com I decided to go with Magento (the free community edition) because it seems to be an extremely flexible base to build on from a design perspective. I have worked with Zen Cart for about 6 years and enjoyed the speed at which you could spin up a store (you could have a fully running and functional store within 6 hours, if you had a payment processor ready to go) but have been frustrated by difficulties in setting up custom templates or adding functionality because either of these activities seemed to require (sometimes extensive) modifications of core files. Version 2.0 of Zen Cart has been promised for a few years and seems like it would address those issues. However, I wanted to try out a new platform so I chose Magento.

Installing Magento was easy enough, I need a database, a user for that database, and needed to allow write access to a few folders during the installation. It took about 15 minutes all told. This is where I diverged from the simple path, however, since I didn’t want our site to look like very other site out there. This meant creating a new design package and theme. In Zen Cart this was easy (if my theme doesn’t have a needed file, the default is used). In Magento the process took over half an hour and I ended up with absolutely no styling. Since my goal was to be able to style the site myself from the ground up, I celebrated!

I used this guide to walk through the basics, and I’ll post later on how easy it was to add my own styles and functionality. However, at the moment, I have an non-functioning but entirely unstyled site to work with. We’ll see how it goes from here!

Splitting a String in MySQL

Sometimes you’ll end up working with a MySQL field that contains multiple pieces of information. This can be a delimited list, or, in my case, nested categories. I’ve been using a single field to contain information on the genre of a book which is often a subcategory of a larger genre. For example, an Historical Romantic Fiction book would have the genre “Fiction/Romance/Historical” in my genre field. When giving our employees a list of genres to select when adding product information to a book, I don’t want to just give them all 3,000 sub-genres, instead I want to give them level-by-level genres to choose from. i.e. “Select from Nonfiction or Fiction” and if they select Fiction: “Select from Romance, Fantasy, or Classic” and if they select Romance: “Select from Historical or Contemporary” etc.

In doing so I need to pull out, level by level, the existing genres. To do this for the first level I use the following query:

SELECT SUBSTRING_INDEX(genre,"/",1), COUNT(eAN) FROM `inventory_product_info` GROUP BY SUBSTRING_INDEX(genre,"/",1) ORDER BY COUNT(eAN) DESC

I’m using the SUBSTRING_INDEX() MySQL function to get a substring from the beginning of a string (in this case the contents of the genre field) until the 1st occurrence of the “/” character. I’m both returning the results of the SUBSTRING_INDEX() and grouping by it to get a count of all products which use that top-level genre.

Using HTML5 Audio Tags: Not as Easy as You Might Think

Firefox Default Audio Player

The HTML <audio> tag is a relative newcomer amongst the HTML tags we use regularly. It was introduced to help correct what seemed to many, myself included, like a major oversight of the modern web: native audio support by browsers. A wide variety of image formats have been available via the <img> tag since soon after HTML was initially developed, but digital audio and video were initially too heavy to be used in HTML pages intended for the public. However, today we finally have both an <audio> tag and a <video> tag. Problem solved, right? Nope. Unfortunately, there are still a number of bugs to work out, mostly with browser interoperability. The root of this issue is a lack of a standard file format.

But, I’m a modern web developer. I’m not going to use some Flash shiv to get my page playing audio! It took some troubleshooting but here’s how I did it:

First of all, it’s important to know a bit about how the <audio> tag works. It can either stand alone or act as a container. A standalone audio tag would look like this and appear familiar to any web developer:

<audio src="bach_brandenburg_5-1.mp3" controls preload="auto" autobuffer></audio>

You’ll recognize the “src” attribute, of course. The “controls” attribute tells the browser whether it should render controls for the audio element or if that will be handled by the page (i.e. Javascript). The “preload” attribute set to auto tells the browser to automatically preload however much of the audio file it feels is appropriate. “Autobuffer” does the same thing as ‘preload=”auto”‘ but it has been deprecated in favor of preload.

The above tag will show controls for an audio file, buffer it, and allow it to be played. If only it were that easy. This will only work in IE 9+, Chrome 6+, and Safari 5+. In Firefox, Opera (although this may change with the switch to Webkit), or any older browser it will silently fail.

The problem is that neither Firefox nor Opera support MP3 as a file format. In fact, there is currently no single file format which can be played natively by every modern browser. IE, Chrome, and Safari can handle MP3, Chrome, Firefox, Safari, and Opera can handle WAV (but do you really want to use WAV?), and Chrome, Firefox, and Opera can handle OGG. So you’ll need both an MP3 and an OGG copy of every audio file you want to use. I used media.io to convert my MP3s to OGGs.

You might think we’re going to get into Javascript browser sniffing here so that we can serve the correct file but, thankfully, the solution is simpler than that. Here’s the final code I ended up using:

<audio controls preload="auto" autobuffer>
    <source src="bach_brandenburg_5-1.mp3" />
    <source src="bach_brandenburg_5-1.ogg" />
    Unfortunately your browser does not support our audio formats.
</audio>

It turns out that the <audio> tag can be the container for multiple <source> tags each of which can define a different format. The great thing here is that so far in testing, all the browsers I’ve used have simply ignored the formats they couldn’t read and played the one they could. The text is a fallback if the browser fails to render the audio controls (old browsers, mostly).

This worked for me, although the site I used it on doesn’t rely heavily on the audio being available so we were fine with the text fallback, however, if audio is a critical part of what you do you’ll likely need to look into either a Javascript or Flash or hybrid solution. For me this solution was perfect as <audio> support is only getting better.

Why?

I’ve been in web development since 2001 starting with maintaining a relatively small site and, since then, developing many medium to large online properties. During all of this I’ve held off on creating a blog for myself because I didn’t feel I had a lot to say.

I’ve also been in the IT field in a more general sense for about a decade, working with networking, desktop support, application support, and most usefully, developing solutions that allow applications to work together. These are batch files, PHP scripts, Excel Templates, shell scripts, and whatever else is handy. The applications I’ve worked with (besides Windows, Windows Server, and Mac OS) include the usual suspects from Adobe, Microsoft, and the LAMP stack. Additionally, I’ve worked closely with aspects of ROS 2000, Lightspeed POS, Monsoon, and UPS WorldShip.

What thoughts do I have that no one else has had? What unique insights into the world do I have? I’m not sure, but what I realized was that I’m encountering problems others are certainly encountering as well. In addition, I’m creating solutions that others could use. Finally, I realized that I keep having to recreate solutions myself! Why not write them down? If they’re useful to others, so much the better!

I don’t pretend to have the best solution to every problem, but I do have solutions that work. Anyway, I hope what I write is useful.