Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Alternating Table Row Colors Help!

by Anonymous Monk
on May 05, 2010 at 18:00 UTC ( [id://838553]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
I have this part of my code here to demonstrated what I am trying to do, which is alternating the colors of the rows based on the query results. I know there are many ways of doing this using odd and even numbers, but because of the nature of this particular query results the values of the counter "$i" will not be in order or sequence, this block of code will print the values of $i as:
0 2 14 15 16 19 20
I am thinking that the only way is to compare the values of $i and if they are different assign a value I could use to do the color alternation, but I am running out of ideas, any help would be very appreciated, thanks for the help!
for (my $i = 0; $i < @$sql; $i++) { my $user = $sql->[$i]{user}; if($unique{$user}) { next; } $unique{$user} = 1; my $show = "<a href=\"show.pl?name=$name\">$name</a>"; my $first = $sql->[$i]{first}; my $last = $sql->[$i]{last}; my $tel = $sql->[$i]{tel}; my $email = $sql->[$i]{email}; + if ($i % 2) { $data = "$data<tr><td>$last, $first</td> <td>$show</td> <td>$email</td> </tr>"; }else { $data = "$data<tr><td>$last, $first</td> <td>$show_link</td> <td>$email</td> </tr>"; } }

Replies are listed 'Best First'.
Re: Alternating Table Row Colors Help!
by samarzone (Pilgrim) on May 05, 2010 at 18:14 UTC

    Use a separate variable instead of $i as a flag

    my $flag = 0; for (my $i = 0; $i < @$sql; $i++) { ... $unique{$user} = 1; $flag++; # increase the variable ... ... if ($flag % 2) ... ... }
Re: Alternating Table Row Colors Help!
by Herkum (Parson) on May 05, 2010 at 21:33 UTC
Re: Alternating Table Row Colors Help!
by Cody Fendant (Hermit) on May 06, 2010 at 00:48 UTC

    Use HTML::Template and its loop_context_vars option?

    You use it like <tmpl_if name="__even__"> [your code here] </tmpl_if>.

      I immediately thought HTML::Template or jQuery.

      Here's an HTML::Template approach:

      <table class="newsitems"> <tr> <th>Title</th> <th>Author</th> </tr> <tmpl_if list> <tmpl_loop list> <tmpl_if name="__odd__"> <tr style="background-color: #fff"> <tmpl_else> <tr style="background-color: #eee"> </tmpl_if> <td><tmpl_var title></td> <td><tmpl_var author></td> </tmpl_loop> <tmpl_else> <tr> <td>No News Items recorded</td> </tr> </tmpl_if> </table>

      Another nice touch is to highlight rows as the mouse hovers over them:

      CSS: .follow_mouse tr:hover td { background: #A4D9F7; } /* light blue */ HTML: <table class="follow_mouse">
      —Brad
      "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
Re: Alternating Table Row Colors Help!
by arc_of_descent (Hermit) on May 06, 2010 at 08:27 UTC

    Another way to do this, is to first process the @$sql array and get all the unique users. You can then loop over the unique users and use $i as intended.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://838553]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-24 07:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found