The following need to be edited: (Click on the hyperlinked lines to see editing comments)
#!/usr/bin/perl5
require 'cgi-lib.pl';
$indata="your_file.txt";
$header="your_header_file.htm";
open (DATA, $indata) || &CgiError("Unable to open database for reading $indata");
my (%data, @names, $hash, $line, $data, @printed, $temp, $blarg);
umask (011);
while (<DATA>) {
chop $_;
#Below is the record split character, in this case, the pipe: |
#Change this to reflect your split character. See the README for details
my @line = split /\|/;
# The next line dictates the sort order. See the README for details.
my $blarg = "$line[1]$line[0]";
#These are the fields in the order they appear in your datafile.
@{$data{$blarg}}{qw(
fname lname title company address1 address2
city province postal country work_phone
work_fax email
)} = @line;
push @names, $blarg;
}
#This foreach loop assigns each name to an array appropriate to it's starting character
foreach (@names) {
$temp=(substr $_, 0, 1);
$temp=~ tr/A-Z/a-z/;
push @$temp, $_;
}
# This foreach loop deletes your old files before updating
foreach ('a'..'z') {
unlink "../members/$_".".htm";
}
# This foreach loop and suproutine opens your files to be written and writes the HTML header info
foreach ('a'..'z') {
$writefile = "../members/$_.htm";
open (WPUBLISH, ">>$writefile") || die "Couldnt open ";
&WRITEHEADER;
}
# This loop opens each file again and adds the individual, sorted records.
foreach ('a'..'z') {
$writefile = "../members/$_.htm";
open (WPUBLISH, ">>$writefile") || die "Couldnt open ";
foreach (@$_) {
print WPUBLISH<<EOF;
<TR><TD><B>First Name</TD><TD>$data{$_}{fname}</TD></TR>
<TR><TD><B>Last Name</TD><TD><B>$data{$_}{lname}</B></TD></TR>
<TR><TD><B>Title</TD><TD>$data{$_}{title}</TD></TR>
<TR><TD><B>Company</TD><TD>$data{$_}{company}</TD></TR>
<TR><TD><B>Address 1</TD><TD>$data{$_}{address1}</TD></TR>
<TR><TD><B>Address2</TD><TD>$data{$_}{address2}</TD></TR>
<TR><TD><B>City</TD><TD>$data{$_}{city}</TD></TR>
<TR><TD><B>Province</TD><TD>$data{$_}{province}</TD></TR>
<TR><TD><B>Postal Code</TD><TD>$data{$_}{postal}</TD></TR>
<TR><TD><B>Country</TD><TD>$data{$_}{country}</TD></TR>
<TR><TD><B>Phone</TD><TD>$data{$_}{work_phone}</TD></TR>
<TR><TD><B>Fax</TD><TD>$data{$_}{work_fax}</TD></TR>
<TR><TD><B>Email</TD><TD><A HREF=\"mailto:$data{$_}{email}\">$data{$_}{email}</A></TD></TR>
<TR><TD> </TD></TR>
EOF
}
print WPUBLISH "</TABLE></TABLE>\n</BODY>\n<</HTML>";
}
print &PrintHeader;
print "Done!\n";
sub WRITEHEADER {
open (PGHEAD, $header) || &CgiError("Unable to open $header for reading");
while (<PGHEAD>) {
print WPUBLISH $_;
}
}
Generally, it is advised to leave this as the pipe character. Most databases allow you to specify your own split character.
So, to use the code from above, if you wanted to sort by last name, and ensure no record duplication by combining last name wiht the phone number, the line would read: my $blarg = "$line[1]$line[10]";
Copyright
This script is being distributed as shareware. If you like it, if it saves you time and money, and more importantly, if you continue to use it, please feel send $50 to the address in the comments section of membermanager.cgi (Don't need spam bots grabbing the address from here)
Back to MemberManager page