Link to home
Start Free TrialLog in
Avatar of phillystyle123
phillystyle123Flag for United States of America

asked on

alternate colors with loop using div (not tables)

trying to alternate colors with this loop. absolutely no idea how to do it!
foreach ($results->result as $current) {
        echo '<div>';
		if ($current->data['phone1']=="") {echo "";} else {echo $current->data['phone1'].'<br/>';}
		if ($current->data['dr_url']=="")
        {echo $current->data['first_mi'].'&nbsp;'.$current->data['last'].',&nbsp;'.$current->data['title'].'<br/>';}
		else 
        {echo '<a href="'.$current->data['dr_url'].'" target="_blank">'.$current->data['first_mi'].'&nbsp;'.$current->data['last'].',&nbsp;'.$current->data['title'].'</a><br/>';}
		if ($current->data['group']=="") {echo "";} else {echo $current->data['group'].'<br/>';}
        echo $current->data['address1'].', '.$current->data['address2'].'<br/>';
        echo $current->data['city'].', '.$current->data['state'].', '.$current->data['zip'].'<br/>';
        printf("%d %s", $current->distance, $units);
        echo '</div>';
      }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is what I do...

Before the loop:
$x=0;

During the loop:

If ($x % 2 = 0){
       // Show code with color 1
}Else {
       // Show code with color 2
}
$x++;

End loop
oops, line:

echo '<div style="color:'.$color.'">';

should be as:

echo '<div style="background-color:'.$color.'">';
Avatar of phillystyle123

ASKER

thanks everyone -went with ryancys's solution - much appreciated.