Hi, Just trying to implement code below for coloring alternate rows in a table, but think I may have got the code wrong somewhere, it shows $title\n"; } ?> on the preview of the page ? ?

<?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 much appreciated.

Joad

    I take it this isn't your whole code. What does your items variable look like?

      Here's the complete 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";
      }
      ?>

      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
      "http://www.w3.org/TR/html4/loose.dtd">
      <html>
      <head>
      <meta name="description" content="Test">
      </head>

      <body>

      <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'>title4</a></td></tr>
      <tr><td><a href="link5" target='blank'>title5</a></td></tr>
      <tr><td><a href="link6" target='
      blank'>title6</a></td></tr>
      </table>

      </body>

      </html>

      Any help very much appreciated.

      Joad.

        Kudose wrote:

        Where is $items being set?

        $bgcolor = 'F00';
        foreach($items as $key => $item){
          echo '<tr><td style="background-color:#';
          echo ($bgcolor == 'F00') ? '00F' : 'F00';
          echo ';"><a href="'.$item['link'].'" title="'.$item['title'].'">'.$item['title'].'</a></td></tr>';
        }
        

        Sorry Kudose, I'm drastically getting stuck on this 😕 I was just trying to color each row automatically, as was mentioned on the thread below, but can't to be able to do it 😕

        http://phpbuilder.com/board/showthread.php?t=10337132

        Any help very much appreciated.

        Joad.

          OK. Here is a starter. I get the idea that you don't really need the link but you can add it back in with some simple code and change the number to the link. I'll let you work that bit out yourself if you need it. This will just make a table with the alternate colours and have the words from the array in each cell from the table:

          
          
          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
          "http://www.w3.org/TR/html4/loose.dtd">
          <html>
          <head>
          <meta name="description" content="Test">
          </head>
          
          <body>
          
          <table border='0' cellpadding='0' cellspacing='0' width='200'>
          <?php
          $items = array('1' => 'hello', '2'=>'world', '3'=>'of', '4'=>'php');
          $i = 0;
          foreach ($items as $item) {
          if ($i%2 == 1)
          {$bgcolor = 'red';} else {$bgcolor = 'blue';}
          $i++;
          echo "<tr><td bgcolor=\"$bgcolor\">$item</td></tr>\n";
          }
          ?>
          </table>
          
          </body>
          
          </html>

            Many thanks for the input, it's appreciated.

            The above table in my earlier thread was just an example, in reality, there's a few hundred rows and rows are constantly added and taken away and need the rows to automatically display different colors for each row, without the need to manually alter any row in the table - will your code do this please ?

            I think I've explained fully, but if not, please let me know and I'll expand on it a tad more.

            The help is very much appreciated.

            Joad.

              Where is this information stored? Is it stored on your site or another server? Have you got the other rows of information in a set of variables or an array by any chance? If you just make an array of all the information and call it $items as above then it will work.

                Not sure what other info there is, maybe best to forget about the php code that was in there, so starting again, just trying to get the rows to change colors, the table below is just an example, in reality, there's a few hundred rows, and rows are constantly added and taken away and need the rows to automatically display different colors for each row, without the need to manually alter any row in the table.

                <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                "http://www.w3.org/TR/html4/loose.dtd">
                <html>
                <head>
                <meta name="description" content="Test">
                </head>

                <body>

                <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'>title4</a></td></tr>
                <tr><td><a href="link5" target='blank'>title5</a></td></tr>
                <tr><td><a href="link6" target='
                blank'>title6</a></td></tr>
                </table>

                </body>

                </html>

                Any help very much appreciated.

                Joad.

                  So your table is already written out in html. If that is the case then you can't use php to alter it unless you alter your tables. I think this just needs some css and javascript. It will be pretty difficult to do if you have already written out the table. Do you have the link to the site that this is on?

                    Please don't create multiple threads for the same problem. Even if it is in another board of the site.

                      I wouldn't usually, but when I posted on the other thread and got no replies, I thought I had done wrong, as it had been resolved on the other thread. Anyway, no worries.

                        I think this just needs some css and javascript.

                        I agree. This may be a better solution than the use of PHP.

                          Kudose says it is possible ?

                          By "automatically changing colors", Kudose means that the table is changed on reload. If you want the table to be changed without reload, then you need AJAX techniques, which means PHP alone is not enough.

                          However, even if you only want the table to be changed on reload, CSS and client side scripting is more appropriate than PHP here.

                            The poster did succeed using the method that loads the colors when the page loads. The poster did not desire dynamic color changing, which would have required JavaScript.

                            Are you looking to have the rows alternating when the page loads, or do you want that and/or dynamic color changes (i.e. clicking a row "highlights" it)?

                              Kudose wrote:

                              The poster did succeed using the method that loads the colors when the page loads. The poster did not desire dynamic color changing, which would have required JavaScript.

                              Are you looking to have the rows alternating when the page loads, or do you want that and/or dynamic color changes (i.e. clicking a row "highlights" it)?

                              Many thanks for sticking with this one Kudose.

                              The one I'm trying to do, is your second one ie :

                              To have the rows alternating when the page loads, and dynamic color changes (i.e. clicking a row (and mousing over a row) "highlights" it)?