Hi all....I found a lot of tutorials about the above BUT....I'm a newbie and I would like to combine this with my code:

here is the tutorial code (I found others very similar to this):

$result = mysql_query (SELECT * FROM table);

if ($row = mysql_fetch_array($result)) {
//run the for loop
for ($i = 0; i < count($row); $i++) {
// Set Table Row Color
if ($i % 2) {
echo "<tr bgcolor=B0C4DE>";
} else {
echo "<tr bgcolor=FOF8FF>";
}
// Now display our table cell info
}

?>
<td><? echo $row["whatever"]; ?></td>
</tr>
</table>
<? }
?>

and this is my code where I have a $color variable to assign the correct font color to the belonging "ruolo"


$result = mysql_query("SELECT * FROM giocatori",$db);


<?PHP
echo "<TABLE BORDER=\"1\">\n";
echo "<TR bgcolor=\"lightblue\"><TD>Ruolo</TD><TD>Nomee</TD></TR>\n";
while($myrow = mysql_fetch_array($result))
{
$ID_giocatore = $myrow["ID_giocatore"];
$ruolo = $myrow["ruolo"];
$nome = $myrow["nome"];

if($ruolo < C)
{
$color = "RED";
}

elseif($ruolo < D)
{
$color = "GREEN";
}

elseif($ruolo < P)
{
$color = "BLUE";
}

else
{
$color = "BLACK";
}


echo "<TR><TD>\n";
echo "<font color=$color>$ruolo</font>\n";
echo "</TD>\n";
echo "<TD>\n";
echo "<font color=$color>$nome</font>\n";
echo "</TD>\n";
}
echo "</TABLE>\n";
?>
</BODY>
</HTML>

I tryed to combine the 2 things but I'm not having it working properly.....
will you be so kind to help a poor newbie ???
thanx
Roberto

    How are the ID_giocatore stored in your database? Name a few entrys.

      OK :
      ID_giocatore is a tinyINT
      ruolo is a varchar that can be P or D or C or A

      now I have managed to assign the font color with the "ruolo" RED if P, BLUE if D and so on; I was thinking about alternating the bgcolor of the rows.....

        Oké, does it work when you do this:

        if($ruolo == 'C') 
        { 
        $color = "RED"; 
        } 
        
        elseif($ruolo == 'D') 
        { 
        $color = "GREEN"; 
        } 
        
        elseif($ruolo == 'P') 
        { 
        $color = "BLUE"; 
        } 
        
        else 
        { 
        $color = "BLACK"; 
        }

          That is not a problem, my problem is to alternate the BGCOLOR of the row!!!! not the font color.... 😕 😕 😕

            ??? The bgcolor of the row??
            You're not working at all with the bgcolor of the row...

            To change the bgcolor you have to do this:

            <tr bgcolor=$color>

              Originally posted by boboli
              Hi all....I found a lot of tutorials about the above BUT....I'm a newbie and I would like to combine this with my code:

              here is the tutorial code (I found others very similar to this):

              $result = mysql_query (SELECT * FROM table);

              if ($row = mysql_fetch_array($result)) {
              //run the for loop
              for ($i = 0; i < count($row); $i++) {
              // Set Table Row Color
              if ($i % 2) {
              echo "<tr bgcolor=B0C4DE>";
              } else {
              echo "<tr bgcolor=FOF8FF>";
              }
              // Now display our table cell info
              }

              ?>
              <td><? echo $row["whatever"]; ?></td>
              </tr>
              </table>
              <? }
              ?>



              ????????????????????????????????????????????

              I was speaking about this part of the message....
              I need to alternate the BGCOLOR of the rows and I wanted to integrate the above code into my own code but I get wrong results....
              any idea on the proper way to do it ?????
              sorry for the confusion I made.....
              ciao
              Roberto

                boboli,

                in order to set the bgcolor, you have to assign your color variable to the <tr bgcolor="..."> part, not to the font color.
                try the following:

                $result = mysql_query("SELECT * FROM giocatori",$db);

                <?PHP
                echo "<TABLE BORDER=\"1\">\n";
                echo "<TR bgcolor=\"lightblue\"><TD>Ruolo</TD><TD>Nomee</TD></TR>\n";
                while($myrow = mysql_fetch_array($result))
                {
                $ID_giocatore = $myrow["ID_giocatore"];
                $ruolo = $myrow["ruolo"];
                $nome = $myrow["nome"];

                if($ruolo == 'C')
                {
                $color = "RED";
                }

                elseif($ruolo == 'D')
                {
                $color = "GREEN";
                }

                elseif($ruolo == 'P')
                {
                $color = "BLUE";
                }

                else
                {
                $color = "BLACK";
                }

                echo "<TR bgcolor=$color><TD>\n";
                echo "<font>$ruolo</font>\n";
                echo "</TD>\n";
                echo "<TD>\n";
                echo "<font>$nome</font>\n";
                echo "</TD>\n";
                }
                echo "</TABLE>\n";
                ?>
                </BODY>
                </HTML>

                  The example you quote gives the row a color based upon wether it is even or uneven. Your code assigns a color based upon a column in your table.

                    Let me explain better....I have my page with the $color working properly with the font color and the $ruolo variable.....
                    BUT I saw a tutorial to alternate the BGCOLOR of the rows with 2 different colors (say lightgrey and lightblue) for the resulting query.....I tryed to combine the code that I found (see my first message) with the code of my page.....well I've been able to have the row in alternate colors but my $color font color is no more working......
                    may you have a look to the 2 different parts of the code I posted in the very first message and help me putting them together to have the $color working properly and the rows alternating 2 different BGCOLORS ????
                    thanx
                    Roberto

                      here it is

                      if you have any prob msg me ok

                      
                      <?PHP
                      
                      $my_color_1="#FF0000" 
                      $my_color_2="#0000FF" 
                      echo "<TABLE BORDER=\"1\">\n"; 
                      $result = mysql_query("SELECT * FROM giocatori",$db); 
                      
                      $i=1;
                      
                      
                      echo "<TR bgcolor='lightblue'><TD>Ruolo</TD><TD>Nomee</TD></TR>\n"; 
                      while($myrow = mysql_fetch_array($result)) 
                      { 
                      $ID_giocatore = $myrow["ID_giocatore"]; 
                      $ruolo = $myrow["ruolo"]; 
                      $nome = $myrow["nome"]; 
                      
                      if($ruolo < C) 
                      { 
                      $color = "RED"; 
                      } 
                      
                      elseif($ruolo < D) 
                      { 
                      $color = "GREEN"; 
                      } 
                      
                      elseif($ruolo < P) 
                      { 
                      $color = "BLUE"; 
                      } 
                      
                      else 
                      { 
                      $color = "BLACK"; 
                      } 
                      if($i%2==0) {
                          $trcolor=$my_color_1;
                      } else {
                          $trcolor=$my_color_2;
                      }
                      $i++;
                      
                      echo "<TR bgcolor='$trcolor'><TD>\n"; 
                      echo "<font color=$color>$ruolo</font>\n"; 
                      echo "</TD>\n"; 
                      echo "<TD>\n"; 
                      echo "<font color=$color>$nome</font>\n"; 
                      echo "</TD>\n"; 
                      }
                      
                      echo "</TABLE>\n"; 
                      ?> 
                      </BODY> 
                      </HTML> 
                      
                      

                      forget the prev msg

                      it was a semantical blunder lol

                        I had to add a couple of ; after the color code and NOW IT ROCKS !!!!

                        thanx all guys
                        Roberto

                          Write a Reply...