I have this piece of code:

foreach ($items as $item) {
   $link = $item['link'];
   $title = $item['title'];

   echo "<tr><td><a href=\"$link\" target='_blank'>$title</a></td></tr>\n";
}

Combined with the rest of the code outputs this:

<table border='0' cellpadding='0' cellspacing='0' width='200'>
<tr><td><a href="link1" target='_blank'>title1</a></td></tr>
<tr><td><a href="link2" target='_blank'>title2</a></td></tr>
<tr><td><a href="link3" target='_blank'>title3</a></td></tr>
<tr><td><a href="link4" target='_blank'>title1</a></td></tr>
<tr><td><a href="link4" target='_blank'>title1</a></td></tr>
<tr><td><a href="link5" target='_blank'>title5</a></td></tr>
</table>

But what I would like is for each line to be a different color, like this:

<table border='0' cellpadding='0' cellspacing='0' width='200'>
<tr><td bgcolor="red"><a href="link1" target='_blank'>title1</a></td></tr>
<tr><td bgcolor="blue"><a href="link2" target='_blank'>title2</a></td></tr>
<tr><td bgcolor="red"><a href="link3" target='_blank'>title3</a></td></tr>
<tr><td bgcolor="blue"><a href="link4" target='_blank'>title1</a></td></tr>
<tr><td bgcolor="red"><a href="link4" target='_blank'>title1</a></td></tr>
<tr><td bgcolor="blue"><a href="link5" target='_blank'>title5</a></td></tr>
</table>

Is this possible?

    $array = array(1,2,3,4,5);
    
    echo '<table>';
    foreach ($array as $key => $value)
    {
    	if ($key & 1) {$bgcolor = 'red';} else {$bgcolor = 'blue';}
    	echo '<tr><td bgcolor="' . $bgcolor . '">' . $value . '</td></tr>';
    }
    echo '</table>';
    

      Well in order to get it to work with the other foreach I did this

      <?php
      $array = array(1,2,3,4,5);
      echo "<table border='0' cellpadding='0' cellspacing='0' width='200'>\n";
      echo "<tr><td><em>Recent Posts:</em></td></tr>\n";
      
      foreach ($array as $key) {
      	if ($key & 1) {$bgcolor = 'red';} else {$bgcolor = 'blue';}
      	$tdcolor = "<tr><td bgcolor=\"$bgcolor\">";
      }
      foreach ($items as $item) {
         $link = $item['link'];
         $title = $item['title'];
      
         echo "$tdcolor<a href=\"$link\" target='_blank'>$title</a></td></tr>\n";
      }
      
      echo "</table>";
      ?>

      But every line is coming out as bgcolor="red" 😕

        Maybe I am wrong, but I think this should work:

        
        // if your array has numeric keys, you could use those. Else:
        $i = 0;
        foreach ($items as $item) {
           if ($i%2 == 1) 
            {$bgcolor = 'red';} else {$bgcolor = 'blue';}
           $i++;
           $link = $item['link'];
           $title = $item['title'];
        
           echo "<tr><td bgcolor=\"$bgcolor\"><a href=\"$link\"   target='_blank'>$title</a></td></tr>\n";
          }
        

          my code example was just that: an example, not to be used directly in your code. i was simply trying to demonstarte how to use odd/even numeric keys to alternate between 2 different colors.

            leatherback wrote:

            Maybe I am wrong, but I think this should work:

            
            // if your array has numeric keys, you could use those. Else:
            $i = 0;
            foreach ($items as $item) {
               if ($i%2 == 1) 
                {$bgcolor = 'red';} else {$bgcolor = 'blue';}
               $i++;
               $link = $item['link'];
               $title = $item['title'];
            
               echo "<tr><td bgcolor=\"$bgcolor\"><a href=\"$link\"   target='_blank'>$title</a></td></tr>\n";
              }
            

            That works, thanks 🙂

              EPJS wrote:

              That works, thanks

              And, for added points, can you explain how it works? Or don't you know what your own code does any more?

                What Weedpacket was asking was for you to explain how the colours are changed every row.
                Yes you got it to work, but can you make every 3 rows a different colour using very similar code?

                Doing is not the same as knowing.

                  HalfaBee wrote:

                  Yes you got it to work, but can you make every 3 rows a different colour using very similar code?

                  [pointing annoying finger to the ceiling] I know! I know! Can I tell please!! [/pointing annoying finger to the ceiling]

                  But yes. Absolutely right. He is probably back next week because he wants alternating colors for colums and can't figure out how to do it. I was possibly a bit lazy by just posting a solution, without letting him potter a bit.

                  J.

                    EPJS might be a supreme overlord, but he probably does not know a % from a &.

                      leatherback wrote:

                      [pointing annoying finger to the ceiling] I know! I know! Can I tell please!! [/pointing annoying finger to the ceiling]

                      But yes. Absolutely right. He is probably back next week because he wants alternating colors for colums and can't figure out how to do it. I was possibly a bit lazy by just posting a solution, without letting him potter a bit.

                      J.

                      🙁

                      HalfaBee wrote:

                      What Weedpacket was asking was for you to explain how the colours are changed every row.
                      Yes you got it to work, but can you make every 3 rows a different colour using very similar code?

                      Doing is not the same as knowing.

                      I'm not a complete idiot, I was able to "read" how the code works after I saw it in print, so I can adjust it to my liking :p

                      HalfaBee wrote:

                      EPJS might be a supreme overlord, but he probably does not know a % from a &.

                      % gives the remainder after you divide (remember long division from highschool?)

                      Now what I'd like to know, is why you all seem to be giving me a hard time about this?

                        EPJS wrote:

                        Now what I'd like to know, is why you all seem to be giving me a hard time about this?

                        See devinemke's posts.
                        (Incidentally, if you have looked at the Coding Forum's FAQ you'd have seen links to threads that already covered exactly your problem.)

                          Weedpacket wrote:

                          See devinemke's posts.
                          (Incidentally, if you have looked at the Coding Forum's FAQ you'd have seen links to threads that already covered exactly your problem.)

                          Well I just think that people shouldn't be ridiculed just because they don't know something 🙁

                            Sorry, if you thought I was ridiculing you.

                            It wasn't about your knowledge, but about the post you deleted.

                            All this over a bit of modulus. <sigh>

                              HalfaBee wrote:

                              All this over a bit of modulus. <sigh>

                              Excellent point! I see no reason for further commentary in this thread... both sides have heard the reasoning/response to the somewhat harsh yet hopefully constructive criticism.

                                a year later
                                leatherback wrote:

                                Maybe I am wrong, but I think this should work:

                                
                                // if your array has numeric keys, you could use those. Else:
                                $i = 0;
                                foreach ($items as $item) {
                                   if ($i%2 == 1) 
                                    {$bgcolor = 'red';} else {$bgcolor = 'blue';}
                                   $i++;
                                   $link = $item['link'];
                                   $title = $item['title'];
                                
                                   echo "<tr><td bgcolor=\"$bgcolor\"><a href=\"$link\"   target='_blank'>$title</a></td></tr>\n";
                                  }
                                

                                Hi, Just trying to implement this, but think I may have got the code wrong somewhere, I did copy and paste the code above, then added codes for starting and ending the php code, but it then shows $title\n"; } ?> on the preview of the page ? ? Here's the code :

                                <?php
                                $i = 0;
                                foreach ($items as $item) {
                                   if ($i%2 == 1)
                                    {$bgcolor = 'red';} else {$bgcolor = 'blue';}
                                   $i++;
                                   $link = $item['link'];
                                   $title = $item['title'];
                                
                                   echo "<tr><td bgcolor=\"$bgcolor\"><a href=\"$link\"   target='_blank'>$title</a></td></tr>\n";
                                  } 
                                ?>
                                

                                Any help appreciated.

                                Joad

                                  This works just fine for me.

                                  <?php
                                  $items = array(
                                            array('link' => 'google.com', 'title' => 'Google'), 
                                            array('link' => 'spechal.com', 'title' => 'Spechal'), 
                                            array('link' => 'froogle.com', 'title' => 'Froogle'), 
                                            array('link' => 'dell.com', 'title' => 'Dell')
                                           );
                                  $i = 0;
                                  echo '<table>';
                                  foreach ($items as $item) {
                                     $bgcolor = ($i++ % 2 == 0) ? 'red' : 'blue';
                                     $link = $item['link'];
                                     $title = $item['title'];
                                  
                                     echo "<tr><td bgcolor=\"$bgcolor\"><a href=\"$link\" target='_blank'>$title</a></td></tr>\n";
                                    }
                                  echo '</table>';
                                  ?> 

                                  I was pretty sure I asked this once before, but I think someone deleted the post ... Where is $items being set?

                                    Kudose wrote:

                                    This works just fine for me.

                                    <?php
                                    $items = array(
                                              array('link' => 'google.com', 'title' => 'Google'), 
                                              array('link' => 'spechal.com', 'title' => 'Spechal'), 
                                              array('link' => 'froogle.com', 'title' => 'Froogle'), 
                                              array('link' => 'dell.com', 'title' => 'Dell')
                                             );
                                    $i = 0;
                                    echo '<table>';
                                    foreach ($items as $item) {
                                       $bgcolor = ($i++ % 2 == 0) ? 'red' : 'blue';
                                       $link = $item['link'];
                                       $title = $item['title'];
                                    
                                       echo "<tr><td bgcolor=\"$bgcolor\"><a href=\"$link\" target='_blank'>$title</a></td></tr>\n";
                                      }
                                    echo '</table>';
                                    ?> 

                                    I was pretty sure I asked this once before, but I think someone deleted the post ... Where is $items being set?

                                    Don't know, but I thought this thread was about each row automatically changing colors, so was trying to find out more about it, but have now been told on another thread here, that it isn't possible ?