DerekAllard.com

Checking CodeIgniter out of the Subversion Repository

Note: the specific subversion information in this post is now out of date. Please refer to the CodeIgniter Downloads page for the latest information.

If you’ve been following the CodeIgniter community, then you’ll know that some time ago, we made an subversion (SVN) repository available. Subversion is a version control system that we use internally to be sure we’re all working from the same page. The SVN is publicly available, and is committed to by 4 of the fine folks at EllisLab.  I’ve referred to it before, but I’ve never really talked about how to use it. Recently, there’s been some people interested in getting the latest and greatest CodeIgniter changes pre-release - and heck, why not, as there’s some fine work in there.  This post will talk about how you can use the SVN to keep up with the latest CodeIgniter changes.

SVNX screenshotFirst of all, the standard disclaimer: we make great efforts to be sure that the code in the repository is bug free and functioning, but as is the case with all “bleeding edge” releases, from time to time things may slip in there, so I don’t recommend you use it in a “mission critical” environment.

So, how to use it?  If you are a Mac user, there are 2 pretty nice graphical interfaces.  SCPlugin gets the most attention, but I really like SVNX

In windows?  Go for Tortoise SVN.  Integrates with Windows Explorer and has probably the most intuitive interface I’ve ever worked with.  In any event, pick a client ;)

Now create a folder on your computer somewhere, and rightclick to set up a new repository.  As your destination, choose “http://dev.ellislab.com/svn/CodeIgniter/trunk” which is where we keep our stuff.  Now your goto command is “update” and “show log”.  Update gets you the latest files, and “log” let’s you see what changed.  Here’s an example of the log file from today.

SVNX showing a log

Notice that most times when we check something in we make a comment? Sometimes a change is so minor that we don’t bother, but in general it’ll help you stay on top of what is new in the repository.

Changes tend to come in fits and spurts.  You might see nothing for two weeks, and then a dozen changes in two days.  In general, anything particularly noteworthy will be discussed here (on this blog), so you don’t need to check it every day, but you might want to keep an eye on future changes yourself.

Welcome to the cutting edge! ;)

Setting up “Live Look” in ExpressionEngine

live look in ExpressionEngineA few days ago, I wrote about the release of ExpressionEngine 1.6.1.  In it, I highlighted the “live look” feature, as a great new addition.  Live look allows you to preview a post within a template, rather than previewing only isolated content.  So you can see what your content will look like inside your template.  This is great if you have a future entry, or a post that you are leaving “closed” until you have a chance to finish it up.

But there has been a bit of confusion about how live look should work.  Essentially, all it does is load your entry into a template, so if you have a future dated entry, or a closed entry, it won’t be visible by default.  The solution?  Create a “preview” template, and take advantage of the status and show_future_entries parameters.  Essentially, make it look like this

{exp:weblog:entries weblog="default_site" status="open|closed" show_future_entries="yes"

I’d further recommend that you take advantage of Template Access Restriction in your preview template.  Without it, anyone who guesses your preview template will be able to see upcoming or closed content.  Probably a good idea to restrict it to the same member groups as your authors.

Happy ExpressionEngineering!

Ordering Database results by “random” in CodeIgniter

A recent CodeIgniter bug report had got me looking into the depths of the database results functions of the framework.  Essentially, the orderby() function of CI’s Active Record says that you can sort by ASC (ascending), DESC (descending) or RAND().  Imagine this:

$query "SELECT * FROM table ORDER BY RAND()"

Anyone familiar with PHP probably looks and that and thinks of the native PHP rand() function.  This is pretty neat actually, and I’d never really thought about randomly ordering things.  But on further investigation, it became clear that this code wasn’t as nice as it initially seemed.  Firstly, its non-standard SQL, reducing its portability greatly.  While MySQL uses RAND(), other databases have their own way of doing things:

So then, how would one randomly order their database results?  Read on for my solution…

Giving your Helpers a little Help

CodeIgniter is one of the most flexible, powerful, unobtrusive frameworks you could choose to use. It’s so useful in many ways, including pre-existing convenience libraries (I can’t live without the email library anymore), plugins (for handy little tools such as javascript calendars and CAPTCHA creation) and the appropriately named “helpers”.

In the world of CodeIgniter libraries, plugins and helpers, the libraries are Rock Stars. They get most of your attention, they do most of the heavy lifting. They date super-models, party in Europe, and return on Monday to keep your application humming along (sometimes Tuesday… you know those darned unreliable rock stars). They can be extended, overwritten, and you can create your own, wholly new libraries just for your application. Wow. No wonder super-models want to date them.

One of the most handy aspects of libraries, is that you can create a libraries folder in system/application/libraries, and leave the existing CodeIgniter libraries untouched. This makes upgrading to newer versions of CI a breeze, as you never need to worry about going in and re-implementing all the changes you made to a library.  From the userguide:

As an added bonus, CodeIgniter permits your libraries to extend native classes if you simply need to add some functionality to an existing library. Or you can even replace native libraries just by placing identically named versions in your application/libraries folder.

And while libraries are doing all this work, we’re left with the humble, but infinitely handy helper. Here’s what the userguide has to say about helpers.

Helpers, as the name suggests, help you with tasks. Each helper file is simply a collection of functions in a particular category.

Makes them sound like one-trick ponies; but they are not. Helpers, while not quite as flexible as libraries, are tremendously useful. They are written procedurally, instead of object oriented (as the rest of CodeIgniter is). And while they can’t be extended as libraries can, a little known fact is that they can be overwritten. Better yet, is the method in which you’d override them. Much like libraries, the trick lies in creating a folder underneath system/application for helpers. Then, when CodeIgniter loads helpers, it looks first in your application/helpers folder, and only if that folder can’t be found does it then fall back on the default system/helpers folder. So you can add your own helpers, or overwrite existing ones.

Here’s a case study. In BambooInvoice I use the date helper’s timespan() function to determine exactly how overdue an invoice might be. My problem is that the default timespan() function keeps tabs down to the second of the span of time. When tracking invoices, I don’t care how many hours, minutes and seconds have passed, only how many days and weeks (or months if you have a really bad-paying client ;)) So for use in Bamboo, I want that helpers default output changed. The solution, to remove or comment out those lines of the helper.

But the problem with modifying the system helper is that in the next upgrade, that helper gets overwritten again. Since I use BambooInvoice as part of my testing codebase for vetting new CodeIgniter code, I tend to overwrite these files a lot. This then required carefully keeping track of what helpers were modified, and introduced some other problems. A preferable approach would be to simply make a copy of system/helpers/date_helper.php in application/helpers. Now, the application helper will be loaded preferentially, and I can modify to my hearts content without ever fearing I’ll accidentally overwrite my changes.

Application based helpers are a handy feature indeed. There have been discussions about changing the procedural structure of helpers to allow for extending and over-riding, but for now, the simplicity of helper functions are preserved, and you can easily maintain your own modified versions.

How are you using CodeIgniter helpers? If you’ve modified any of the base helpers, or created your own, I’d love to hear about it here.

Using PHP for random, non-repeating numbers

Need a series of non-repeating but random numbers? For example, random images from a gallery or portfolio screenshots? Let me save you some time:

$numbers range($min$max);
shuffle($numbers);

$random_1 $numbers[0]// first random number
$random_2 $numbers[1]// next random, non repeating
$random_3 $numbers[2]// next random, non repeating 

Normally I wouldn’t blog about something like that, except that I just spent 20 minutes using rand() to generate a random number, then saved it into an array, then ran an in_array() to look for it, and start the process over.

It wasn’t until I was about 15 minutes into it that I thought to myself… “What on Earth am I doing?”.  Then stubbornness kicked in and I tried to finish my monstrosity, then I finally re-evaluated my strategy.

Funny, because I’ve never used shuffle() before for anything, so my brain could only think of commonly used array functions like sort() and asort() (and of course, your favourite and mine… array_intersect_uassoc() - which I freely admit to only just now looking up to discover it exists).

Why are we sometimes driven to code the most complex solution possible, and worse, even when we know it is utterly involved and inelegant, we still drive to make it work, “just to see it working”?

Changing MySQL default character sets to UTF-8

With BambooInvoice 0.8 about to be released, and the big new feature being internationalization, this has brought to light a few issues with character sets, particularly as they apply to the database.  It seems that MySQL, in its default configuration, and especially with popular installers such as XAMPP, MAMP and WinLamp, usually sets the default character set as “latin-1”.  For English, this is a complete non-issue, and things work as they should, however if you try to set other languages, characters such as ö, ä, and ï (as well as dozens of others) tend to load to the screen as garbage.  This is often compounded by the fact that the webpage itself might be in a character set such as UTF-8, which is very “international friendly”, and so things appear to work on the “front end” but as soon as data goes into a database and back out… its borked.

The solution of course is to simply use UTF-8 as the character set for your database.  The problem though, is that many of us have legacy data in MySQL, and as mentioned above, the data started its life as latin-1.  This is particularly true of BambooInvoice, as the installer I set up did not specificy any character set, so nearly everyone’s databases (my own included) is in latin-1.

I’ve found 2 good ways of converting an existing database from latin-1 to UTF-8.  Here’s how you can convert your data…

alternating comment styles

I've been asked how I acheive the alternating comment styles in my blog. When this blog was custom built on CodeIgniter, I used alternator() in the string helper. It looked like this:

<?php foreach ($post_comments->result() as $comment): ?>     <div class="comment<?= alternator(' even', ' odd');?>        <p><?php         if ($comment->comment_author_website) {             echo anchor ($comment->comment_author_website, $comment->comment_author_name);         } else {             echo $comment->comment_author_name;         }         ?> wrote on <?= date ('F jS, Y @ G:i', $comment->comment_date);?></p>         <?= $comment->comment_body;?>     </div> <?php endforeach; ?>

When I switched my blog over to ExpressionEngine a few months ago, I decided to change my strategy a bit, and use the tools EE makes available for me. Specificly, the {switch} tag for comments.

{exp:comment:entries sort="asc"}     <div class="{switch="even|odd"}">         <p>{url_as_author} wrote on {comment_date format="%F %d<sup>%S</sup>, %Y @ %G:%i"}</p>         {comment}     </div> {/exp:comment:entries}

I find it ever bit as intuitive as pure PHP, and I love the convenience shortcuts like {url_as_author} (Hyperlink pointing to the URL (if it exists) with the author name as the link title. If the URL does not exist simply the name is returned).

I still have all the legacy code (of course) from the custom written blog app, and while I don't want to release it wholesale, I'd be happy to field any specific questions about any part of it.

Web Application / CodeIgniter face to face course in Toronto

Some of you who know me, know that I roughly split my professional time between development and training.  I’ve been lucky to have some success over the years, and I’ve managed to build up a pretty good rapport with local universities and colleges.  Like any relationship, after a while, your input starts to be highly trusted, and I’m fortunate to find myself in this situation.  It has put me in a position recently to get a new course on the books at area schools, I’ve called it “Building a Web Application: Concept to Completion Workshop”.  Why do you care?  It’s a course on how to build a web application using CodeIgniter and other “web 2.0” technologies.

My vision was to create a course for working web professionals who want to explore the ins and outs of CodeIgniter, professional web 2.0 application development, and/or have a vision for a web application, but don’t know how to make it a reality.  This is not a course to teach you PHP, and javascript - I expect that you already have intermediate knowledge of that - and preferably you’ve built a few things with PHP/JS before, and now want to get ambitious.  I will assume though that you’ve never used CodeIgniter before, and on that front we’ll start at “ground zero”, and quickly build our way up.

We’re going to plan, wireframe, mockup and build a full-on, functional web-application.  I’m not sure what exactly yet, but it’ll be something practical, and not a complex example of “hello world”.  It’ll be data-intensive, and I’ll probably release the final product under the GPL, just like BambooInvoice

The first run is going to be in North Toronto at Seneca College’s Markham Campus.  If I get any interest from around Hamilton, I also have permission to start up a course at McMaster University.

Inspired by Lightbox: Anatomy of a Modal Window

Ugh... that just might be the worst title I've ever written...

For a "ultra-top secret"™ web application I've been working on, I need to take all focus away from the browser screen, and allow/force the user to interact with a window before being able to continue. Commonly, these are called modal windows, and gained some credibility for having practical uses with Lokesh Dhakar's wonderful Lightbox script (incidently, I use a variation of it on this script on DerekAllard.com).

There are many variations of modal windows running around, but I wanted a simple, unanimated "overlay" would would require a user's interaction, so I set about to build my own. The first thing I needed, was an alpha transparent div to sit on top of the whole screen. I stole was inspired by Matthew Pennel's great article on Easy Cross Browser Transparency, and began building from there. The ultimate alpha transparent solution I chose was a pure CSS base.

Not so useless! image_to_text() as a CAPTCHA

This entry is guest-posted by CodeIgniter programmer Alexander "Iksander" Springmeyer, who contacted me about a unique use of my "Most Useless CodeIgniter Helper Ever". I've asked him to write up a few words, that I present here for all. My sincerest thanks to go Alexander for looking for new ways to implement old ideas, and for teaching an the old image2text() dog a new trick!
-Derek

Some time ago I happened across Derek's blog post on the 'useless' image2text helper he had created. Despite all the "it is useless" comments in the entry, I felt it had powerful possibilities offered by its application in my Sentinel re-write and upgrade of the Freak Auth light authentication system (by Daniel "danfreak" Vecchiato). [editors note: if you want to, you can read a very long and detailed account of its evolution.]

I was inspired to use it for a CATPCHA. Here's how did I do it?