Zip code library in CodeIgniter

July 7th, 2010

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, but first you should look at the demo.

Demo

Read the rest of this entry »

“Presto” math trick in Java

June 23rd, 2010

According to Futility Closet, if you start with a 3 digit and place it next to the same number to form a 6 digit number, you can divide the 6 digit number by 7, 11, and then 13 and you will end up with the original 3 digit number and no remainders.  For example, by taking the number 412 and making it 412412 and then doing the divisions, you will end up with 412.  I wrote a small program in Java to test it.

The Code

  1. public class Main
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.         for(int i = 100; i <= 999; i++)
  6.         {
  7.             //get the number, and "double" it
  8.             int number = Integer.parseInt(Integer.toString(i) + Integer.toString(i));
  9.  
  10.             //successively divided by 7, 11, then 13
  11.             System.out.print((i) + "\t");
  12.             System.out.print((number) + "\t");
  13.             System.out.print((number /= 7) + "\t");
  14.             System.out.print((number /= 11) + "\t");
  15.             System.out.print(number /= 13);
  16.  
  17.             //is the result what we expect? (input == output)
  18.             if(i == number)
  19.             {
  20.                 System.out.print("\tCorrect\n");
  21.             }
  22.             else
  23.             {
  24.                 System.out.print("\tIncorrect");
  25.                 break;
  26.             }
  27.         }
  28.     }
  29. }

In this snippet, we loop through all 3 digit numbers (100 to 999) and output the results of each operation in a tab separated column. If the result (output) is the same as the input, we print correct and continue with the loop. Otherwise, we print incorrect and break. Give it a try!

Recursive Collatz conjecture in PHP

May 18th, 2010

I stumbled across a blog post by Brie Gordon about the Collatz conjecture, so naturally I had to Wikipedia it. The simple story is that for any given integer, if it is even you divide it by 2 and if it is odd you multiply it by 3 and add 1. You continue this pattern with the returned numbers, and eventually the number will reach 1.  I’ve heard of it before, but not since one of my earlier computer science classes, so to commemorate the re-discovery I wrote a recursive PHP function to display the progression.  Here is the function:

  1. function collatz($num)
  2. {
  3.     if($num == 1)
  4.     {
  5.         echo "<p style='color: red'>" . $num . "</p>\n";
  6.         return;
  7.     }
  8.  
  9.     if($num % 2 == 0) //even
  10.     {
  11.         echo "<p style='color: green'>" . $num . "</p>\n";
  12.         return collatz($num / 2);
  13.     }
  14.     else
  15.     {
  16.         echo "<p style='color: red'>" . $num . "</p>\n";
  17.         return collatz(3 * $num + 1);
  18.     }
  19. }

View Demo

Creating a simple, extensible CodeIgniter authentication library

May 10th, 2010

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 the rest of this entry »

External anchors in CodeIgniter

May 7th, 2010

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 the rest of this entry »

Update any index in CodeIgniter Cart class

April 14th, 2010

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 the rest of this entry »

jQuery change event on checkbox

April 6th, 2010

A lot of times, you’ll want to call a javascript function when a checkbox is changed from unchecked to check and vice versa.  Up until the release of jQuery 1.4, the obvious way to do it (the jQuery .change() event) did not work properly across all versions of Internet Explorer.  Instead, you had to check for the .click() event.  This was an accessibility problem because it did not fire when a user changed a checkbox with the space bar instead of by clicking.  Well, now we can rejoice.  The following code snippet works exactly how you think it would across browsers, including IE.

<!-- Load jQuery, and the necessary html -->
<script type='text/javascript src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script>
<input type='checkbox' name='my_checkbox' value='true' checked='checked'>
$(document).ready(function(){
  1.     $(":checkbox").change(function(){
  2.         if($(this).attr("checked"))
  3.         {
  4.             //call the function to be fired
  5.             //when your box changes from
  6.             //unchecked to checked
  7.         }
  8.         else
  9.         {
  10.             //call the function to be fired
  11.             //when your box changes from
  12.             //checked to unchecked
  13.         }
  14.     });
  15. });

View Demo

Easy ajax with jQuery and PHP

March 29th, 2010

When I was first learning about web programming, it was difficult to grasp how javascript can interact with PHP.  How do I manipulate data in the database with a client-side language?  The answer, of course, is ajax, and if you’ve never used ajax, now is the time to learn how.

What is ajax?

Ajax is not a singular technology, it is a collection of technologies used for asynchronous communication between the client and server i.e. javascript grabbing data from a PHP script and displaying it on the page without refreshing the page.  This is done through a DOM api called XMLHttpRequest, and I would encourage you to read a more in-depth view on its Wikipedia page.  While this has some SEO implications, it also gives the programmer or designer a way to build rich, dynamic internet applications that don’t require a propriety plugin like Flash or Silverlight.  Now that you have a basic understanding of ajax, lets try it out.

In this tutorial and demonstration, I will take a number from the user with an input box, and grab the row with that ID from the database and display it.  This is not a useful example, but it does demonstrate the principles of moving data back and forth between the client and the server, and how we can display the data.

View the demo

Read the rest of this entry »

Forwarding X11 over SSH from Linux to Windows

March 19th, 2010

For those that are not familiar with Linux, X11 is a part of the graphics system, and that is really all you need to know for this short blog entry.  Below is a video that demonstrates what forwarding x11 over SSH will actually allow you to do: in the video, I run a remote session of Kate (a Linux text editor) and Gwenview (a Linux image viewer) from my Linux machine to my Windows machine.  It’s similar to remote desktop, but instead of the whole desktop, you can use certain applications.  You’ll see what I mean in the video if you’re not following already.

The first step is to configure SSH client and server on your Linux machine.  Instead of repeating what’s been written a thousand times before, I’ll send you to the Ubuntu SSH Server instruction guide .  If you use a different distribution, you can look up it for yourself if you have to, but as far as SSH goes it is pretty much the same wherever you go.

Read the rest of this entry »

Introductions

March 13th, 2010

Thank you for visiting my new blog, the latest addition to my main sites redesign.  First, lets talk about who and what this blog is for.

Topics of Discussion

The main purpose of this blog is two-fold.  Most articles will be technical articles related to programming.  I want to teach people techniques that I think are useful.  Since graduating with a degree in CS, I’ve mostly done web development professionally, and many of the techniques I use I learned from different blogging communities, and this is how I’m going to give back.  Web programming is significantly different from the traditional academic programming I did in college, and I had to approach it with a different mindset.  I’m planning my first few articles to target budding web developers, and maybe ease the bumps in the road that I had as I was learning.

Read the rest of this entry »