Category Archives: CodeIgniter

Zip code library in CodeIgniter

Recently, a client wanted a member search for his website that included a search by zip code. The ‘easy’ solution would be to implement the search as a LIKE statement in SQL, but the solution is inaccurate, and I like the ‘good’ solutions over the ‘easy’ ones. I looked for a CodeIgniter library that would give me the functionality for which I wanted to implement, but no such thing existed. Then, I came across Micah Carrick’s native PHP library which gave the exact functionality I was looking for. I did end up using his library, but not without modification: I’ve ported the native library to a CodeIgniter library. This is where you can get it.

Read more »

Creating a simple, extensible CodeIgniter authentication library

While it is true that there are a whole slew of third party CodeIgniter authentication systems, there are a few compelling reasons to write your own. In my own experience, third party authentication systems are too feature-rich and complex for a particular application or they are not currently being supported, which means they may not work with newer versions of CI or may have outstanding functional and security bugs. Instead of tackling a new codebase to fight these issues, you may be better off rolling your own solution. This tutorial is for those of you who may need some help starting out.
Read more »

External anchors in CodeIgniter

When using CodeIgniter, I almost never use bare anchor elements to create links. Instead, I use the anchor(“controller/function”, “text”) function in the url helper. At first glance, however, it appears that you cannot use the anchor() function to link to external urls and thus have to write the anchor html element yourself.

This is not true. Instead of using the bare html element, I decided that I would extend the helper to include the functionality I wanted. When I looked into the helper, though, it appears that the CodeIgniter team had already done it! However, as of CI 1.7.2, the functionality is undocumented. Luckily, it is easy to do.

<?php echo anchor("http://www.codeigniter.com", "CodeIgniter"); ?>

Read more »

Update any index in CodeIgniter Cart class

If you use the Cart class that was introduced in CodeIgniter 1.7.2 very often, you’ll notice that it has some shortcomings. In particular, the update() method will only update the quantity of the item in the cart; it will not update other indexes.

This is troublesome because sometimes you may want to update the price, for example, if the quantity goes above a certain threshold. Suppose you want to give a 10% discount on an item to anyone who orders 10 or more of that item. As it stands, you would have to remove the item from the cart, calculate the discount for the item, and re-add the item with a different price. I have also ran into the problem when I added a flag index to items in the cart to which I needed to do additional processing during checking depending on its status. I’ve found in both cases that the better solution is to extend the cart class. Here is how we can do it.

Read more »