Nothing But Words

Mike Toppa’s Blog

About | Contact | Archives | Photos | WP Plugins

Introducing Kai’s Candy Company

Kai's Candy CompanyKai’s Candy Company

Kai’s Candy Company

My blog has been quiet recently, as I’ve been focused on creating and launching the site for my new business, Kai’s Candy Company. Our goal with the company is to seek out the most unusual, fun, and tasty candies from around the world, and sell them! We’re starting with Obama and McCain candies that we’ve made especially for the 2008 Presidential campaign. The candies are hand made by artisans in Japan, using traditional kumi ame (rolled candy) techniques.

We also have a Halloween candy poll that’s waiting for your vote! Your votes will help us decide which candy designs to pick for our Halloween candies.

Trailer for the Conclusion of Avatar: The Last Airbender

Last Fall I admitted to my guilty pleasure of watching Avatar with Kai. The first half of the last season ended several months ago, and the series will conclude with episodes starting on July 14. Here’s the trailer for it from Nickelodeon. The dialog they choose for it is cheesier than the previous trailer, but it still looks really promising:

I also stumbled across this interview with M Night Shyamalan, who is directing what will hopefully be the first of three Avatar live action films. Shyamalan has an awfully inconsistent track record with his films so far, veering between brilliant and mind-numbing. But I get the sense from the interview that he genuinely gets what Avatar is really about, so I find that a hopeful sign.

If you’re a person who laments the decline of the Saturday morning kids cartoon genre, then you should really check out Avatar: The Last Airbender. It may not currently be on TV (we’ve not been getting up early), but it’s available on DVD and it’s properly brilliant; beautifully animated and very well written for kids’ entertainment. M Night Shyamalan is currently in pre-production adapting the cartoon into a movie trilogy, so now is really the time to get yourself familiar with it…

“Buddhist and Hindu philosophies run through the stuff,” he [Shyamalan] continues. “When I realised that is what it was, it really drew me as the template for putting storytelling on a new level. There is a kind of thread that connects Star Wars and The Matrix – the first one. That same thread is in this story, about a forgotten belief system, or the illusion of the world now.”

Making an HTML Table Using Arrays of Column Data

Most of the time, if you need to create an HTML table to display data pulled from a database, it’s a fairly straightforward task: the data is organized in rows, and since HTML tables are generated in rows, it’s easy to loop through the rows of data and display them in succession. But sometimes you can have a situation where you want to display the data as columns, which is not something you can easily do in HTML. Here’s an example of how to approach this challenge.

Displayed below is an array of data from a project I’ve been working on. I’ll explain the color coding in a minute. The first element is a sub array of laboratory sample IDs, and the subsequent elements are subarrays indicating which laboratory tests should be run on which samples. (I’ll spare you an explanation of the database structure behind all this, but suffice it to say that pulling out the data with this organization was the most efficient solution).


Array[4] (
    0 => Array[6] (
        0 => 'Sample ID',
        1 => 193,
        2 => 194,
        3 => 195,
        4 => 196,
        5 => 197,
    ),
    1 => Array[4] (
        0 => 'Phospholipids',
        1 => 'Y',
        3 => 'Y',
        5 => 'Y',
    ),
    2 => Array[6] (
        0 => 'Apolipoprotein A-I',
        1 => 'Y',
        2 => 'Y',
        3 => 'Y',
        4 => 'Y',
        5 => 'Y',
    ),
    3 => Array[4] (
        0 => 'Gastrin',
        2 => 'Y',
        3 => 'Y',
        4 => 'Y',
    ),
)

How I ultimately what to display the data is like this:

Sample ID Phospholipids Apolipoprotein A-I Gastrin
193 Y Y  
194   Y Y
195 Y Y Y
196   Y Y
197 Y Y  

To get from here to there, you need to visualize how you want to arrange the data elements. If you use the color coding I assigned to the array indexes above, you can picture the data in rows, like this:

0,0    1,0    2,0    3,0

0,1    1,1    2,1    3,1

etc.

To generate a display of the data in this manner, you need to loop through the arrays in an inside-out manner. You loop through the subarrays to set the rows, and then have a nested loop through the parent array to set the cells. Here’s the PHP code for it:


$content = "<table>\n";

// This loop controls the table rows
// $cols is the name of the parent array
for ($inner=0; $inner < count($cols[0]); $inner++) {
    $content .= "<tr>";

    // This loop generates the table cells
    for ($outer=0; $outer < count($cols); $outer++) {
        // this assumes you want the first row to be a header row
        $tag = ($inner == 0) ? "th" : "td";
        $content .= "<$tag>{$cols[$outer][$inner]}</$tag>";
    }

    $content .= "</tr>\n";
}

$content .= "</table>\n";

// then display or return $content...

The first subarray contains the Sample IDs, so I used it to set the maximum count for the outer loop that controls the HTML table rows (because I can be confident there will never be data beyond the last sample). If you don’t have an analogous subarray, you’ll need to check to see which subarray is longest, and use that to control the count.

You are currently browsing the Nothing But Words blog archives for June, 2008.