Recursive Collatz conjecture in PHP

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. So, to kill some time, I wrote a recursive version in PHP.  Here it is:

function collatz($num)
{   
    if($num == 1)
    {
        echo "<p style='color: red'>" . $num . "</p>\n";
        return;
    }

    if($num % 2 == 0) //even
    {
        echo "<p style='color: green'>" . $num . "</p>\n";
        return collatz($num / 2);
    }
    else
    {
        echo "<p style='color: red'>" . $num . "</p>\n";
        return collatz(3 * $num + 1);
    }
}

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]

Forwarding X11 over SSH from Linux to Windows

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 More]