Author Archives: Patrick McGoohan

Validation on optional fields

CakePHP’s allowEmpty option makes performing validation on optional fields remarkably simple, even ones with multiple validation rules. Let’s assume we want to collect a user’s zip code. We configure an entry in our model’s $validate array, taking advantage of CakePHP’s built-in numeric validation: public $validate = array( ‘zip’ => array( ‘rule’ => ‘numeric’, ‘message’ => […]

using a helper to wrap injected markup

CakePHP’s FormHelper comes with several, intuitively-named options for injecting markup into a form element. The option I use most frequently is “after”, a very simple way to append an explanatory note after a particular form element. For example, as part of my “Venues” view let’s assume I have an input field for a phone number […]

The Gzip Basics

Gzip is data compression software used to compress a file into an archive, as well as expand an already-compressed archive back into a file. One common use for gzip is to compress a tarball. [One could use Zip both to collapse and compress a directory, instead of tar / gzip, but the compression isn’t as […]

Breaking out of the ZIP-CPGZ loop

I receive a lot of compressed archives, from a wide variety of different users. Those who prefer the command-line (usually other developers) will TAR the directory then GZIP the TAR. Most users are more comfortable with a GUI for archiving and wind up sending a ZIP file. With those it’s usually a quick double-click and […]

Opening a PDF in Illustrator without the fonts

When producing the final, print-ready PDF from an Illustrator file, it’s a thoughtful gesture to convert all the text in the file to outlines (Type/Create Outlines) prior to saving it. Otherwise, when the next user opens the file in Illustrator — say, in the print shop as they ready the file for production — they’ll […]

Multiple background images with CSS3

Assigning multiple background images to a single element is as complicated as creating a comma-separated list. That is to say, not very. To wit: <style> h2 { background: url(star.png), url(star.png); background-position: left center, right center; background-repeat: no-repeat; } </style> All H2 elements that follow will have “star.png” to either side of the text. Like so: […]

Easy alternating styles with Nth Child

When displaying large groups of data, especially wide sets in which the user’s eye has to travel from one side of the screen to the other, it’s always a good idea to having a slight variation between alternating rows, for ease of reading. Like so: odd even odd even odd even odd <rant>Astute observers will […]

Detecting iPad

Need to tell if a user is browsing your site on an iPad? Here’s how to do so via PHP: <?php define(‘IS_IPAD’, (bool)strpos($_SERVER[‘HTTP_USER_AGENT’],’iPad’)); ?> One can also do it via Javascript: <script> var IS_IPAD = navigator.userAgent.match(/iPad/i) != null; </script> However, I prefer making the determination server-side, as there’s a (small) number of users who browse […]

Disabling platform-specific default styles

Both WebKit and Mozilla browsers have introduced default styles for certain specific element types, in an effort to impose some degree of design consistency across the platform. You might be tempted to assume a well-placed !important will override those default styles but not necessarily so. Depending on the type (like “search” for example) only certain […]

Using GLOB to retrieve the filtered contents of a directory

I am not a demanding man. I have a directory of images; I want a list of the images inside. The usual drill is to create a directory handle and loop through, returning everything that’s found, then close the handle, like so: <?php $dir = “/var/www/vhosts/example.com/images/”; $handle = opendir($dir); while (false !== ($file = readdir($handle))) […]