#native_company# #native_desc#
#native_cta#

alternate_row_colors()

By Steve Werby
on April 24, 2001

Version: 1.0

Type: Function

Category: HTML

License: GNU General Public License

Description: Alternate row colors in rows in a table. Includes three separate versions of the function to demonstrate multiple ways to achieve same effect.

<?php
/*********************************************************************
* alternate_row_colors.php
*
* Author: Steve Werby <steve at(@) befriend dot(.) com>
* Created: 2001-04-17 15:55
* Revised: 2001-04-17 16:20
* 
* Purpose: Alternate row colors in rows in a table.
*
* Latest version always available at http://www.befriend.com/.
*********************************************************************/

// The following functions allow you to alternate background colors
// of table rows.  They can easily be edited to alter rows and other
// output in other ways instead.  They can also be rewritten such
// that the code isn't within a function; however the code will have
// to be within some kind of control structure.

/*********************************************************************
* alternate_rows().
*********************************************************************/

// This version works, but is not as elegant as the second version
// which makes use of the % (modulus) operator and allows for
// more than 2 colors to alternate between.

function alternate_rows(
// color_1 and color_2 have default values, but can be over-ridden.
    $data,
    $color_1 = 'aqua',
    $color_2 = 'yellow'
)
{
// Declare $color as static so it retains its value when program
// execution leaves the local function's scope.
    static $color;

// Set the color to that's not currently set.
    if ( $color == $color_1 )
    {
        $color = $color_2;
    }
    else
    {
        $color = $color_1;
    }

    echo '<tr bgcolor="' . $color . '"><td>' . $data . '</td></tr>';
}

/*********************************************************************
* alternate_rows_2().
*********************************************************************/

// This version is a little more elegant.  It makes use of
// the % (modulus) operator and allows for more than 2 colors to
// alternate between.

function alternate_rows_2(
    $data,
    $colors = array( 'aqua', 'yellow', 'green' )
)
{
// Declare $color as static so it retains its value when program
// execution leaves the local function's scope.
    static $row_count = 0;

    $row_count++;

    if ( $row_count % 3 == 1 )
    {
        $color = $colors[0];
    }
    else if ( $row_count % 3 == 2 )
    {
        $color = $colors[1];
    }
    else
    {
        $color = $colors[2];
    }   

    echo '<tr bgcolor="' . $color . '"><td>' . $data . '</td></tr>';
}

/*********************************************************************
* alternate_rows_3().
*********************************************************************/

// This version is a even more elegant.  It makes use of
// the % (modulus) operator and allows for more than 2 colors to
// alternate between.  And it counts the number of colors passed as
// argument within the function in order to allow the code to be
// used without editing for an infinite number of colors.

function alternate_rows_3(
    $data,
    $colors = array( 'aqua', 'yellow', 'green', 'orange' )
)
{
// Declare $color as static so it retains its value when program
// execution leaves the local function's scope.
    static $row_count = 0;
        
    $row_count++;

// Calculate the number of colors passed to the function.
    $color_count = count( $colors );

    for ( $i = 0; $i <= $color_count -1; $i++ )
    {
        if ( (     $row_count - 1 ) % $color_count == $i )
        {
            $color = $colors[$i];
        }
    }
    
    echo '<tr bgcolor="' . $color . '"><td>' . $data . '</td></tr>';
}

/*********************************************************************
* Usage of alternate_rows().
*********************************************************************/

echo '<table border="0" cellpadding="0" cellspacing="0">';

for ( $i = 1; $i <= 10; $i++ )
{
    $data = $i;
    
    alternate_rows( $data );
} 
  
echo '</table>';

/*********************************************************************
* Usage of alternate_rows_2().
*********************************************************************/

echo '<br>';    
echo '<table border="0" cellpadding="0" cellspacing="0">';
    
for ( $i = 1; $i <= 10; $i++ )
{
    $data = $i;
    
    alternate_rows_2( $data );
}
     
echo '</table>';

/*********************************************************************
* Usage of alternate_rows_3().
*********************************************************************/

echo '<br>';
echo '<table border="0" cellpadding="0" cellspacing="0">';
 
for ( $i = 1; $i <= 10; $i++ )
{
    $data = $i;

    alternate_rows_3( $data );
}

echo '</table>';
?>