Re: how to say "inverse your value" (to a boolean)?
| From: | Ashley Sheridan | Date: | Tue, 11 Aug 2009 10:37:15 +0000 |
| Subject: | Re: how to say "inverse your value" (to a boolean)? | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-296617@lists.php.net to get a copy of this message | ||
On Tue, 2009-08-11 at 11:28 +0100, David Otton wrote:
> 2009/8/11 Daevid Vincent <daevid@daevid.com>:
>
> > 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
> > }
>
> A change request just came in - the interaction designer wants every
> third line to have a grey background, instead of every second line.
>
> > 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
>
> Um. No. Just no.
>
I tend to do something like this:
$count = 0;
foreach($foo_array as $foo)
{
$class = ($count % 3 == 0)?'class="thirdRow"':'';
print "<tr $class><td>$foo</td></tr>";
}
You only need to give one row the class, as you style up all the rows
and only change the row that needs to change.
Thanks,
Ash
http://www.ashleysheridan.co.uk