Your DBMS returns dates in a known form, say "YYYY-MM-DD", but it's not so good at parsing dates, or you're bloody-minded and want to do it in Perl. You want to generate a calendar, a list of days in each month for which you have entries in your DB (that's not as odd as you think: I came up with this developing an app that lets me see which dates my website got hit, for example)
The problem : your DBMS doesn't grok dates so well. You have a bunch of items (say, events) in a table and you want to know, for any given month, on which days those events occurred. Maybe you want to print out a calendar of some sort, and mark the eventful days. How to represent your data perspciously in Perl? The snippet below will give you, at the end, a hash whose keys are months in the form "MM-YYYY", and whose values are references to arrays that hold the days in that month for which you have entries.
# SQL
#SELECT DISTINCT date FROM hit WHERE location_id = $loc_id
# will get a list back in the form 'YYYY-MM-DD'
my %months;
while (my $row = $sth->fetchrow) {
my ($year, $month, $day) = split '-', $row;
push @{$months{"$month-$year"}}, $day;
}
# cheap print routine to illustrate access to the hash
foreach my $month (keys %months) {
print "For the month of $month, we have events on the following da
+ys :\n";
print "$_ " foreach (@{$months{$month}});
print "\n";
}
Use a module such as HTML::CalendarMonth, and you can quickly create a pretty HTML calendar which you can populate with links, to allow users to see which events occurred on which days.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.