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:
- MySQL uses ORDER BY RAND()
- PostgreSQL uses ORDER BY RANDOM()
- Oracle uses a subquery with ORDER BY dbms_random.value
- SQL Server users ORDER BY NEWID()
So then, how would one randomly order their database results? Read on for my solution…
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.
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”?
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…
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.
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.
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.
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?
Just a quick tip for those of you working away in Flash. You've probably noticed that the source fla files get really big, really fast. In fact, Here's a fun little exercise for you.
- Create a new, blank .fla file and save it.
- Note the filesize.
- Add a bunch of sound files to it, some components, a couple symbols... now resave, and note the new file size. It should be bigger (d'uh!)
- Now remove all that crap. Resave, and note the new file size.
Your instincts would tell you that since you removed all that information, that the file should be smaller, but in fact, its the opposite...
I posted earlier about an article that James Nicol had up on this blog. The long awaited (well, long awaited for 6 days) article is now up, Real world apps with CodeIgniter: part II. Clever title!
He delves deeper into the black art of Code Ingiter, and discussing integrating with third party systems, validation, and my favourite understatement of the week.
Before we even went live the clients came back asking for more features...
<sarcasm>What!?! That hardly ever happens!</sarcasm>
At any rate, its a great read, and I hope James keeps writing!