#!/usr/bin/perl -w # interests.pl # produce several HTML tables: # 1.) all BRBC members # 2.) membership types # 3.) member interest by category # 4.) grand prix series # author: gregory c. wilcox # date: 3/14/2003 # note: use double quote with escape characters, such as \n # use single quote with HTML tags that contain single quotes, such as HREF #paths to csv data files # $path_to_csv="/www"; use warnings; use strict; use Text::ParseWords; use CGI::Carp qw(fatalsToBrowser); sub member; my $headingp = 1; # is this line the heading? my $email = 5; # email column of table my $category = 0; # category to build table for my $head; # table heading my $file = "members.csv"; # database name my $members = 0; # number of members my $data = 1; # database field comparison value -- normally 1 my $line; # one line of the database my @fields; # fields in one line my $input; # input value my @inputs; # all input values my $types; # string of all membership types to match my $typenames = ""; # list of the names for each type my $debug = 0; # set to 1 for debugging # column headings of member database my @heading = ( "Last Name", "First Name", "City", "State", "Types: ", "Email Address", "Occupation", "Road Rides", "Adopt-a-Lane", "Mountain Bike", "Trail Maintenance", "Direct Road Rides", "Advocacy", "Direct Mountain Rides", "Volunteer", "Assault on the Carolinas", "WesTrek??", "Hilly Hellacious Volunteer", "Tobacco Free for Life", "The Beat Goes On", "YMCA Tour de Leaves", ); # HTML header my @header = ( "Content-type: text/html\n", # must have two carriage returns '', 'BRBC Members By Interest Category', '', '' ); # HTML footer my @footer = ( '


', '

Home | ', 'Top | ', 'Mail', '

© Blue Ridge Bicycle Club Inc. 2003', '', ); # main program begins here my $query = $ENV{'QUERY_STRING'}; # print 'Query string=', $query, "\n"; unless ( $query ) { $query = "19&G"; } split_query($query); $category = int($inputs[0]); $types = $inputs[1]; # for category 'type', use type data if ($category == 4) { $data = $types; make_typenames($types); } # double duty for types: A = all members; G = Grand Prix if ($types =~ /A/) { $head = "All Members"; } elsif ($types =~ /G/) { $head = "Grand Prix Series"; } else { $head = $heading[$category].$typenames; } open(FILE, $file) or die "can't open $file: $!"; output_strings(@header); print '

', $head, "

\n"; print "\n"; while ($line=) { @fields=parse_line(',',0, $line); # parse_line does not work with single quotes - why? unless ( $fields[0] ) { @fields = split /,/, $line; } # print @fields, "\n"; if ($types =~ /G/) { grandprix(); } else { member(); } } print "
\n"; print "

Number of members: $members \n"; output_strings(@footer); close(FILE); # end of main program # output strings sub output_strings { foreach (@_) { print; print "\n"; } } # make a list of type names sub make_typenames { $typenames .= "Individual " if $types =~ /I/; $typenames .= "Family " if $types =~ /F/; $typenames .= "Relative " if $types =~ /R/; } # split the query into input values # input 0 = category # input 1 = types # no keys used here sub split_query { my $index = 0; foreach $input (split("&",$query)) { if ($debug) { print "Input($index) = [$input]
\n"; } $inputs[$index++] = $input; } } # output one entry in the table sub entry { my $i = shift; my $str = $fields[$i]; # print $heading[$i], ' = "', $str, "\"\n"; print ''; # if this is the table heading, use boldface if ($headingp) { print '', $str, ''; } # if this is an email address, make a link to it elsif ($i==$email && $str !~ m/none/i ) { print '', $str, ''; } else { print $str; } print "\n"; } # output table row for one member sub member { # print "Number of fields: " . @fields . "\n"; my $str = $fields[$category]; # print 'Category = ', $str, "\n"; # print 'line = "', $line, "\"\n"; # output this line if $types=A (all lines), line(category)=true, or for heading if ($types =~ /A/ || $str =~ /[$data]/ || $headingp) { print "\n"; # just the first seven fields for (my $i = 0; $i < 7; $i++) { entry($i); } print "\n"; unless ($headingp) { $members += 1; } $headingp = 0; } } # output table row for grand prix participant sub grandprix { my $str = $fields[$category]; my $grandprixp = 0; my $start = 15; my $stop = 21; # bug in conversion from excel to csv: some lines omit the last field unless ( exists $fields[$stop-1] ) { $fields[$stop-1] = ""; } # all grand prix events for (my $event = $start; $event < $stop; $event++) { # print "Event $event = $fields[$event]\n"; if ( int($fields[$event]) > 0 ) { $grandprixp = 1; } } # output this line if grand prix participant, or for heading if ($grandprixp || $headingp) { print "\n"; # just the first six fields for (my $i = 0; $i < 6; $i++) { entry($i); } # all grand prix fields for (my $i = $start; $i < $stop; $i++) { entry($i); } print "\n"; unless ($headingp) { $members += 1; } $headingp = 0; } } # end of file