0

I have a page that displays a table for teams league positions, I am trying to get the top row one colour, the second row another colour, and the bottom two another colour. This is to highlight winners, promotion, and relegation. I've attached the code, you can see it's already set to add a line after the first 2 and before the last 2. Please can anyone help? I've researched but I'm struggling :( I use CSS.

    {
     //Lets print data
     $j=1;
     $i=0;
     while($i< $qty)
     {
    if(isset($draw_line))
    {
      //Tarkistetaan, piirretäänkö erotusviiva
      for($k = 0 ; $k < sizeof($draw_line) ; $k++)
      {
        if($draw_line[$k] == $i)
        {
            $templine_width = $tb_width-20;
            echo"<tr>
            <td height=\"2\" colspan=\"20\" align=\"center\" valign=\"middle\"> 
                        <img src=\"images/line.png\" width=\"$templine_width\" height=\"1\" ALT=\"\"><br>
            </td>
            </tr>";
        }
      }
    }
    echo"<tr><td align=\"center\" height=\"25\" cellspacing=\"1\" valign=\"middle\" bgcolor=\"$bg1\">$j</td>
            <td align=\"left\" valign=\"middle\" cellspacing=\"1\" bgcolor=\"$bg1\">&nbsp$team[$i]</td>
        <td align=\"center\" valign=\"middle\" bgcolor=\"$bg1\">";
    if($sort == 'pld')
        echo'<b>';
    echo"$pld[$i]";

    if($sort == 'pld')
       echo'</b>';
    echo"</td>
    <td align=\"center\" valign=\"middle\" bgcolor=\"$bg1\">";
    if($sort == 'tw')
           echo'<b>';
    echo"$wins[$i]";
    if($sort == 'tw')
           echo'</b>';
    echo"</td>
    <td align=\"center\" valign=\"middle\" bgcolor=\"$bg1\">";
    if($sort == 'td')
        echo'<b>';
    echo"$draws[$i]";
    if($sort == 'td')
            echo'</b>';
    echo"</td>
    <td align=\"center\" valign=\"middle\" bgcolor=\"$bg1\">";
    if($sort == 'tl')
            echo'<b>';
    echo"$loses[$i]";
    if($sort == 'tl')
        echo'</b>';
    echo"</td>
    <td align=\"center\" valign=\"middle\" bgcolor=\"$bg1\">";
    if($sort == 'tf')
        echo'<b>';
    echo"$goals_for[$i]";
    if($sort == 'tf')
        echo'</b>';
    echo"</td>
    <td align=\"center\" valign=\"middle\" bgcolor=\"$bg1\">";

    if($sort == 'ta')
        echo'<b>';
    echo"$goals_against[$i]";
    if($sort == 'ta')
        echo'</b>';
    echo"</td>
    <td align=\"center\" valign=\"middle\" bgcolor=\"$bg1\">";
    if($sort == 'GD')
        echo'<b>';
            $GD = $goals_for[$i] - $goals_against[$i];
            echo"$GD";
    if($sort == 'GD')
        echo'</b>';
    echo"</td>
    <td align=\"center\" valign=\"middle\" bgcolor=\"$bg1\">";
    if($sort == 'pts')
        echo'<b>';
    echo"$points[$i]";
    if($sort == 'pts')
        echo'</b>';
    echo"</td>
    </tr>";
    $i++;
    $j++;
    }
     }
}
plain jane
  • 1,009
  • 1
  • 8
  • 19
user2692939
  • 31
  • 1
  • 3

4 Answers4

2

Assign color to each like the below example

for(i=0;i=5;i++)
{
  if($i%2==0)
  {
    $col="#E6E6E6";
  }
  else
  {
    $col="#FFCCE6";
   }
?>
  <table>
   <tr bgcolor="$col">
    <td>ABCDEFG</td>
   </table>
<?php
}

Use this same logic, you can use different Styles also

Puppuli
  • 464
  • 5
  • 17
1

More simplified answer using ternary operators:

<?php
for(i=0 ; i=5 ; i++) {
  $col = ($i%2==0) ? "#E6E6E6" : "#FFCCE6";
?>
  <table>
   <tr bgcolor="<?php print $col;?>">
    <td>ABCDEFG</td>
   </table>
<?php
}
?>
Pupil
  • 23,834
  • 6
  • 44
  • 66
0

You could do it quite easily with CSS:S nth-child selector.

http://www.w3schools.com/cssref/sel_nth-child.asp

And with even-odd selectors:

http://www.w3.org/Style/Examples/007/evenodd

If you want the nth-child override the even odd, then add !important after the rule like here:

What does !important in CSS mean?

EDIT:

CSS also has first and last child options:

http://quirksmode.org/css/selectors/firstchild.html

Community
  • 1
  • 1
sleepless_in_seattle
  • 2,132
  • 4
  • 24
  • 35
0
TRy this one :

$chk_len=sizeof($draw_line);
$chk_len_n=$chk_len-1;
$chk_len_last=$chk_len-2;
for($k = 0 ; $k < sizeof($draw_line) ; $k++)
{
 if($k==0 || $k==1) // for 1st and 2nd row
 {
   $bg="#E6E6E6";
 }
 else if($k==$chk_len_last || $k==$chk_len_n) // for last two row
 {
   $bg="#FFCCE6";
 }
 else
 {$bg="#FFFFFF";}
}
Pragati
  • 16
  • 7