Define a couple of constants (BG_COLOR1 and BG_COLOR2) in a configuration section at the head of the file (easy to find and change later).
Just before the loop where you create the items, set $a = 0.
Inside the loop do this:
// alternating bgcolor
IF ($a % 2 == 0) {
$bgcolor = BGCOLOR_1;
} ELSE {
$bgcolor = BGCOLOR_2;
}
$a++;
.. and of course stick $bgcolor in the string where you declare the table cell / row / etc code.
Don't need a function - and you can even omit the constants if you want to be super-efficient.