[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 296
  • Last Modified:

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

0
phillystyle123
Asked:
phillystyle123
  • 2
1 Solution
 
Ryan ChongCommented:
something like this?
$cnt = 0;
foreach ($results->result as $current) {
       $color = ($cnt % 2) == 0 ? "#FF0000" : $color = "#FFFFFF";
        echo '<div style="color:'.$color.'">';
                if ($current->data['phone1']=="") {echo "";} else {echo $current->data['phone1'].'<br/>';}
                if ($current->data['dr_url']=="")
        {echo $current->data['first_mi'].' '.$current->data['last'].', '.$current->data['title'].'<br/>';}
                else 
        {echo '<a href="'.$current->data['dr_url'].'" target="_blank">'.$current->data['first_mi'].' '.$current->data['last'].', '.$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>';
$cnt = $cnt + 1;	
      }

Open in new window

0
 
k_dietzCommented:
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
0
 
Ryan ChongCommented:
oops, line:

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

should be as:

echo '<div style="background-color:'.$color.'">';
0
 
phillystyle123Author Commented:
thanks everyone -went with ryancys's solution - much appreciated.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now