📄 ch11.htm
字号:
# Read in database.
$any="no"; # Flag which determines if appts were found.
open (DATES, "$datebook");
@dates=<DATES>;
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) {
($julean,$time,$endtime,$desc,$name,$id)=split(/~~~/,$date);
if ($julean==$jule) {
print "<tr><td>";
if ($time eq "00:00" && $endtime eq "00:00") { print "(All day) ";}
else {
# am/pm the time
($hr,$min)=split(/:/,$time);
if ($hr==24) {$hr="12";
$ampm="am"}
else {
if ($hr<12) {$ampm="am"}
else {$hr-=12;
if ($hr==0){$hr=12};
$ampm="pm"};
} # end else
$time=$hr.":".$min." ".$ampm;
print "$time";}
if ($endtime eq "00:00") {}
else {
# am/pm the time
($hr,$min)=split(/:/,$endtime);
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>";
$any="yes";}
}
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.";
&footer;
</FONT></PRE>
<P>Let's take a look at how disp_day.cgi works. The database file is opened as a
file handle (<TT>DATES</TT>) and read line by line into an array (<TT>@dates</TT>).
Once read into the array, the file handle is closed.</P>
<PRE><FONT COLOR="#0066FF">
open (DATES, "$datebook");
@dates=<DATES>;
close DATES;
</FONT></PRE>
<P>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 <TT>$month</TT>,
<TT>$day</TT>, and <TT>$year</TT> submitted in the URI:</P>
<PRE><FONT COLOR="#0066FF">&julean($month,$day,$year); # Julean date of day in question.
</FONT></PRE>
<P>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 <TT>@dates</TT> array until the specified date is reached:</P>
<PRE><FONT COLOR="#0066FF"># Checks database for listings of that day.
print "<table border width=100%>";
print "<tr><th> Time <th> Event <th> Name <br>";
for $date (@dates) {
</FONT></PRE>
<P>When a line in the database is found that matches the specified date, it is broken
down using the split operator into six variables: <TT>$julean</TT>, <TT>$time</TT>,
<TT>$endtime</TT>, <TT>$desc</TT>, <TT>$name</TT>, and <TT>$id</TT>.</P>
<PRE><FONT COLOR="#0066FF">($julean,$time,$endtime,$desc,$name,$id)=split(/~~~/,$date);
</FONT></PRE>
<P>From these elements, an HTML table of events for that day is generated. The variables
are embedded into HTML tables. Notice that the <TT><table></TT> 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 <TT><TR><TD></TT>
tags and end with <TT></TD></TR></TT>. The <TT></table></TT> 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.
<CENTER>
<H4><A NAME="Heading11"></A><FONT COLOR="#000077">Adding Calendar Entries with add_date.cgi</FONT></H4>
</CENTER>
<P>The add_date.cgi program either outputs an HTML form to set up a new appointment
or takes the post-method URI encoded data from the form and inputs it into the database
(see Listing 11.4). Like hypercal.cgi, which action occurs is based on the input
supplied to the program in the URI.
<CENTER>
<H3><A NAME="Heading12"></A><FONT COLOR="#000077">Listing 11.4. add_date.cgi.</FONT></H3>
</CENTER>
<PRE><FONT COLOR="#0066FF">#!/usr/bin/perl
#
# Add Date
#
# Prints html form for input of new appointment. Sends the
# form input to part_2 for processing.
# Richard Bowen, 12/14/95
# rbowen@aiclex.com
#_____________________________________________________
require `httools.pl';
require `variables';
&header;
# Determine if it is a personal calendar
($sub=$ENV{`PATH_INFO'})=~s#^/##;
if ($sub=~/personal/){require $personal};
# Read in date from QUERY_STRING
$date=$ENV{`QUERY_STRING'};
($month,$day,$year,$command)=split(/&/,$date);
#
# Determine which part of the script is being called
#__________________________________
if ($command eq "doit") {&part_2}
else { &part_1 };
#
# Part One
#
# Prints html form and sends results to part 2
#______________________________________________
sub part_1 {
# Print some titles to browser.
&month_txt("$month");
&title ("Add an appointment for $month_txt $day, $year.");
print <<"HTML";
<h2>Add an appointment for $month_txt $day, $year.</h2>
<b>Note:</b> Only authorized users will be able to add and delete
appointments. Please contact your web adminstrator for a username
and password.
<hr>
<form method=post action=$base_url$add_date?$month&$day&$year&doit>
<b>Time:</b>
<input name="hour" size=2 value="00"><b>: </b>
<input name="min" value="00" size=2>
<input type=radio name="ampm" value="am" CHECKED>AM
<input type=radio name="ampm" value="pm">PM
<br>
<b>Until:</b>
<input name="hour_done" size=2 value="00"><b>: </b>
<input name="min_done" value="00" size=2>
<input type=radio name="ampm_done" value="am" CHECKED>AM
<input type=radio name="ampm_done" value="pm">PM
<br>
<i>If no beginning time is specified, the event will be listed as
the whole day. If no end time is entered, the event will be listed
with only the beginning time</i>
<br>
<b>Description</b><br>
<textarea name="desc" rows=5 cols=60></textarea><br>
<table><tr><td valign=top rowspan=5><b>Event occurs:</b><br>
<td><input type=radio name="freq" value="once" CHECKED>Once<br>
<tr><td><input type=radio name="freq" value="daily">Daily for :
<input name="days" value="1" size="2"> days.<br>
<tr><td><input type=radio name="freq" value=\"weekly\">Weekly for :
<input name="weeks" value="1" size=2> weeks.<br>
<tr><td><input type=radio name="freq" value="monthly">Monthly for :
<input name="months" value="1" size=2> months.<br>
<tr><td><input type=radio name="freq" value="annual">Annually for :
<input name="years" value="1" size=2> years.<br>
</table>
Please enter your name : <input name="perp" size="20"><br>
<input type=submit value="Add Appointment">
</form><hr>
Calendar entries will expire and be deleted $old days after the event.
HTML
&footer;
} # End of part 1
sub part_2 {
# Receives the post data from add_form and adds the information
# to the database. Format of database is currently:
# Julean&time&endtime&event&name&id
#
# Get data from form post.
# Variables are:
# hour, min, ampm, desc, freq, perp
# hour_done, min_done, ampm_done, days, weeks, months
# freq = one of (once, daily, weekly, monthly)
&form_parse;
# Strip returns from description field to make it one continuous string.
$FORM{`desc'} =~ s/\n//g;
# Print titles to HTML page.
&month_txt("$month");
&title ("Appointment added to $month_txt $day, $year");
print "<h1>Appointment added to $month_txt $day, $year</h1>";
# Read in current contents of database.
open (DATES,"$datebook") || print "Was unable to open the datebook
database for reading <br>\n";
@dates=<DATES>;
close DATES;
for (@dates){chop};
&julean($month,$day,$year);
# Rewrite time
&time($FORM{`hour'},$FORM{`min'},$FORM{`ampm'});
$begin=$time;
&time($FORM{`hour_done'},$FORM{`min_done'},$FORM{`ampm_done'});
$done=$time;
# Get id number
open (ID, "$hypercal_id") || print "Was unable to open the ID file for Âreading<br>\n";
@id=<ID>;
close ID;
for (@id){chop};
@id[0]++;
if (@id[0]>=999999) {@id[0]=1};
$id=@id[0];
open (NEWID,">$hypercal_id") || print "Was unable to open the ID
file ($hypercal_id) for writing<br>\n";
for $each (@id) {
print NEWID "$each\n";}
# Add the new appointment to the database.
$newappt="$jule~~~$begin~~~$done~~~$FORM{`desc'}~~~$FORM{`perp'}~~~$id";
push (@newdates,$newappt);
if ($FORM{`freq'} ne "once") { &many };
&julean($month,$day,$year);
&todayjulean;
for $date (@dates) {
($juldate,$apptime,$appendtime,$appdesc,$perpname,$id)=split(/~~~/,$date);
if (($today-$juldate)<=$old) {push (@newdates,$date) }
}
@dates=sort(@newdates);
# Write database back to disk file.
open (NEWDATES,">$datebook") || print "Was unable to open the
datebook file for writing.<br>\n";
foreach $date (@dates) {print NEWDATES "$date\n"}
close NEWDATES;
# Links back to other pages.
print "Back to calendar for
<a href=$base_url$hypercal?$month&$year>$month\/$year</a><br>";
print "Back to <a href=$base_url$disp_day?$month&$day&$year>
$month\/$day\/$year</a>.";
&footer;
} # End of part_2
#
# Sub time
# Rewrites time into 24hr format.
#
sub time {
$time="";
$HOUR=$_[0];
$MINS=$_[1];
$merid=$_[2];
if ($merid eq "pm") {
$HOUR+=12;
if ($HOUR==24) {$HOUR=12}
}
if ($HOUR==12 && $merid eq "am"){$HOUR=24};
if ($HOUR>24){$HOUR=23};
if ($MINS>59){$MINS=59};
$HOUR=sprintf "%02.00f",$HOUR;
$MINS=sprintf "%02.00f",$MINS;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -