By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,848 Members | 1,416 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,848 IT Pros & Developers. It's quick & easy.

Using CGI to print a table

Ganon11
Expert 2.5K+
P: 3,652
Hey everyone,

For a class assignment, I am making a class scheduling CGI program/script. I have everything working so far except actually displaying the schedule as a grid/2D table. I am not very proficient in HTML, let alone Perl making HTML, so I tried to grab some code I found online and found two issues:

1) In the following code:

Expand|Select|Wrap|Line Numbers
  1. print table({-border=>undef},
  2.            caption('Class Schedule'),
  3.            Tr({-align=>CENTER,-valign=>TOP},
  4.            [
  5.               th(\@headers),
  6.               td(\@AM_8),
  7.               td(\@AM_9),
  8.               td(\@AM_10),
  9.               td(\@AM_11),
  10.               td(\@PM_12),
  11.               td(\@PM_1),
  12.               td(\@PM_2),
  13.               td(\@PM_3),
  14.               td(\@PM_4),
  15.               td(\@PM_5),
  16.               td(\@PM_6),
  17.               td(\@PM_7),
  18.               td(\@PM_8),
  19.               td(\@PM_9)
  20.            ]
  21.            )
  22.         ), p;
my CGI page tells me it cannot recognize the CENTER or TOP keywords, and will not print anything. Not a big deal - I can delete that portion and the errors will end.

2) I am building @AM_8, @AM_9, etc. in the following manner:

Expand|Select|Wrap|Line Numbers
  1. my (
  2.    @AM_8, 
  3.    @AM_9, #etc etc etc...all the way up to @PM_9
  4. ) = (
  5.    ('8 AM', ' ', ' ', ' ', ' ', ' '), 
  6.    ('9 AM', ' ', ' ', ' ', ' ', ' '), #etc etc etc...for each array
  7. );
  8.  
  9. for my $class (@master_class_list) {
  10.         my $class_desc = $class->NAME . ", Room " . $class->ROOM;
  11.         if ($class->START_TIME == 1) {    # Only way this class could be at 8 is if it starts at 8
  12.             if (array_contains("Monday", @{$class->DAYS})) {
  13.                 $AM_8[1] = $class_desc;
  14.             }
  15.             if (array_contains("Tuesday", @{$class->DAYS})) {
  16.                 $AM_8[2] = $class_desc;
  17.             }
  18.             if (array_contains("Wednesday", @{$class->DAYS})) {
  19.                 $AM_8[3] = $class_desc;
  20.             }
  21.             if (array_contains("Thursday", @{$class->DAYS})) {
  22.                 $AM_8[4] = $class_desc;
  23.             }
  24.             if (array_contains("Friday", @{$class->DAYS})) {
  25.                 $AM_8[5] = $class_desc;
  26.             }
  27.         }
  28.                 # And so on and so forth, for each time period.
I realize this is incredibly tedious, but I don't know how to print the table any better.

Anyway, after building the list like that, I printed the table (using the code from (1)), and got horribly incorrect output. I changed the code to

Expand|Select|Wrap|Line Numbers
  1. print table({-border=>undef},
  2.            caption('Class Schedule'),
  3.            Tr(
  4.            [
  5.               th(\@headers),
  6.               td(\@AM_8)
  7.            ]
  8.            )
  9.         ), p;
to see if I could narrow down the problem, and got this as my output:

http://i295.photobucket.com/albums/m...RONG_WRONG.jpg

I do not understand this in the least. I am no longer printing ANYTHING containing '9 AM', '10 AM', etc., yet they are still being output. I would like to have a 2D grid with days across the top (as shown) and times along the left side. Any advice on how to do this?

This is a homework assignment due Tuesday night, and this is only a small portion. If I can't figure this out by Tuesday night, I'll just print all the class information, which will at least get me partial credit, but I'd like to get this table working.

Any help is MUCH appreciated!
Apr 20 '08 #1
Share this Question
Share on Google+
3 Replies


KevinADC
Expert 2.5K+
P: 4,059
my CGI page tells me it cannot recognize the CENTER or TOP keywords, and will not print anything. Not a big deal - I can delete that portion and the errors will end.
You need to quote those words, since they are just simple strings use single-quotes:

Expand|Select|Wrap|Line Numbers
  1. Tr({-align=>'CENTER',-valign=>'TOP'},
Apr 20 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
The thing to do to look at why your html is not what you expect is examine the html source code. Then modify your perl code that generates the html accordingly.

Your perl code is quite confusing and therefore there is no way to help determine if it is contribuitng to the problem or if it is a simple html problem.
Apr 20 '08 #3

Ganon11
Expert 2.5K+
P: 3,652
Kevin,

Thanks for the reply, both problems have been solved. It turns out the initialization list for my arrays was flattening out and filling @AM_8 with everything. Separating the declarations into different lines solved that issue.

I also agree that this code is a huge mess. Unfortunately, I am terrible at HTML, and thus terrible at generating HTML with Perl, so this is the best I can do for code simplicity.
Apr 20 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.