Re: How to format every secound row in a database result

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I've used the mod (%) function to do this, which also gives you the flexibility of defining how many lines to alternate the colors or do whatever you need to do every "X" lines.

Another method (and forgive me if this was mentioned, I didn't see it yet) is to use a bitwise NOT to flip-flop a value.  I think I did something like this before:

$bgcolors[0] = "#FFFFFF";
$bgcolors[1] = "#000000";

$a = 0;

echo '<table>\n';
for ($i = 0; $i <= 100; $i++) {
  $a = ~$a
  echo '<tr><td bgcolor=' . $bgcolors[$a] . '>Some text</td></tr>\n';
}
echo '</table>\n';

Maybe it was 0 and -1 instead of 0 and 1.. I forget.  But you get the idea.  The $a = ~$a flips the value of $a back and forth.  You only have two options but that maybe all you need.

And hey.. I've been known to do some goofy things so if there's some major drawback to this in processing time or if I'm just stupid in some way, that's cool.. feel free to speak up.  But it does present another way to do what was requested and the theory might help someone at least :)

-TG

= = = Original message = = =

On Mar 30, 2005, at 12:08 PM, Richard Davey wrote:

> Hello Leif,
>
> Wednesday, March 30, 2005, 6:54:15 PM, you wrote:
>
> LG> http://www.devtek.org/tutorials/alternate_row_colors.php
>
> There is no need to involve a math heavy modulus function just to
> alternate row colours! The following single line will do it just as
> well, swap the colours for whatever you need and echo them where
> required:
>
> $bgcolor = ($bgcolor === '#daf2ff' ? '#c9e1ef' : '#daf2ff');
>
> Best regards,
>
> Richard Davey


It does not involve heavy math to color every other line. I use this 
line when looping and determining if the background should be colored 
or not:

<?php
if (($i % 2) == 0) echo ' style="background: #A8B1E9;"';
?>

Obviously, the first part is the important part. Good luck!

~Philip


___________________________________________________________
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux