nlafferty has asked for the wisdom of the Perl Monks concerning the following question:

I have a script I'm writing for scheduling of employees. It generates a table displaying each hour of the day and marks the scheduled hours. This works with on employee. I need to know how to handle multiple inputs from the form with fields of the same name. The most I can figure is to use foreach with arrays. But I'm not sure how to orrganize it so that it works. In other words, it needs to print the values correctly in separate rows in the table. Here is my code so far.
Thanks.
I also think that my elsif statments could be shorter!

#!/usr/bin/perl -w ######################################## # Work Schedule Chart Generator Version 0.01 # By Nick Lafferty # ######################################## #use CGI::Carp qw(fatalsToBrowser); #warn "this is a complaint"; #die "But this is serious"; #use strict; use CGI qw/:standard/; print header; my ($employee, $date, $sshift, $eshift, $total_shift, $on_shift, $td, +$off_shift1, $off_shift2, $name); $date = param("date"); $employee = param("employee"); $sshift = param("sshift"); $eshift = param("eshift"); $name = "<TD>$employee</TD>"; # Calculate On shift hours and generate table cells $total_shift = $eshift - $sshift; $on_shift = "<TD>d</TD>" x $total_shift; # Calculate Off Shift hours and generate cells $td = "<TD></TD>"; if ($sshift eq "8"){ $off_shift1 = ""; }elsif ($sshift eq "9"){ $off_shift1 = "$td"; }elsif ($sshift eq "10"){ $off_shift1 = "$td$td"; }elsif ($sshift eq "11"){ $off_shift1 = "$td$td$td"; }elsif ($sshift eq "12"){ $off_shift1 = "$td$td$td$td"; }elsif ($sshift eq "13"){ $off_shift1 = "$td$td$td$td$td"; }elsif ($sshift eq "14"){ $off_shift1 = "$td$td$td$td$td$td"; }elsif ($sshift eq "15"){ $off_shift1 = "$td$td$td$td$td$td$td"; }elsif ($sshift eq "16"){ $off_shift1 = "$td$td$td$td$td$td$td$td"; }elsif ($sshift eq "17"){ $off_shift1 = "$td$td$td$td$td$td$td$td$td"; }elsif ($sshift eq "18"){ $off_shift1 = "$td$td$td$td$td$td$td$td$td$td"; }elsif ($sshift eq "19"){ $off_shift1 = "$td$td$td$td$td$td$td$td$td$td$td"; }elsif ($sshift eq "20"){ $off_shift1 = "$td$td$td$td$td$td$td$td$td$td$td$td"; }elsif ($sshift eq "21"){ $off_shift1 = "$td$td$td$td$td$td$td$td$td$td$td$td$td"; }elsif ($sshift eq "22"){ $off_shift1 = "$td$td$td$td$td$td$td$td$td$td$td$td$td$td"; }elsif ($sshift eq "23"){ $off_shift1 = "$td$td$td$td$td$td$td$td$td$td$td$td$td$td$td"; }else{ $off_shift1 = "$td$td$td$td$td$td$td$td$td$td$td$td$td$td$td$td"; } if ($eshift eq "8"){ $off_shift2 = "$td$td$td$td$td$td$td$td$td$td$td$td$td$td$td$td"; }elsif ($eshift eq "9"){ $off_shift2 = "$td$td$td$td$td$td$td$td$td$td$td$td$td$td$td"; }elsif ($eshift eq "10"){ $off_shift2 = "$td$td$td$td$td$td$td$td$td$td$td$td$td$td"; }elsif ($eshift eq "11"){ $off_shift2 = "$td$td$td$td$td$td$td$td$td$td$td$td$td"; }elsif ($eshift eq "12"){ $off_shift2 = "$td$td$td$td$td$td$td$td$td$td$td$td"; }elsif ($eshift eq "13"){ $off_shift2 = "$td$td$td$td$td$td$td$td$td$td$td"; }elsif ($eshift eq "14"){ $off_shift2 = "$td$td$td$td$td$td$td$td$td$td"; }elsif ($eshift eq "15"){ $off_shift2 = "$td$td$td$td$td$td$td$td$td"; }elsif ($eshift eq "16"){ $off_shift2 = "$td$td$td$td$td$td$td$td"; }elsif ($eshift eq "17"){ $off_shift2 = "$td$td$td$td$td$td$td"; }elsif ($eshift eq "18"){ $off_shift2 = "$td$td$td$td$td$td"; }elsif ($eshift eq "19"){ $off_shift2 = "$td$td$td$td$td"; }elsif ($eshift eq "20"){ $off_shift2 = "$td$td$td$td"; }elsif ($eshift eq "21"){ $off_shift2 = "$td$td$td"; }elsif ($eshift eq "22"){ $off_shift2 = "$td$td"; }elsif ($eshift eq "23"){ $off_shift2 = "$td"; }else{ $off_shift2 = ""; } $row = "$name$off_shift1$on_shift$off_shift2"; &table_header; sub table_header { print <<HTML; <HTML> <HEAD> <TITLE>b</TITLE> </HEAD> <BODY> <TABLE cols=18 width="95%" bgColor=#ccffff border=1> <CAPTION>$date</CAPTION> <TBODY> <TR vAlign=top> <TD vAlign=top>Name</TD> <TD vAlign=top>8:00</TD> <TD vAlign=top>9:00</TD> <TD vAlign=top>10:00</TD> <TD vAlign=top>11:00</TD> <TD vAlign=top>12:00</TD> <TD vAlign=top>1:00</TD> <TD vAlign=top>2:00</TD> <TD vAlign=top>3:00</TD> <TD vAlign=top>4:00</TD> <TD vAlign=top>5:00</TD> <TD vAlign=top>6:00</TD> <TD vAlign=top>7:00</TD> <TD vAlign=top>8:00</TD> <TD vAlign=top>9:00</TD> <TD vAlign=top>10:00</TD> <TD>11:00</TD> <TD>12:00</TD></TR> <TR>$row</TR> </BODY> </HTML> HTML }

Replies are listed 'Best First'.
Re: Help With Multiple Form Fields
by merlyn (Sage) on Feb 13, 2002 at 18:27 UTC
    You might want to look at HTML::Table. For example:
    use HTML::Table; my $t = HTML::Table->new; $t->setCell(1, 1, "Name"); $t->setCell(1, $_, sprintf "%d:00", ($_ + 5) % 12 + 1) for 2..16; my $employee = "Randal"; my $sshift = 10; my $eshift = 14; # for testi +ng $t->setCell(2, 1, $employee); $t->setCell(2, $_ - 8 + 2, "d") for $sshift..$eshift; $t->print;
    That dumps a table of two rows and the right columns, plus the letter d in the right cells below. That's about 90% of your code so far, right there. {grin}

    -- Randal L. Schwartz, Perl hacker

(Ovid) Re: Help With Multiple Form Fields
by Ovid (Cardinal) on Feb 13, 2002 at 18:46 UTC

    merlyn gave a good answer, so I'm going to point to another issue (which you've obviously recognized, because you asked the question).

    Whenever you see a pattern emerge in your code, look for an opportunity to refactor your code into something that more naturally reflects what your code is trying to do. For example, the entire chunk of code that you have from "use strict" down to assigning to the $row variable can be reduced to the following:

    use strict; use CGI qw/:standard/; print header; my ( $off_shift1, $off_shift2 ); my $date = param("date") || ''; my $employee = param("employee") || ''; my $sshift = param("sshift") || ''; my $eshift = param("eshift") || ''; my $name = td( $employee ); # Calculate On shift hours and generate table cells my $total_shift = $eshift - $sshift; my $on_shift = td( 'd' ) x $total_shift; # ??? # Calculate Off Shift hours and generate cells my $td = td(''); if ( $sshift > 7 and $sshift < 25 ) { $off_shift1 = $td x ( $sshift - 8 ); } if ( $eshift > 7 and $eshift < 25 ) { $off_shift2 = $td x ( 24 - $eshift ); } my $row = "$name$off_shift1$on_shift$off_shift2";

    That's a lot easier to read and maintain. Now, why did I test to ensure that $eshift and $sshift were in the correct range? Because validating your input data is almost always one of the first things you want to do. Read perlsec for more information about taint checking and ensuring that the data you read doesn't do horrible things to you later.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      Cool beans! That is so much more legible. The only other thing I need to do is have it print for multiple entries. I started the script to do one single row. What I need is for my script to work with 30 employees. So I need some sort of loop.

      I'm not sure wheather to use while or foreach And how exactly to accept all of the data if each form field is say

      <input type=text name=employee>

      All of my form fields have the same name (date, employee, sshift, eshift) assigned to allow adding of employees.
Re: Help With Multiple Form Fields
by screamingeagle (Curate) on Feb 13, 2002 at 18:23 UTC
    you could use the repitition operator to make things easier . for example, substitute
    $off_shift1 = "$td$td$td$td$td";
    for
    $off_shift1 = $td x 5;
    this will give the same result.
Re: Help With Multiple Form Fields
by nlafferty (Scribe) on Feb 13, 2002 at 20:17 UTC
    Alright. So I turn
    $employee = param("employee"); $sshift = param("sshift"); $eshift = param("eshift");
    To
    @employee = param("employee"); @sshift = param("sshift"); @eshift = param("eshift");
    This allows me to get all the data into each array. Now I need my loop to process the three arrays for each $employee, $sshift, and $eshift like in my above code.

    Perhaps I need to type more clearly as my main delema was overlook. Although a lot of good came from it being missed! Thanks