Daevid Vincent wrote:
NO! For the love of God and all that is holy, don't do that accumulator /
mod "hack".
That's sooooo 1980's. And why make the CPU do all that math for every row...
Just do this. It's quick and simple:
CSS:
.dataRow1 { background-color: #DFDFDF; }
.dataRow2 { background-color: #FFFFFF; }
foreach ($foo_array as $foo) {
?><tr class="<?= ($dr = !$dr) ? "dataRow1" : "dataRow2" ?>"><td><?= $foo
?></td></tr><?php
Wow, were to start with all the problems in the above code:
1. Short tags?
2. Not using echo or print. You actually recommend breaking in/out of php?
3. Using uninitialized variables (I run with the E_ALL crowd)
4. Using the <tr class=''> and not the <td class=''>... Hmmm
}
No need to initialize $dr as by default PHP will make it a boolean "false",
then each itteration, it will toggle true/false and substitute the CSS class
-----Original Message-----
From: Jim Lucas [mailto:lists@xxxxxxxxx]
Sent: Monday, August 10, 2009 4:03 PM
To: John Butler
Cc: PHP-General List
Subject: Re: how to say "inverse your value" (to a boolean)?
John Butler wrote:
quick Q:
I have this inside a foreach{} that I want to alternate
between on and
off so I can alternate the background-color of my <tr>'s.
$tableRowBGcolorBoolCounter != $tableRowBGcolorBoolCounter;
//-boolean
on and off
I am looking thru' docs and books, but can't remember (nor
find now) in
PHP how to say "inverse your value" (to a boolean).
?
TIA! -G
<?php
$arr = range(1, 10);
$i = 0;
foreach ( $arr AS $row ) {
$row_color = ( ( $i++ % 2 ) ? 'green' : 'red');
echo $row_color;
}
?>
another (neat|strange)+ way I use the above is like so
<style>
.rowColor0 { background: #FFFFFF; }
.rowColor1,
.rowColor3 { background: #EEEEEE; }
.rowColor2 { background: #DDDDDD; }
</style>
<?php
$arr = range(1, 10);
$i = 0;
foreach ( $arr AS $row ) {
echo '<tr><td class="rowColor'.( $i++ % 4 ).'"'>'.$row.'</td></tr>';
}
?>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
Jim Lucas
"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."
Twelfth Night, Act II, Scene V
by William Shakespeare
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php