How Truth Tables Ruined My Afternoon

This piece of code was killing me this afternoon:

$DB_ERROR = -1;
$API_ERROR = -2;
$UI_ERROR = -3;

$errorCodes = array($DB_ERROR, $API_ERROR, $UI_ERROR);

$returnCode = TRUE;
$isError = in_array($returnCode, $errorCodes);
print($isError);

I was expecting $isError to be FALSE but it kept turning up as TRUE. I just couldn’t understand it. That is until I read the online docs:

When converting to boolean, the following values are considered FALSE:

* the boolean FALSE itself
* the integer 0 (zero)
* the float 0.0 (zero)
* the empty string, and the string “0″
* an array with zero elements
* an object with zero member variables (PHP 4 only)
* the special type NULL (including unset variables)
* SimpleXML objects created from empty tags

Every other value is considered TRUE (including any resource).

Do you see what I was assuming here? I considered TRUE to be a 1 and FALSE to be everything but a 1. I guess I’m just so used to those truth tables with 1 representing true (thank you very much Discrete Mathematics For Computer Science). But hey you’ve gotta work with whatever the language gives you.

At first I thought about converting $returnCode to an integer before I make that in_array comparison but then after reading more of the online docs I found something cleaner:

$isError = in_array($returnCode, $errorCodes, TRUE);

That extra TRUE checks the types as well as the values.

Three Hours For A Pair Of Square Brackets

The Project has this one particular form with a set of ‘Yes’/'No’ radio buttons and a <textarea> element.

The Client wanted the ‘Yes’ radio button automatically checked whenever a user enters some text in the <textarea> and the ‘No’ radio button to do the same thing when a user erases said text.

Solution: Attach an onChange()* onBlur() event handler on the <textarea>. Everytime the event is fired set the radio buttons based on the <textarea>’s value.

I knew how to do this in plain vanilla Javascript but I’m drinking the JQuery kool aid nowadays so off to figure out how to jquery-select a radio button.

Three hours later I finally figured it out. Here’s a quick summary of those three hours:

And that’s what bugs me about JQuery. It possesses great power but with great power comes great… reading of sh*tloads of blogs tagged with ‘jquery’ in order to try to figure out your JQuery problem.

* Pointless because there’s no onChange() event handler for a <textarea> element

Displaying Zebra Tables In Smarty

How come I’ve never heard of {cycle} before? It turns crap like this:

<table>
{section name = i loop = $items}
   {if $smarty.section.i.index % 2 == 0}
      {assign var = 'rowStyle' value = 'odd'}
   {else}
      {assign var = 'rowStyle' value = 'even'}
   {/if}

   <tr class = "{$rowStyle}">
   ...
   </tr>
{/section}
</table>

Into concise stuff like this:

<table>
{section name = i loop = $items}
   <tr class = "{cycle values = 'odd,even'}">
   ....
   </tr>
{/section}
</table>

Which may not seem like much until you realize that The Designer uses zebra tables everywhere in The Project.