the sun's not yellow it is chicken

Category: Work (Page 1 of 8)

$this->lang /* FAIL */

Note to self: when dealing with a multi-language site, in a CodeIgniter extended class constructor, do not attempt this:

protected $lang;
function __construct() {
$this->lang = $this->uri->segment(1);

Instead you will want to do this:

	protected $language;
	function __construct() {
		$this->language = $this->uri->segment(1);

See what I did there? ‘lang’ to ‘language’.

As it happens $this->lang, when in a CI constructor, will name-conflict with any native CI system library that needs to tap into something similar to $CI =& get_instance(), with a subsequent $CI->load->lang('some_library') call.

Thank me not.

CodeIgniter’s Cart Class With Accented Characters

This has bit me in the ass one too many times, so I thought I’d do a quick write-up of how I deal with CodeIgniter‘s otherwise wonderful Cart class and its refusal to accept accented “foreign” characters.

Important: The following assumes you are using PHP5+ and CI 2.0 or newer.

The problem

You have your product pages all nicely set up with an “Add to Basket” button. When that button gets clicked, via some jQuery $.get magic in my set-up, the product and its specifications gets added to the Cart object. If however your product’s name has any accents in it (e.g. “Verre à Bière”), CodeIgniter will by default silently refuse to add that product to the cart.

The solution

Create a file in application/libraries and call it “MY_cart.php”. In that file, paste the following code:

< ?php 
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Cart extends CI_Cart {
    function __construct() {
        $this->product_name_rules = '\d\D';

This will override CodeIgniter’s default set of allowed characters, and extend it with whatever accent you can think of. If you’re not using Javascript to add items to the cart, that should do the trick and you can stop reading now.

However, as mentioned, I’m using jQuery’s $.get method to add items to the cart, so that the page doesn’t have to reload each time an item gets added (it’s what the cool kids refer to as “Ajax”). Here’s an example of the script I’m using for this:

			id: $('input[name=id]').val(), 
			name: $('#name').val(), 
	   		qty: $('#qty').val(),
		       	price_option: $('#price_option').val()
		 function(data) {	
			}, 'json'
		); // end get
		return false;   

Notice that I’m passing on the value of a hidden formfield with id “name” to this function. Now, here’s the pitfall: Javascript will convert accented characters to their equivalent Unicode string (while, for legacy reasons, I’m having to use the iso-8859-1 charset). As a result, CodeIgniter will gladly add my item to its cart, but since it’s passed through Javascript, the accented characters come out as “gibberish”, once you have to display them in your cart view.

The solution here is to pass the regular product name (accents and all) to the jQuery function, and then do the following in the php script which actually adds the item to the cart. Instead of something like:

$data['name'] = $this->input->get('name');
//proceed to insert in cart

Do this:

//run the name variable through the ascii_to_entities() function
$data['name'] = ascii_to_entities($this->input->get('name'));
//proceed to insert in cart

Note that for this you will have to have CodeIgniter’s Text helper loaded.

Almost there

What we’ve accomplished so far: CodeIgniter’s Cart class accepts our accented product names, and although we’ve passed the names through Javascript, the correct characters still show up within the Cart object. But how about displaying items in a cart overview? If you link back each item to its product page, you will have to construct that link such that it re-instates the ascii characters instead of their respective entities:


Note that I’m using sanitize_title_dashes() (see the WordPress core) as a method to obtain URL’s which are restricted to an “a-z0-9” realm of characters – thereby omitting any characters that had previously been turned into entities! Using entities_to_ascii() allows them back into the url. Phew.

Wrapping up

While it may look a bit messy, the above solved the problem for me. I love CodeIgniter very much, and it’s sped up my webdevelopment significantly. It being mildly US-centric though in the way it handles accented characters in URL’s and Cart processing leaves a bit to be desired. The up side is that through its extensibility, you can make things work – allbeit with a bit of angry fist shaking. Having these workarounds present out of the box from within CodeIgniter would be nice. But that’s another can of worms…

The Rage & The Fury

While all of us Zappa freaks await the official release of FZ conducting Edgard Varèse‘s “The Rage & The Fury“, I had some fun imagining what the cd-cover might look like. How’s this:

The Rage & The Fury

Frank Zappa on Varèse:

[…] the thing that is fantastic about what he wrote for normal instruments is that he got sounds out of them that nobody had dreamed of before. For instance, “Deserts”, which is probably the starkest of the pieces in terms of the way they deal with the raw material, there’re special overblown chords that produce difference tones, which you wouldn’t be able to get any other way – you know what I’m talking about? If you take two intervals and play them very loudly on a woodwind instrument – for instance, this one spot where two piccolos are playing either a Major second or minor second apart, very high octave – when you blow it real hard you hear a third note that’s not there. To know in advance what’s going to come out and to plan your composition to achieve effects like that was something that people just hadn’t thought of doing before.

Feelin’ Groovy!

Don’t let that title fool you: I’ve been so busy at work recently, I hardly ever get to update that other site of mine anymore — let alone this one. Did manage to squeeze in a little design job though, with the executive help of Gabriela. No money involved, just helping out a good friend. Hope he likes it!

Click to enlarge:

Poster for a barber-shop singing camp aimed at (American!) teenagers. Gaby was in charge of the hairdo’s and the typography whilst I spent a Saturday afternoon drawing open mouths of various persuasions. Fun!

My favourite is the blue-scalped Goth, though it must be said the dorky “Will Someone Release Me From These Dumbass Glasses” kid has a certain je ne sais quoi as well.

« Older posts

© 2024

Theme by Anders NorenUp ↑