PHP: Show alternate colors between rows

Posted on August 27, 2008, under PHP,  Bookmark it

Hi,

In this tutorial we will show you how to show alternate colors between (table) rows. They are nice to use in a site’s design.

First let’s define each color:

<?php
// Define colors here

$color_one = '#FFFFFF';
$color_two = '#E6F2F7';

Let’s build our sample array.

$array = array('apple','pine','strawberry',
                    'pear','banana', 'cranberry','kiwifruit');

We will use a variable that will increment with each loop:

$i = 1; // starting value
?>

Let’s output our values in table rows:

<!-- Table with alternate colors -->
<table width="100%" cellspacing="0">

How we will determine which color to show for each row?

While looping trough the array, we will check if the variable $i is an either an odd or an even number. We will determine this by checking if ($i / 2) results in an integer. If it is, then $i is even ($color_one) . Otherwise, it is odd ($color_two).

<?php
foreach($array as $value)
{
/*
Check if ($i / 2) is an integer and determine which color to show.
We will divide the number by 2
*/

$color = (is_int($i / 2)) ? $color_one : $color_two;
?>
<tr>
<td bgcolor="<?=$color?>"><?=$value?></td>
</tr>
<?php
$i++; // Increment $i
}
?>
</table>

Here’s the complete code:

<?php
// Define colors here

$color_one = '#FFFFFF';
$color_two = '#BBCCED';

$array = array('apple','pine','strawberry',
                    'pear','banana', 'cranberry','kiwifruit');

$i = 1;

?>

<!-- Table with alternate colors -->

<table width="100%" cellspacing="0">
<?php
foreach($array as $value)
{
/*
Check if ($i / 2) is an integer and determine which color to show.
We will divide the number by 2
*/

$color = (is_int($i / 2)) ? $color_one : $color_two;
?>
<tr>
<td bgcolor="<?=$color?>"><?=$value?></td>
</tr>
<?php
$i++; // Increment $i
}
?>
</table>

The output will look like this:


You can use this in any loop (for, foreach, while). Make sure you have a variable that is incrementing with each row so you can determine the alternate color.

Stay in touch
Sign up for free updates

3 Replies to "PHP: Show alternate colors between rows"

  1. I see you broke the html code just to add the php to increment the variable $i.
    Did you know you could just increment $i when your defining $color?
    Like this:
    $color = (is_int($i / 2)) ? $color_one : $color_two; $i++;

  2. Yes, I know. I usually put it before the ending } of the loop because there are many cases when I need to use it in more than one situation. Example:

    <tr>
    <td bgcolor="<?=$color?>"><?=$i?>. <?=$value?> </td>
    </tr>
    

Leave a Reply


* = required fields

  (will not be published)


XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Note: If you want to post CODE Snippets, please make them postable first!
(e.g. <br /> should be converted to &lt;br /&gt;)