Chas Owens wrote:
> On 2/25/06, henry chen <[EMAIL PROTECTED]> wrote:
>>I can't seem to figure out how to alternate the bgcolor for each row that
>>i'm printing from arrayref.  Is there a 'foreveryother' function that would
>>allow me to put two lines in the loop?  Or is there someway I can put two
>>rows of information and loop that?  Here's what I have so far.....
>>
>>my $rows = $dbhandle->selectall_arrayref($sql) || die $dbhandle->errstr;
>>
>>        if (@$rows) {
>>          print "<center><table border=1 cellspacing=0 cellpadding=3
>>width='70%'><tr>" .
>>"<th>Title</th><th>Description</th><th>Price</th><th>Email</th></tr>";
>>          foreach my $row (@$rows) {
>>            print "<tr><td>" . join ("</td><td>", @$row) . "</td></tr>\n";
>>          }
>>          print "</table>\n</center>\n";
>>        }
>>        else {
>>          print "<p><i>No matches found</i></p>\n";
>>        }
> 
> Use the module operator (%) with an if statement to do different
> things in a loop:
> 
> my $rows = $dbhandle->selectall_arrayref($sql) || die $dbhandle->errstr;
> if (@$rows) {
>     print "<center><table border=1 cellspacing=0 cellpadding=3
> width='70%'><tr>" .
>         "<th>Title</th><th>Description</th><th>Price</th><th>Email</th></tr>";
>     my $i = 1;
>     foreach my $row (@$rows) {
>         my $color;
>         if ($i % 2) { #odd rows
>             $color = "#303030";
>         } else { #even rows
>             $color = "#000000";
>         }
>         print qq(<tr bgcolor="$color"><td>) . join ("</td><td>",
> @$row) . "</td></tr>\n";
>     }
>     print "</table>\n</center>\n";
> } else {
>     print "<p><i>No matches found</i></p>\n";
> }

Or use an array:

my @colours = qw[ #303030 #000000 ];

# ...

    for my $row ( @$rows ) {
        my $bgcolour = shift @colours;
        print qq(<tr bgcolor="$bgcolour">),
              map( "<td>$_</td>", @$row ),
              "</tr>\n";
        push @colours, $bgcolour;
    }



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to