Gossamer Forum
Home : Products : DBMan : Customization :

Alternating Table Colors In Top 10 List

Quote Reply
Alternating Table Colors In Top 10 List
Hi,

I've got my database (with the Top Ten Records mod) installed at http://www.warningtrack.net/cgi-bin/articles.cgi (username guest, password guest) and have my Top Ten Records list in a simple table...I was wondering if I could alternating colors from row to row? As in the first row is white, the next is grey?

Smile Chris Smile
Quote Reply
Re: Alternating Table Colors In Top 10 List In reply to
Right now you have

Code:
foreach my $toplist (split(/,/,$toparray)) {
%rec = (&get_record($toplist));
print qq|
<a href="$db_script_link_url&$db_key=$rec{$db_key}&view_records=1&ww=1">$rec{'FieldName'}</a>
|;
}

(Well, actually you don't. Smile But this is the code that's from the mod. I think you should be able to figure out where to make the changes.)

Change the above code to

Code:
$i = 1;
print "<table width=470 align="center" valign="top">";
foreach my $toplist (split(/,/,$toparray)) {
if ($i%2) {
print qq|<tr bgcolor="#CCCCCC">|;
}
else {
print qq|<tr bgcolor="#ffffff">|;
}
%rec = (&get_record($toplist));
$count_up= "$counter_dir/$rec{$db_key}";
open (COUNT, "<$count_up");
$countup = <COUNT>;
close COUNT;
print qq|<td width="385">
<a href="$db_script_link_url&$db_key=$rec{$db_key}&view_records=1&ww=1">$rec{'FieldName'}</a>
</td>
<td width="85"><b>$countup views</b></td>
</tr>|;
++$i;
}
print "</table>";

You may need to play around with the "CCCCCC" color. I just did that off the top of my head and I'm not sure what color that comes up with. Smile

What I have puts all of the records into one table, each in its own row, rather than each being in a table.

------------------
JPD






Quote Reply
Re: Alternating Table Colors In Top 10 List In reply to
JP,

Dang, got an error! Frown

Code:

CGI ERROR
==========================================
Error Message : Error loading required libraries.
Check that they exist, permissions are set correctly and that they compile.
Reason: syntax error at ./html.pl line 1676, near ""

Here's the code for the subroutine I have right now, for reference (I have it set to display the number of views each record has, as you helped me with before Smile)

Code:
sub html_topten {
# --------------------------------------------------------
# The subroutine to display the Top Ten most visited records


opendir (TEMPDIR, "$counter_dir") or &cgierr("unable to open directory $counter_dir. Reason: $!");
@files = readdir(TEMPDIR); # Read in list of files in directory..
closedir (TEMPDIR);
FILE: foreach $file (@files) {
next if ($file =~ /^\./); # Skip "." and ".." entries..
next if ($file =~ /^index/); # Skip index.htm type files..
open (COUNTER, "<$counter_dir/$file") or &cgierr("unable to open file $counter_dir/$file. Reason: $!");
$count{$file} = int(<COUNTER> ); close COUNTER;}
foreach $key (sort {$count{$b} <=> $count{$a} } keys %count) {
push (@top_ten,$key);}&html_print_headers;
print qq| <html>
<head><title>$html_title: Top Ten Articles</title><style>A:hover {color: #AB0303}</style></head>
<body background="http://www.warningtrack.net/images/background.gif" link="#008000" vlink="#008000" alink="#FF0000">
<table width="400" align="center" valign="top">
<tr><td><img src="http://www.warningtrack.net/images/articlearchive.jpg"></tr></td>
</table>
|;
for ($i=0;$i<10 ;$i++) { $toparray .= "$top_ten[$i],";
}chop $toparray;
$i = 1;
print "<table width=470 align="center" valign="top">";
foreach my $toplist (split(/,/,$toparray)) {
if ($i%2) {
print qq|<tr bgcolor="#DEDCDC">|;
}
else {
print qq|<tr bgcolor="#FFFFFF">|;
}
%rec = (&get_record($toplist));
$count_up= "$counter_dir/$rec{$db_key}";
open (COUNT, "<$count_up");
$countup = <COUNT>;
close COUNT;
print qq|<td width="385">
<a href="$db_script_link_url&$db_key=$rec{$db_key}&view_records=1&ww=1">$rec{'Title of Article'}</a>
</td>
<td width="85"><b>$countup views</b></td>
</tr>|;
++$i;
}
print "</table>";

&html_footer;print qq|</body></html>|;

}

Thanks! Smile
Chris
Quote Reply
Re: Alternating Table Colors In Top 10 List In reply to
My fault. Smile

Code:
print "<table width=470 align="center" valign="top">";

should be

Code:
print qq|<table width=470 align="center" valign="top">|;

Details, details, details. Smile

------------------
JPD






Quote Reply
Re: Alternating Table Colors In Top 10 List In reply to
JP,

Made the change, but got a different error! Frown

Code:
CGI ERROR==========================================
Error Message : Error loading required libraries.
Check that they exist, permissions are set correctly and that they compile.
Reason: syntax error at ./html.pl line 1685, near "$count_up = "$counter_dir/$rec{$db_key"
syntax error at ./html.pl line 1689, near "print qq|

Here's the updated subroutine code...

Code:
sub html_topten {
# --------------------------------------------------------
# The subroutine to display the Top Ten most visited records


opendir (TEMPDIR, "$counter_dir") or &cgierr("unable to open directory $counter_dir. Reason: $!");
@files = readdir(TEMPDIR); # Read in list of files in directory..
closedir (TEMPDIR);
FILE: foreach $file (@files) {
next if ($file =~ /^\./); # Skip "." and ".." entries..
next if ($file =~ /^index/); # Skip index.htm type files..
open (COUNTER, "<$counter_dir/$file") or &cgierr("unable to open file $counter_dir/$file. Reason: $!");
$count{$file} = int(<COUNTER> ); close COUNTER;}
foreach $key (sort {$count{$b} <=> $count{$a} } keys %count) {
push (@top_ten,$key);}&html_print_headers;
print qq| <html>
<head><title>$html_title: Top Ten Articles</title><style>A:hover {color: #AB0303}</style></head>
<body background="http://www.warningtrack.net/images/background.gif" link="#008000" vlink="#008000" alink="#FF0000">
<table width="400" align="center" valign="top">
<tr><td><img src="http://www.warningtrack.net/images/articlearchive.jpg"></tr></td>
</table>
|;
for ($i=0;$i<10 ;$i++) { $toparray .= "$top_ten[$i],";
}chop $toparray;
$i = 1;
print qq|"<table width=470 align="center" valign="top">";
foreach my $toplist (split(/,/,$toparray)) {
if ($i%2) {
print qq|<tr bgcolor="#DEDCDC">|;
}
else {
print qq|<tr bgcolor="#FFFFFF">|;
}
%rec = (&get_record($toplist));
$count_up = "$counter_dir/$rec{$db_key}";
open (COUNT, "<$count_up");
$countup = <COUNT>;
close COUNT;
print qq|<td width="385">
<a href="$db_script_link_url&$db_key=$rec{$db_key}&view_records=1&ww=1">$rec{'Title of Article'}</a>
</td>
<td width="85"><b>$countup views</b></td>
</tr>|;
++$i;
}
print "</table>";

&html_footer;print qq|</body></html>|;

}

Thanks! Smile
Chris
Quote Reply
Re: Alternating Table Colors In Top 10 List In reply to
My little frown faces look mean...I didn't mean it that way! Smile
Quote Reply
Re: Alternating Table Colors In Top 10 List In reply to
This time it's your fault. Smile

You have

Code:
print qq|"<table width=470 align="center" valign="top">";

and it needs to be

Code:
print qq|<table width=470 align="center" valign="top">|;

From the looks of things, you just added print qq| to the line.

If you have quotation marks within something that's being printed, you can't use quotation marks to tell Perl what to print. The alternative for the quotation marks is

qq|....|

So, in your line now, take out the first and last quotation marks and add a | just before the ;

(I know about the frowny graphic. He's not very friendly. Smile )


------------------
JPD






Quote Reply
Re: Alternating Table Colors In Top 10 List In reply to
Actually I noticed that right after posting, and I changed it, but I still get that error!

Chris
Quote Reply
Re: Alternating Table Colors In Top 10 List In reply to
Carol,

Nevermind about the error, I've fixed it...just a character or two missing...however, the Top Ten page still only displays the one color on the table (where you had CCCCCC, which I replaced with DEDCDC)... Smile

Chris
Quote Reply
Re: Alternating Table Colors In Top 10 List In reply to
I'm stumped now. This is exactly the code I use in my "bookstore" database at http://www.jpdeni.com/...ault&uid=default . Click on the "List All Books" button and you'll see it. I just use a different color than you do.

For debugging purposes, change the line

Code:
<td width="85"><b>$countup views</b></td>

to

Code:
<td width="85"><b>$countup views</b> $i</td>

Since the $i variable is a counter, I'd like to make sure it is incrementing correctly.


------------------
JPD






Quote Reply
Re: Alternating Table Colors In Top 10 List In reply to
Carol,

I did as you said and now it displays the number "8" everywhere in palce of the "$i" addition.

Chris
Quote Reply
Re: Alternating Table Colors In Top 10 List In reply to
Hey, guys.

I have an idea for fixing this problem. It brings everything down to a more simplistic form, but might do the trick. Smile

Change:

Code:
if ($i%2) {
print qq|<tr bgcolor="#DEDCDC">|;
}
else {
print qq|<tr bgcolor="#FFFFFF">|;
}

to:

Code:
if ($i == 1) {
print qq|<tr bgcolor="#DEDCDC">|;
$i = 2;
}
elsif ($i == 2) {
print qq|<tr bgcolor="#FFFFFF">|;
$i = 1;
}
else {
&cgierr("Code Syntax Error\nLost track of the Top Ten Table Colors variable. It said $i when it should have been 1 or 2.");
quit;
}

And then get rid of the ++$i;.

This is a more restricted way of performing the same task, because you specifically assign $i values, and alternate that value each time a record is displayed.

Just my .02 Smile

--Lee
Quote Reply
Re: Alternating Table Colors In Top 10 List In reply to
Lee's solution will work. Why don't you give it a try?


------------------
JPD






Quote Reply
Re: Alternating Table Colors In Top 10 List In reply to
Hi,

I gave it a try, got an error:

Code:
CGI ERROR
==========================================
Error Message : Code Syntax Error
Lost track of the Top Ten Table Colors variable. It said 6 when it should have been 1 or 2.
Script Location : /usr6/home/warningt/public_html/cgi-bin/dbman/db.cgi

Here's my current subroutine:

Code:
sub html_topten {
# --------------------------------------------------------
# The subroutine to display the Top Ten most visited records


opendir (TEMPDIR, "$counter_dir") or &cgierr("unable to open directory $counter_dir. Reason: $!");
@files = readdir(TEMPDIR); # Read in list of files in directory..
closedir (TEMPDIR);
FILE: foreach $file (@files) {
next if ($file =~ /^\./); # Skip "." and ".." entries..
next if ($file =~ /^index/); # Skip index.htm type files..
open (COUNTER, "<$counter_dir/$file") or &cgierr("unable to open file $counter_dir/$file. Reason: $!");
$count{$file} = int(<COUNTER> ); close COUNTER;}
foreach $key (sort {$count{$b} <=> $count{$a} } keys %count) {
push (@top_ten,$key);}&html_print_headers;
print qq| <html>
<head><title>$html_title: Top Ten Articles</title><style>A:hover {color: #AB0303}</style></head>
<body background="http://www.warningtrack.net/images/background.gif" link="#008000" vlink="#008000" alink="#FF0000">
<table width="400" align="center" valign="top">
<tr><td><img src="http://www.warningtrack.net/images/articlearchive.jpg"></tr></td>
</table>
|;
for ($i=0;$i<10 ;$i++) { $toparray .= "$top_ten[$i],";
}chop $toparray;
$i = 1;
print qq|<table width=470 align="center" valign="top">|;
foreach my $toplist (split(/,/,$toparray)) {
if ($i == 1) {print qq|<tr bgcolor="#DEDCDC">|;$i = 2;}elsif ($i == 2) {print qq|<tr bgcolor="#FFFFFF">|;$i = 1;}else {&cgierr("Code Syntax Error\nLost track of the Top Ten Table Colors variable. It said $i when it should have been 1 or 2.");quit;}
%rec = (&get_record($toplist));
$count_up= "$counter_dir/$rec{$db_key}";
open (COUNT, "<$count_up");
$countup = <COUNT>;
close COUNT;
print qq|<td width="385">
<a href="$db_script_link_url&$db_key=$rec{$db_key}&view_records=1&ww=1">$rec{'Title'}</a>
</td>
<td width="85"><b>$countup views</b></td>
</tr>|;
}
print "</table>";
&html_footer;

}

Chris Smile
Quote Reply
Re: Alternating Table Colors In Top 10 List In reply to
I'm not sure why, but the script is not initializing the $i variable. Let's try something else.

Instead of

Code:
if ($i == 1) {
print qq|<tr bgcolor="#DEDCDC">|;
$i = 2;
}
elsif ($i == 2) {
print qq|<tr bgcolor="#FFFFFF">|;
$i = 1;
}
else {
&cgierr("Code Syntax Error\nLost track of the Top Ten Table Colors variable. It said $i when it should have been 1 or 2.");
quit;
}

try

Code:
if ($j) {
print qq|<tr bgcolor="#DEDCDC">|;
$j = 1;
}
elsif ($j == 1) {
print qq|<tr bgcolor="#FFFFFF">|;
$j = 0;
}
else {
&cgierr("Code Syntax Error\nLost track of the Top Ten Table Colors variable. It said $j when it should have been 1 or 0.");
quit;
}


------------------
JPD






Quote Reply
Re: Alternating Table Colors In Top 10 List In reply to
I believe JPD may be right. It looks like we might be using $i too much here, or getting it scrambled with another variable in use. Smile

Another idea, if that doesn't solve it, is to change:

Code:
if ($i == 1) {print qq|<tr bgcolor="#DEDCDC">|;$i = 2;}
elsif ($i == 2) {print qq|<tr bgcolor="#FFFFFF">|;$i = 1;}
else {&cgierr("Code Syntax Error\nLost track of the Top Ten Table Colors variable. It said $i when it should have been 1 or 2.");quit;}

to:

Code:
if ($i eq 1) {print qq|<tr bgcolor="#DEDCDC">|;$i = 2;}
elsif ($i eq 2) {print qq|<tr bgcolor="#FFFFFF">|;$i = 1;}
else {&cgierr("Code Syntax Error\nLost track of the Top Ten Table Colors variable. It said $i when it should have been 1 or 2.");quit;}

I can't really see why that should make much of a difference, as == is a perfectly acceptable comparator. But other than that possibility, I can't see anything else wrong right now. Smile

--Lee
Quote Reply
Re: Alternating Table Colors In Top 10 List In reply to
JPD,

Your $j solution won't work, as if ($j) will never return true of $j=0. (your next statement is elsif ($j == 1) so the first comparator has to be if $j=0.) I believe this will have to be a positive interger, or you'd have to compare if ($j == 0) instead.

--Lee


[This message has been edited by leisurelee (edited April 24, 2000).]
Quote Reply
Re: Alternating Table Colors In Top 10 List In reply to
Yep, Lee, you're right. I don't know what I was thinking.

It should be

Code:
unless ($j) {
print qq|<tr bgcolor="#DEDCDC">|;
$j = 1;
}
elsif ($j == 1) {
print qq|<tr bgcolor="#FFFFFF">|;
$j = 0;
}

That will work! Smile


------------------
JPD






Quote Reply
Re: Alternating Table Colors In Top 10 List In reply to
Lee and Carol,

Thanks, the colors are alternating great! However the Article title/link doesn't show up (the number of views does, though)...

Here's my subroutine:

Code:
sub html_topten {
# --------------------------------------------------------
# The subroutine to display the Top Ten most visited records

opendir (TEMPDIR, "$counter_dir") or &cgierr("unable to open directory $counter_dir. Reason: $!");
@files = readdir(TEMPDIR); # Read in list of files in directory..
closedir (TEMPDIR);
FILE: foreach $file (@files) {
next if ($file =~ /^\./); # Skip "." and ".." entries..
next if ($file =~ /^index/); # Skip index.htm type files..
open (COUNTER, "<$counter_dir/$file") or &cgierr("unable to open file $counter_dir/$file. Reason: $!");
$count{$file} = int(<COUNTER> ); close COUNTER;}
foreach $key (sort {$count{$b} <=> $count{$a} } keys %count) {
push (@top_ten,$key);}&html_print_headers;
print qq| <html>
<head><title>$html_title: Top Ten Articles</title><style>A:hover {color: #AB0303}</style></head>
<body background="http://www.warningtrack.net/images/background.gif" link="#008000" vlink="#008000" alink="#FF0000">
<table width="400" align="center" valign="top">
<tr><td><img src="http://www.warningtrack.net/images/articlearchive.jpg"></tr></td>
</table>
|;
for ($i=0;$i<10 ;$i++) { $toparray .= "$top_ten[$i],";
}chop $toparray;
$i = 1;
print qq|<table width=470 align="center" valign="top">|;
foreach my $toplist (split(/,/,$toparray)) {

unless ($j) {
print qq|<tr bgcolor="#DEDCDC">|;
$j = 1;
}
elsif ($j == 1) {
print qq|<tr bgcolor="#FFFFFF">|;
$j = 0;
}
%rec = (&get_record($toplist));
$count_up= "$counter_dir/$rec{$db_key}";
open (COUNT, "<$count_up");
$countup = <COUNT>;
close COUNT;
print qq|<td width="385">
<a href="$db_script_link_url&$db_key=$rec{$db_key}&view_records=1&ww=1">$rec{'Title'}</a>
</td>
<td width="85"><b>$countup views</b></td>
</tr>|;
}
print "</table>";
&html_footer;
}

Chris
Quote Reply
Re: Alternating Table Colors In Top 10 List In reply to
According to your search form, the field that holds the title of the article is Title of Article. You might try changing

Code:
<a href="$db_script_link_url&$db_key=$rec{$db_key}&view_records=1&ww=1">$rec{'Title'}</a>

to

Code:
<a href="$db_script_link_url&$db_key=$rec{$db_key}&view_records=1&ww=1">$rec{'Title of Article'}</a>


------------------
JPD






Quote Reply
Re: Alternating Table Colors In Top 10 List In reply to
D'oh! My bad...works great! Thanks to you both! Smile

Chris