CGI and Perl

Displaying Appointments on a Specific Day: disp_day.cgi

Each day in the calendar is a link to disp_day.cgi, along with the date URI encoded. For example, a link to June 25, 1997, looks like this:

The disp_day.cgi script, shown in Listing 11.3, simply reads the day from the URI, reads the datebook database, looks up the Julian date to reference the database entry, and formats an HTML page based on the contents of the database (if any) on that day.

Listing 11.3. disp_day.cgi.

 #  Display Day.  Reads in database and prints appointments for the
 #  selected day.  Allows option of adding new appointment.
 require `variables';
 require `';
 #  Determine if this is a personal calendar;
 if ($sub=~/personal/){require $personal};
 #   Read date from QUERY_STRING
 #  Print titles to html page
 &title("Appointments for $month_txt $day, $year.");
 print "<h2>Appointments for $month_txt $day, $year.</h2><hr>";
 #  Read in database.
 $any="no";     #  Flag which determines if appts were found.
 open (DATES, "$datebook");
 close DATES;
 &julean($month,$day,$year);   #    Julean date of day in question.
 #  Checks database for listings of that day.
 print "<table border width=100%>";
 print "<tr><th> Time <th> Event <th> Name <br>";
 for $date (@dates)    {
 if ($julean==$jule) {
 print "<tr><td>";
 if ($time eq "00:00" && $endtime eq "00:00")  { print "(All day) ";}
 else {
 #  am/pm the time
 if ($hr==24) {$hr="12";
 else {
 if ($hr<12) {$ampm="am"}
 else {$hr-=12;
     if ($hr==0){$hr=12};
 }    # end else
 $time=$hr.":".$min." ".$ampm;
 print "$time";}
 if ($endtime eq "00:00") {}
 else {
 #  am/pm the time
 if ($hr<=12) {$ampm="am"}
 else {$hr-=12; $ampm="pm"};
 if ($hr==0){$hr="12"};
 $endtime=$hr.":".$min." ".$ampm;
 print " - $endtime ";}
 print "<td> $desc <td> $name <br>";
 if ($any eq "no") {print "<tr><td colspan=3><center>
                           <b>** No appointments **</b></center><br>";}
 print "</table>";
 print "<hr>";
 print "<a href=$base_url$add_date?$month&$day&$year>Add an appointment</a><br>";
 print "<a href=$base_url$del_date?$month&$day&$year>
        Delete an appointment</a><br>" unless ($any eq "no");
 print "<a href=$base_url$hypercal?$month&$year>Back</a> to the calendar.";

Let's take a look at how disp_day.cgi works. The database file is opened as a file handle (DATES) and read line by line into an array (@dates). Once read into the array, the file handle is closed.

 open (DATES, "$datebook");
 close DATES;

Since entries are indexed in the database by their Julian date, referred to in the code as "julean" date, the Julian date is constructed from the $month, $day, and $year submitted in the URI:

&julean($month,$day,$year);   #    Julean date of day in question.

Now that we've read in the database and know what Julian day we're looking for, the real work can be done. The code that follows sets up a HTML table, then steps through the @dates array until the specified date is reached:

#  Checks database for listings of that day.
 print "<table border width=100%>";
 print "<tr><th> Time <th> Event <th> Name <br>";
 for $date (@dates)    {

When a line in the database is found that matches the specified date, it is broken down using the split operator into six variables: $julean, $time, $endtime, $desc, $name, and $id.


From these elements, an HTML table of events for that day is generated. The variables are embedded into HTML tables. Notice that the <table> tag is set up before any looping begins. To properly create tables with loop structures in your code, define the table before the loop. Each loop should begin with <TR><TD> tags and end with </TD></TR>. The </table> tag should be placed after the loop. This is obvious once you think about it and look at the HTML generated by an improperly written program.