Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need a way to make tables on the fly and I came accross Calender::Simple which looks like it would do the trick.

Using the example on their page worked great except it doesn't show how to print them in HTML tables so the numbers and days align properly.

Below you can see the attempt I made and applying a table for the results but it doesn't work. Can someone repair my mistake(s) and let me know what I did wrong?

Also, I need to find a way that I can make each cell background of the table a certain color depending on the day of the week. I have a renting business for cabins and I want to print out the months in a calander format and colorcode the days according to different values (rented out already, open, closed, etc).

I know DB_File and SDBM_file but not MySQL and this project needs to be completed by the middle of December so it can be released to our users by the new year. Any ideas on how I can print the table based on results from a DB_File database if I store them it in such a way like:

key = year-month , value = day::(either rented, open or closed) 2005-jan => 11::rented 2005-jan => 12::rented 2005-jan => 13::open
In this example we could say that anything "rented" would have a red background, everything "open" would be blue.
use Calendar::Simple; print header, start_html("test"); my @months = qw(January February March April May June July August September October November December); my $mon = shift || (localtime)[4] + 1; my $yr = shift || ((localtime)[5] + 1900); my @month = calendar($mon, $yr); print qq(<table width="297" border="1"> <tr>); print "<td>Su</td> <td>Mo</td> <td>Tu</td> <td>We</td> <td>Th</td> < +td>Fr</td> <td>Sa</td></tr><tr>"; foreach (@month) { print map { "<td>$_</td>" ? sprintf "%2d ", "$_" : '&nbsp;&nbsp;&n +bsp;' } @$_; print "<tr>"; } print "</tr></table>";

20041129 Edit by ysth: change title from Creating tables

Replies are listed 'Best First'.
Re: Creating HTML tables
by jZed (Prior) on Nov 29, 2004 at 03:14 UTC
    Others have given you suggestions about creating the actual HTML tables. I'll just say that, if you have any control over the data itself, do yourself a giant favor and store it with the full date as the key. The way you have it with the year-month as the key, you will never have unique keys and any value of storing them in a DB_File (or any other hashed system) will be lost. If instead of having "2005-jan" as the key and "11-rented" as the value, have "2005-jan-11" as the key and "rented" as the value. That will give you unique keys and it will also eliminate the need to parse the serialized value. Even better, store the dates using numerical months as yyyy-mm-dd e.g. "2005-01-11" so you can order the dates properly. If you know SQL at all, you'd be best off using SQLite or MySQL or using DBD::DBM to keep using DB_File but with a DBI/SQL frontend.
Re: Creating HTML tables
by NetWallah (Canon) on Nov 29, 2004 at 01:54 UTC
    It sounds like you need HTML::CalendarMonth,
    "HTML::CalendarMonth - Perl extension for generating and manipulating HTML calendar months"

    "Multiple cells of the calendar can be manipulated as if they were a single element. For instance, $c->item(15)->attr(bgcolor => 'cyan') would alter the background color of the cell representing the 15th. By the same token, $c->item(15, 16, 17, 23)->attr(bgcolor => 'cyan') would do the same thing for all cells containing the item symbols passed to the item() method."

    I have not personally used this module, but it sounds like it would do what you need.

    update: HTML::CalendarMonthSimple seems even better suited to meet your requirements.
    "HTML::CalendarMonthSimple is a Perl module for generating, manipulating, and printing a HTML calendar grid for a specified month. It is intended as a faster and easier-to-use alternative to HTML::CalendarMonth."

        ...each is assigned his own private delusion but he cannot see the baggage on his own back.

Re: Creating HTML tables
by erix (Prior) on Nov 28, 2004 at 22:41 UTC

    Here is my try for those tables, once in table-tags, once in pre-tags:

    #!/usr/bin/perl -w use strict; use CGI; use Calendar::Simple; my $rqry = new CGI(); print CGI::header(); my @months = qw(January February March April May June July August September October November December); my $mon = shift || (localtime)[4] + 1; my $yr = shift || ((localtime)[5] + 1900); my @month = calendar($mon, $yr); print '<html><body>'; print "$months[$mon -1] $yr\n\n"; print '<table>'; print '<tr><td>Su</td> <td>Mo</td> <td>Tu</td> <td>We</td>' .'<td>Th</td> <td>Fr</td> <td>Sa</td></tr></tr>'; foreach (@month) { print '<tr>'; print map{"<td>$_</td>"} @$_; print '</tr>'; } print "</table>"; print "<pre>// or with pre-tag:\n"; print "$months[$mon -1] $yr\n"; print "Su Mo Tu We Th Fr Sa\n"; foreach (@month) { print map { $_ ? sprintf("%2d ", $_) : ' ' } @$_; print "\n"; } print '</pre></body></html>';

    update changed a little (so that it works ;)