Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

alternate row colours in dynamic tables?

New Here ,
Jul 09, 2009 Jul 09, 2009

Hi all,

I know that alternate row colors are easily accomplished on static tables using class styles. Does any one know if this can be implemented on pages that dynamically display multiple records?

TOPICS
Server side applications
691
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jul 09, 2009 Jul 09, 2009

Yes, it's very easy to do. The actual coding depends on your server-side language. The basic principle is to use the modulo operator (%) with a counter that's incremented with each iteration of the loop.

Modulo division produces the remainder of a division sum. So, if you use modulo division on the counter, and divide the counter by 2, the result will alternate between 1 and 0, which can be used to determine which class to apply to the current row.

In PHP:

<?php $i = 0;

<?php do {

<tr <?php if($i++ %

...
Translate
LEGEND ,
Jul 09, 2009 Jul 09, 2009
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 09, 2009 Jul 09, 2009

Yes, it's very easy to do. The actual coding depends on your server-side language. The basic principle is to use the modulo operator (%) with a counter that's incremented with each iteration of the loop.

Modulo division produces the remainder of a division sum. So, if you use modulo division on the counter, and divide the counter by 2, the result will alternate between 1 and 0, which can be used to determine which class to apply to the current row.

In PHP:

<?php $i = 0;

<?php do {

<tr <?php if($i++ % 2) {echo 'class="hilite"';} ?>>

<!-- rest of code -->

</tr>

<?php } while ($row_recordsetName = mysql_fetch_array($recordsetName)); ?>

Create a style rule for all table rows, and a different one for the hilite class. Stripes.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 09, 2009 Jul 09, 2009
LATEST

Thank you so much.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines