Coloring Even And Odd Rows, What's the best approach?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
Cameri
Forum Commoner
Posts: 87
Joined: Tue Apr 12, 2005 4:12 pm
Location: Santo Domingo, Dominican Republic

Coloring Even And Odd Rows, What's the best approach?

Post by Cameri »

What is the best approach to determine if the row is even or odd?
The most popular way I think it's checking it this way:

Code: Select all

while (...) {
  if ($row_number % 2) {
    //odd
  } else {
    //even
  }
}
But is the popular the best one?

Another way it could be done is this:

Code: Select all

$i=0; // or =1, depends on you
while (...) {
 $i ^= 1;
  if ($i) {
    //even
  } else {
    //odd
  }
}
Another third way:

Code: Select all

$even = true;
while (...)
  if ($even) {
    //even
  } else {
    //odd
  }
  $even = !$even;
}
And so on...
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

way I usually go about it is

Code: Select all

$class = ( $i % 2 == 0 ) ? $red : $blue;

<tr class="<?php echo $class; ?>" ...


but I'm also curious about what others are doing
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Due to the need to sometimes do more elaborate coloring than plain zebra (every other), I tend to use the modulo operator.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

I generally use the modulo operator too... But in situations where absolute performance is required i might compare the counter with the number, and if they're even, reset the counter to 0...
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

timvw wrote:But in situations where absolute performance is required i might compare the counter with the number, and if they're even, reset the counter to 0...
wouldn't a boolean be faster than that?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Look, in the end, the database/file-system calls are going to weigh a lot more than a modulus operator called fifty times for a nicely colored table. Just use whatever is most readable and comfortable for you.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

Ambush Commander wrote:Look, in the end, the database/file-system calls are going to weigh a lot more than a modulus operator called fifty times for a nicely colored table. Just use whatever is most readable and comfortable for you.
I have no doubt that the bottleneck would not be in the row coloring, I asked if the boolean would be faster simply for my own knowledge's sake....I don't know and want to know.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Sorry, I wasn't trying to directly knock you, I was trying to discuss the thread as a whole.

But while we're on a theoretical basis: no, because once you incremented the variable, it would have to be cast back into an integer. Edit Wait, I think I'm misunderstanding you. What do you mean by using a boolean?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

I think i've done something like this in the past..

Code: Select all

$color1 = '#FFFFFF';
$color2 = '#000000';
$row_color = true;

while(something)
{
   if($row_color) echo $color1;  else  echo $color2; 
   $row_color = !$row_color;
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

I'm not sure what timvw is referring to, but is he talking about something like:

Code: Select all

$row = 0;
while ( ... ) {
  $even = $row % 2;
  if ($even) {
     $row = 0;
     // ...
  } else {
    // ...
  }
}
Which, if you really think about it, doesn't seem to have any positive performance effect...
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Booleans baby...

Code: Select all

<?php
$colorizationizer = true;
foreach ($array as $value)
{
    $color = ($colorizationizer) ? '#ff0000' : '#00ff00';
    echo 'The color of ' . $value . ' is ' . $color . '...';
    $colorizationizer = ($colorizationizer) ? false : true;
}
?>
User avatar
Cameri
Forum Commoner
Posts: 87
Joined: Tue Apr 12, 2005 4:12 pm
Location: Santo Domingo, Dominican Republic

Post by Cameri »

What's the speed increase if you remove the condition and always negate it instead?

Code: Select all

$colorizationizer = !$colorizationizer;
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Oren had a nifty approach...

Code: Select all

$cnt & 1
Basically you check the first bit, which changes state on every iteration.

As far as efficiency and clarity, assuming you understand how bits in a byte work...that is likely the best approach for alternate coloring...

The modulus operator is probably more expensive (in terms of clock cycles) but it really depends on what you need and what you understand.

Code: Select all

$color = $cnt & 1 ? 'black' : 'white';
echo '<div style="background-color: '.$color.'"></div>';
Cheers :)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Burrito wrote:
timvw wrote:But in situations where absolute performance is required i might compare the counter with the number, and if they're even, reset the counter to 0...
wouldn't a boolean be faster than that?
Likely yes. The modulus operator takes two numbers and divides them, returning the remainder...

Anything division on a CPU even with a dedicated FPU is a slow(er) process than whole number calculations. Boolean at the lowest level is typically an integer, so yes, in theory it would be faster.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Cameri wrote:What's the speed increase if you remove the condition and always negate it instead?

Code: Select all

$colorizationizer = !$colorizationizer;
I bet it would increase. Instead of ternary checks a simple 'make it opposite' would be faster I'd guess.
Post Reply