in reply to cgi calander

For a start decide if you are going to use the OO interface to CGI or the functional interface. If you are going to use the functional interface you don't need to create a $cgi object.

I don't see anything in your code that aligns dates (numeric) with day (name). You may find it helps to do this in two passes - generate a table (2d array) of weeks populated with the correct dates (and undefs at start and end for "missing" dates), then loop over the weeks to generate the HTML table. The following may help:

use strict; use warnings; use CGI::Pretty qw(:standard start_table end_table start_Tr end_Tr), ( +-unique_headers); use CGI::Carp qw(fatalsToBrowser); print header(); print start_html (-title => "Calendar sample"); my @weekDays = qw(Sunday Monday Tuesday Wednesday Thursday Friday Satu +rday); my $today = 11; my @weeks = ( [undef, undef, undef, undef, undef, undef, 27], [28, 29, 30, 31, 1, 2, 3], [ 4, 5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23, 24], [25, 26, undef, undef, undef, undef, undef] ); print start_table(); print Tr ({-align=>'CENTER',-valign=>'TOP'}, td (\@weekDays)); for my $week (@weeks) { print start_Tr({-align=>'CENTER',-valign=>'TOP'}); for my $date (@$week) { if (! defined $date) { print td (''); } elsif ($date == $today) { print td(strong($date)); } else { print td ($date); } } print end_Tr(); } print end_html ();

Generates:

Content-Type: text/html; charset=ISO-8859-1 <?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-U +S"><head><title>Spawn Results</title> </head><body> <table> <tr align="CENTER" valign="TOP"> <td>Sunday</td> <td>Monday</td> <td>Tuesday</td> <td>Wednesday</td> <td>Thursday</td> <td>Friday</td> <td>Saturday</td> </tr> <tr align="CENTER" valign="TOP"> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td>27</td> </tr> <tr align="CENTER" valign="TOP"> <td>28</td> <td>29</td> <td>30</td> <td>31</td> <td>1</td> <td>2</td> <td>3</td> </tr> <tr align="CENTER" valign="TOP"> <td>4</td> <td>5</td> <td>6</td> <td>7</td> <td>8</td> <td>9</td> <td>10</td> </tr> <tr align="CENTER" valign="TOP"> <td><strong> 11 </strong></td> <td>12</td> <td>13</td> <td>14</td> <td>15</td> <td>16</td> <td>17</td> </tr> <tr align="CENTER" valign="TOP"> <td>18</td> <td>19</td> <td>20</td> <td>21</td> <td>22</td> <td>23</td> <td>24</td> </tr> <tr align="CENTER" valign="TOP"> <td>25</td> <td>26</td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> </table> </body></html>
Sunday Monday Tuesday Wednesday Thursday Friday Saturday
27
28 29 30 31 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26

Update: clean up table layout


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: cgi calander
by eric256 (Parson) on Feb 11, 2007 at 05:39 UTC

    Some creative urge over took me and i figured out the date calculations to go with your HTML output! ;)

    use strict; use warnings; use Data::Dumper; use CGI::Pretty qw(:standard start_table end_table start_Tr end_Tr), ( +-unique_headers); #use CGI::Carp qw(fatalsToBrowser); use Date::Calc qw(Week_of_Year Monday_of_Week Add_Delta_Days Standard +_to_Business); my @day = (2007,2,10); my @week = Week_of_Year(@day); my @weeks = []; for my $delta (-15..15) { my @n_day = Add_Delta_Days(@day,$delta); #calculate which week we are in my @n_week = Week_of_Year(@n_day); #calculate the day of the week my @b_day = Standard_to_Business(@n_day); #put it in a nice array of arrayref $weeks[$n_week[0]]->[$b_day[2]-1] = $n_day[2]; } # this seemed easier than calculating the smallest week # number and subtracting that from each index @weeks = grep {defined} @weeks; print header(); print start_html (-title => "Calendar sample"); my @weekDays = qw(Mon Tue Wed Thur Fri Sat Sun); print start_table(); print Tr ({-align=>'CENTER',-valign=>'TOP'}, td (\@weekDays)); for my $week (@weeks) { print start_Tr({-align=>'CENTER',-valign=>'TOP'}); for my $date (@$week) { if (! defined $date) { print td (''); } elsif ($date == $day[2]) { print td(strong($date)); } else { print td ($date); } } print end_Tr(); } print end_html ();

    ___________
    Eric Hodges
Re^2: cgi calander
by mikejones (Scribe) on Feb 15, 2007 at 23:17 UTC
    ok thank you for pushing me in the right direction, but now when the dates change my calendar gets thrown off. I tested this by simply adding and subtracting 1 from the SQL call curdate() +1 or curdate() -1. Here is my code:
    #use strict; #use warnings; require "dbi-lib.pl"; use CGI qw/:standard *table start_ul/, (-unique_headers); use CGI::Carp qw(fatalsToBrowser); use DBI; my $cgi = new CGI; local $sth; #----------------------# # Function calls # #----------------------# print_header(); initialize_dbi(); #----------------# # Begin Main # #----------------# print $cgi->header(), $cgi->start_html ('Oreilly SQL Class, Lesson 11'),# Header $cgi->h1 ({-style=>'Color:blue'},'SQL Class');# Body print "<u>",$cgi->strong('Date/Time CGI Table:'),"</u><p>"; print $cgi->caption('MySQL calendar table output, Lesson 11:'); print "<p>"; run_statement( "select date_format(curdate(), '%b %e, %Y');" ); my $today = $sth->fetchrow; #run_statement( "select dayofweek('$today');" ); #my $dayofweek = $sth->fetchrow; #print $dayofweek; #&run_statement( "select date_format(curdate()-2,'%W');" ); + #my $twodayago = $sth->fetchrow; #print "$twodayago <p>"; my @WeekDays = qw(Sunday Monday Tuesday Wednesday Thursday Friday Satu +rday); my ($day,$week,) = (0,0); my (@weeks, @weeks_anonymous,) = (); foreach $day ('-15' .. '15') { run_statement( "select date_format(date_add(curdate(), interval '$ +day' day), '%b %e, %Y');" ); push (@weeks, $sth->fetchrow); } ## Begin Table ## print table({border=>undef}); print Tr ({-align=>'CENTER',-valign=>'TOP'}, td (\@WeekDays)); @weeks_anonymous = ( [ undef, undef, undef, $weeks[0], $weeks[1], $weeks[2], $weeks[3] ], [ $weeks[4], $weeks[5], $weeks[6], $weeks[7], $weeks[8], $weeks[9], $ +weeks[10] ], [ $weeks[11], $weeks[12], $weeks[13], $weeks[14], $weeks[15], $weeks[1 +6], $weeks[17] ], [ $weeks[18], $weeks[19], $weeks[20], $weeks[21], $weeks[22], $weeks[2 +3], $weeks[24] ], [ $weeks[25], $weeks[26], $weeks[27], $weeks[28], $weeks[29], $weeks[3 +0], $weeks[31] ], ); foreach $element (@weeks_anonymous) { print Tr({-align=>'CENTER',-valign=>'TOP'}); for $date (@{$element}) { if (! defined $date) { print td (''); } elsif ($date eq $today) { print td(strong($date)); } else { print td ($date); } } print $cgi->end_Tr; } print $cgi->end_html; ************************** I was thinking of hard-coding January, Febuary and March months into +an anonymous array then combined with the @weeks_anonymous elements c +reate a final anonymous array that reflects the correct pairing of +daynames and dates???