Matthew Pennel (of The Watchmaker Project) just wrote a short and very clear article on Digital-Web Magazine on handling contact forms with PHP entitled “Building a Bulletproof Contact Form with PHP”. If you are new to the topic, or are just looking to get that contact form set up and working, then for sure, give it a read.
Great work Matthew!
OK, these are the laziest of possible posts, but there have been a few things I’ve found in the last few days that I wanted to bring up.
- 7 reasons I switched back to PHP after 2 years on Rails.
- Jim O’Halloran has been blogging about CodeIgniter a bit recently, including Building a Complete CodeIgniter Application (there are several parts to this, so go read it). Nice work Jim!
- EE Design has started a rebuild/refresh/whatever and the site is starting to come alive again. I wish the same could be said of the awesome Jambor-EE, but they haven’t updated in 8 months… ;(
- glider.js is out, and looks sexy as hell.
- More impressive in my mind is ModalBox which I intend on utilizing shortly. Looks great, and is built on Prototype/Scripto, which is my JS library(ies) of choice recently.
- Mobile Web Design was released, and I was fortunate enough to have EllisLab provide me with a copy. There’s some real gold in there, and while I’m not doing anything explicitly mobile at the moment, I know this book will end up being one of those “keep going back to” texts that we all have.
Interesting problem I encountered this morning. I needed to make a case sensitive search through MySQL. So consider this search.
SELECT * FROM people WHERE field LIKE '%Derek%'
This will match
but I only was interested in exact matches. Ah right! MySQL cases are case insensitive by default. Hmm… how to get around this one?
So begin the search… which brought me to a MySQL docs page called Case Sensitivity in Searches. Buried in that page is this little gem.
If you want to make this search case sensitive, make sure that one of the operands has a case sensitive or binary collation.
But then all their examples suggest changing the collation with the “COLLATE” argument. Then something from years gone by clicked in my head, and I remembered forcing the result to binary, which led me to write this query.
SELECT * FROM people WHERE BINARY field LIKE '%Derek%'
Works a charm, so I wanted to document it (for my benefit), and share it on blog (for anyone reading this).
And now that I’ve got my result, this pretty much ends my investigation into this, so if anyone cares to shed more light, or share a “proper” way of doing this, please just comment below!
This entry gets a little nerdy, and into the underlying depths and behaviour of CodeIgniter. If you aren’t interested in the explanations, and just want to know what a good best-practice might be for application development with CodeIgniter, then just skip to the bottom paragraph now. If however you are a glutton for convoluted writing and technical details, then read on!
Recently, I dealt with the idea of PHP error messages and logging in CodeIgniter. Since CodeIgniter subverts PHP errors for its own purposes, I thought I’d write a bit about how it works.
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…
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);
$random_1 = $numbers; // first random number
$random_2 = $numbers; // next random, non repeating
$random_3 = $numbers; // 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”?
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!
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?
For a project I'm currently working on, subscribed users get up to the minute most current information (as outlined by a date field in MySQL). Unsubscribed users get the information still, only it is a day behind.
Due to the way the information is going into the system, there could be multiple entries for one date, and then no entries for a few days, then new, etc. Basically, the information cannot be predicted by the application. Retrieving the most recent date is trivially easy with a MAX command, but there is no "almostMAX" command, so I found myself staring at the screen wondering what the most efficient way to approach this was. Here's my solution:
SELECT * FROM table GROUP BY dateIssued ORDER BY dateIssued DESC LIMIT 1,1
Fortunately for me, LIMIT accepts offsets, and I can specific to grab only the second entry. If you can think of a more elegant way to do this (or heck, if you just want to comment), please leave a note below.
Have I written the most useless helper Code Igniter will ever see? I think so.
It takes an arbitrary image and arbitrary text and builds an image out of them.
becomes (full size)
Which really doesn't do it justice until you see it full size, or run the demo. Here's a zoom.
- Completely impractical? CHECK!
- Chew up server and browser resources? CHECK!
- Fun to take images and overlay completely stupid slogans and sayings? BIG CHECK!
I should note that the helper handles colours just fine. Here's the rendered robot (2.7MB... be kind)
I proudly present the most useless helper ever written! The (useless) helper is available for download, and I've appropriately named it "useless_fun_helper". Run it just like you'd run any other helper.
$txt = 'Code Igniter is an Open Source Web Application Framework that makes writing kick-ass PHP programs simple as apple pie';
$img = 'http://yourserver/ci_logo.jpg';
echo image_to_text($img, $txt); // typically you'd pass that into a view... I know... no hate mail ok
So this is my formal entry into the most useless helper of all time. Any runner-ups?
Hmm... lools like my love affair with SQL related posts continues.
MySQL has posted an excellent resource posted called Top 10 SQL Performance Tips. Although, it is kind of a misleading name, since its a wiki, and everybody is able to post. Currently it's up to 84 tips, most of which look really great. Here is a small sampling...