⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch11.htm

📁 Web_Programming_with_Perl5,一个不错的Perl语言教程。
💻 HTM
📖 第 1 页 / 共 4 页
字号:
#  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, &quot;$datebook&quot;);



@dates=&lt;DATES&gt;;



close DATES;



</FONT></PRE>



<P>Since entries are indexed in the database by their Julian date, referred to in



the code as &quot;julean&quot; 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">&amp;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 &quot;&lt;table border width=100%&gt;&quot;;



print &quot;&lt;tr&gt;&lt;th&gt; Time &lt;th&gt; Event &lt;th&gt; Name &lt;br&gt;&quot;;



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>&lt;table&gt;</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>&lt;TR&gt;&lt;TD&gt;</TT>



tags and end with <TT>&lt;/TD&gt;&lt;/TR&gt;</TT>. The <TT>&lt;/table&gt;</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';



&amp;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(/&amp;/,$date);



#



#    Determine which part of the script is being called



#__________________________________



if ($command eq &quot;doit&quot;) {&amp;part_2}



else { &amp;part_1 };



#



#    Part One



#



#    Prints html form and sends results to part 2



#______________________________________________



sub part_1    {



#  Print some titles to browser.



&amp;month_txt(&quot;$month&quot;);



&amp;title (&quot;Add an appointment for $month_txt $day, $year.&quot;);



print &lt;&lt;&quot;HTML&quot;;



&lt;h2&gt;Add an appointment for $month_txt $day, $year.&lt;/h2&gt;



&lt;b&gt;Note:&lt;/b&gt; Only authorized users will be able to add and delete 



appointments. Please contact your web adminstrator for a username 



and password.



&lt;hr&gt;



&lt;form method=post action=$base_url$add_date?$month&amp;$day&amp;$year&amp;doit&gt;



&lt;b&gt;Time:&lt;/b&gt;



&lt;input name=&quot;hour&quot; size=2 value=&quot;00&quot;&gt;&lt;b&gt;: &lt;/b&gt;



&lt;input name=&quot;min&quot; value=&quot;00&quot; size=2&gt;



&lt;input type=radio name=&quot;ampm&quot; value=&quot;am&quot; CHECKED&gt;AM



&lt;input type=radio name=&quot;ampm&quot; value=&quot;pm&quot;&gt;PM



&lt;br&gt;



&lt;b&gt;Until:&lt;/b&gt;



&lt;input name=&quot;hour_done&quot; size=2 value=&quot;00&quot;&gt;&lt;b&gt;: &lt;/b&gt;



&lt;input name=&quot;min_done&quot; value=&quot;00&quot; size=2&gt;



&lt;input type=radio name=&quot;ampm_done&quot; value=&quot;am&quot; CHECKED&gt;AM



&lt;input type=radio name=&quot;ampm_done&quot; value=&quot;pm&quot;&gt;PM



&lt;br&gt;



&lt;i&gt;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&lt;/i&gt;



&lt;br&gt;



&lt;b&gt;Description&lt;/b&gt;&lt;br&gt;



&lt;textarea name=&quot;desc&quot; rows=5 cols=60&gt;&lt;/textarea&gt;&lt;br&gt;



&lt;table&gt;&lt;tr&gt;&lt;td valign=top rowspan=5&gt;&lt;b&gt;Event occurs:&lt;/b&gt;&lt;br&gt;



&lt;td&gt;&lt;input type=radio name=&quot;freq&quot; value=&quot;once&quot; CHECKED&gt;Once&lt;br&gt;



&lt;tr&gt;&lt;td&gt;&lt;input type=radio name=&quot;freq&quot; value=&quot;daily&quot;&gt;Daily for :



&lt;input name=&quot;days&quot; value=&quot;1&quot; size=&quot;2&quot;&gt; days.&lt;br&gt;



&lt;tr&gt;&lt;td&gt;&lt;input type=radio name=&quot;freq&quot; value=\&quot;weekly\&quot;&gt;Weekly for :



&lt;input name=&quot;weeks&quot; value=&quot;1&quot; size=2&gt; weeks.&lt;br&gt;



&lt;tr&gt;&lt;td&gt;&lt;input type=radio name=&quot;freq&quot; value=&quot;monthly&quot;&gt;Monthly for :



&lt;input name=&quot;months&quot; value=&quot;1&quot; size=2&gt; months.&lt;br&gt;



&lt;tr&gt;&lt;td&gt;&lt;input type=radio name=&quot;freq&quot; value=&quot;annual&quot;&gt;Annually for :



&lt;input name=&quot;years&quot; value=&quot;1&quot; size=2&gt; years.&lt;br&gt;



&lt;/table&gt;



Please enter your name : &lt;input name=&quot;perp&quot; size=&quot;20&quot;&gt;&lt;br&gt;



&lt;input type=submit value=&quot;Add Appointment&quot;&gt;



&lt;/form&gt;&lt;hr&gt;



Calendar entries will expire and be deleted $old days after the event.



HTML



&amp;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&amp;time&amp;endtime&amp;event&amp;name&amp;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)



&amp;form_parse;



# Strip returns from description field to make it one continuous string.



$FORM{`desc'} =~ s/\n//g;



#  Print titles to HTML page.



&amp;month_txt(&quot;$month&quot;);



&amp;title (&quot;Appointment added to $month_txt $day, $year&quot;);



print &quot;&lt;h1&gt;Appointment added to $month_txt $day, $year&lt;/h1&gt;&quot;;



#  Read in current contents of database.



open (DATES,&quot;$datebook&quot;) || print &quot;Was unable to open the datebook 



                                   database for reading &lt;br&gt;\n&quot;;



@dates=&lt;DATES&gt;;



close DATES;



for (@dates){chop};



&amp;julean($month,$day,$year);



#    Rewrite time



&amp;time($FORM{`hour'},$FORM{`min'},$FORM{`ampm'});



$begin=$time;



&amp;time($FORM{`hour_done'},$FORM{`min_done'},$FORM{`ampm_done'});



$done=$time;



# Get id number



open (ID, &quot;$hypercal_id&quot;)  || print &quot;Was unable to open the ID file for &#194;reading&lt;br&gt;\n&quot;;



@id=&lt;ID&gt;;



close ID;



for (@id){chop};



@id[0]++;



if (@id[0]&gt;=999999) {@id[0]=1};



$id=@id[0];



open (NEWID,&quot;&gt;$hypercal_id&quot;)  || print &quot;Was unable to open the ID 



                                        file ($hypercal_id) for writing&lt;br&gt;\n&quot;;



for $each (@id)    {



print NEWID &quot;$each\n&quot;;}



#  Add the new appointment to the database.



$newappt=&quot;$jule~~~$begin~~~$done~~~$FORM{`desc'}~~~$FORM{`perp'}~~~$id&quot;;



push (@newdates,$newappt);



if ($FORM{`freq'} ne &quot;once&quot;) { &amp;many };



&amp;julean($month,$day,$year);



&amp;todayjulean;



for $date (@dates)    {



($juldate,$apptime,$appendtime,$appdesc,$perpname,$id)=split(/~~~/,$date);



if (($today-$juldate)&lt;=$old) {push (@newdates,$date) }



        }



@dates=sort(@newdates);



#  Write database back to disk file.



open (NEWDATES,&quot;&gt;$datebook&quot;) || print &quot;Was unable to open the 



                                       datebook file for writing.&lt;br&gt;\n&quot;;



foreach $date (@dates) {print NEWDATES &quot;$date\n&quot;}



close NEWDATES;



#  Links back to other pages.



print &quot;Back to calendar for 



       &lt;a href=$base_url$hypercal?$month&amp;$year&gt;$month\/$year&lt;/a&gt;&lt;br&gt;&quot;;



print &quot;Back to &lt;a href=$base_url$disp_day?$month&amp;$day&amp;$year&gt;



       $month\/$day\/$year&lt;/a&gt;.&quot;;



&amp;footer;



        }    # End of part_2



#



#    Sub time



#  Rewrites time into 24hr format.



#



sub time    {



$time=&quot;&quot;;



$HOUR=$_[0];



$MINS=$_[1];



$merid=$_[2];



if ($merid eq &quot;pm&quot;) {



 $HOUR+=12;



 if ($HOUR==24) {$HOUR=12}



        }



if ($HOUR==12 &amp;&amp; $merid eq &quot;am&quot;){$HOUR=24};



if ($HOUR&gt;24){$HOUR=23};



if ($MINS&gt;59){$MINS=59};



$HOUR=sprintf &quot;%02.00f&quot;,$HOUR;



$MINS=sprintf &quot;%02.00f&quot;,$MINS;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -