• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 298
  • 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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