Changing cell color based on value?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • patsman77
    New Member
    • Nov 2007
    • 27

    Changing cell color based on value?

    Hello All,

    I have been working on a little script, I am at a slight problem and was hoping for a little assistance.

    I am trying to change the color of a cell in my table based on a value change:

    It is a "Standings" page, where If a team has been noted as a "Winner", the cell be green, or if the team is a "Loser", then the cell color be red.

    This is AMERICAN FOOTBALL...hehe

    I have it registering the correct winner if a team wins, and a counter to total the wins....now I want it to output in a table to reflect it by color.

    Here is what I have:

    [PHP]// Get all the data from the "example" table
    $result = mysql_query("SE LECT * FROM picksheet WHERE week='1'")
    or die(mysql_error ());
    echo "<table border='1'<td class='v8'>" ;
    echo "<tr><th>PoolMe ister</th> <th>Game01</th><th>Game02</th> <th>Game03</th> <th>Game04</th> <th>Game05</th> <th>Game06</th> <th>Game07</th> <th>Game08</th> <th>Game09</th> <th>Game10</th> <th>Game11</th> <th>Game12</th> <th>Game13</th> <th>Game14</th> <th>Game15</th> <th>Game16</th> <th>survivor</th> <th>margin</th> <th>Tiebreake r</th><th> Total Wins</th></tr>";
    // keeps getting the next row until there are no more to get
    while($row = mysql_fetch_arr ay( $result )) {
    // Print out the contents of each row into a table

    {

    echo "<tr><td class='v8'>";
    echo $row['user_name'];


    echo "</td><td class='v8'>";
    echo $row['Game01'];
    if('$Game01'==' $Game1Winner')
    {
    print '<TH bgcolor="green" >\n';
    }
    else
    {
    print '<TH bgcolor="red">\ n';
    }
    echo "</td><td class='v8'>";
    echo $row['Game02'];
    echo "</td><td class='v8'>";
    echo $row['Game03'];
    echo "</td><td class='v8'>";
    echo $row['Game04'];
    echo "</td><td class='v8'>";
    echo $row['Game05'];
    echo "</td><td class='v8'>";
    echo $row['Game06'];
    echo "</td><td class='v8'>";
    echo $row['Game07'];
    echo "</td><td class='v8'>";
    echo $row['Game08'];
    echo "</td><td class='v8'>";
    echo $row['Game09'];
    echo "</td><td class='v8'>";
    echo $row['Game10'];
    echo "</td><td class='v8'>";
    echo $row['Game11'];
    echo "</td><td class='v8'>";
    echo $row['Game12'];
    echo "</td><td class='v8'>";
    echo $row['Game13'];
    echo "</td><td class='v8'>";
    echo $row['Game14'];
    echo "</td><td class='v8'>";
    echo $row['Game15'];
    echo "</td><td class='v8'>";
    echo $row['Game16'];
    echo "</td><td class='v8'>";
    echo $row['Survivor'];
    echo "</td><td class='v8'>";
    echo $row['Margin'];
    echo "</td><td class='v8'>";
    echo $row['Tiebreaker'];
    echo "</td><td class='v8'>";
    echo "$Winner";
    echo "</td></tr>";

    }

    }

    echo "</table>";
    ?>



    </u>[/PHP]

    $Game1Winner is correct variable for the winner of Game01

    My table comes out with one red cell and the fields are pushed 1 spot to the right??


    Any help would be great

    Thanx,

    Patsman77
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Line 4 you need to close your <TABLE> tag.

    Also, in your 'if' statement, variables don't need to be surrounded with qoutes.

    But i can't really see from your code what's wrong. Could you include the code that tells me what your variables $Game1 and $Game1Winner are doing..?

    Comment

    • patsman77
      New Member
      • Nov 2007
      • 27

      #3
      Here is my current code:

      It is almost right, except, it pushes the fields over by 1 spot??

      [PHP]$sql="select *from matchups where week='1'";
      $result=mysql_q uery($sql);
      while ($sql = mysql_fetch_obj ect($result))
      {
      $pass[Week] = $sql -> Week;
      $pass[Favorite] = $sql -> Favorite;
      $pass[Underdog] = $sql -> Underdog;
      $pass[Spread] = $sql -> Spread;
      $pass[FavScore] = $sql -> FavScore;
      $pass[UndScore] = $sql -> UndScore;
      $pass[Time] = $sql -> Time;
      $pass[Week] = $sql -> Week;
      $pass[Favorite2] = $sql -> Favorite2;
      $pass[Underdog2] = $sql -> Underdog2;
      $pass[Spread2] = $sql -> Spread2;
      $pass[FavScore2] = $sql -> FavScore2;
      $pass[UndScore2] = $sql -> UndScore2;
      $pass[Time2] = $sql -> Time2;



      }
      mysql_select_db ("swammisp_swam misports", $con);
      //mysql_select_db ("sports", $con);
      $sql2="select *from picksheet where user_name='$_SE SSION[user_name]'";
      $result=mysql_q uery($sql2);
      while ($sql2 = mysql_fetch_obj ect($result))
      {
      $pass[Game01] = $sql -> Game01;
      $pass[Game02] = $sql -> Game02;
      $pass[Game03] = $sql -> Game03;
      $pass[Game04] = $sql -> Game04;
      $pass[Game05] = $sql -> Game05;
      $pass[Game06] = $sql -> Game06;
      $pass[Game07] = $sql -> Game07;
      $pass[Game08] = $sql -> Game08;
      $pass[Game09] = $sql -> Game09;
      $pass[Game10] = $sql -> Game10;
      $pass[Game11] = $sql -> Game11;
      $pass[Game12] = $sql -> Game12;
      $pass[Game13] = $sql -> Game13;
      $pass[Game14] = $sql -> Game14;
      $pass[Game15] = $sql -> Game15;
      $pass[Game16] = $sql -> Game16;
      $pass[Survivor] = $sql -> Survivor;
      $pass[Survivor2] = $sql -> Survivor2;
      $pass[Margin] = $sql -> Margin;
      $pass[High51] = $sql -> High51;
      $pass[High52] = $sql -> High52;
      $pass[High53] = $sql -> High53;
      $pass[High54] = $sql -> High54;
      $pass[High55] = $sql -> High55;
      $pass[Tiebreaker] = $sql -> Tiebreaker;
      $pass[Name] = $sql -> Name;
      $pass[email] = $sql -> email;





      }

      $Winner=0;
      $Game1Winner="R ";
      $Game2Winner="R ";

      // if/then/else Example
      if (($pass[FavScore]-$pass[Spread]) > ($pass[UndScore])) {
      echo "$pass[Favorite] is Winner.\n<p>"; ($Winner==$Winn er++); ($Game1Winner=" $pass[Favorite]");
      } elseif (($pass[FavScore]-$pass[Spread]) < ($pass[UndScore])) {
      // notice that PHP uses "elseif" instead of Perl's "elsif"
      echo "$pass[Underdog] wins.\n", ($Game1Winner=" $pass[Underdog]");
      } else {
      echo "Tie.\n";
      }



      if (($pass[FavScore2]-$pass[Spread2]) > ($pass[UndScore2])) {
      echo "$pass[Favorite2] is Winner.\n<p>"; ($Winner==$Winn er++); ($Game2Winner=" $pass[Favorite2]");
      } elseif (($pass[FavScore2]-$pass[Spread2]) < ($pass[UndScore2])) {
      // notice that PHP uses "elseif" instead of Perl's "elsif"
      echo "<p>$pass[Underdog2] wins.\n", ($Game2Winner=" $pass[Underdog]");
      } else {
      echo "Tie.\n";
      }


      {
      echo "<br>Winner of Game01 is $Game1Winner\n" ;
      echo "<br>Winner of Game02 is $Game2Winner\n" ;
      echo "<br>Total Wins is $Winner\n";

      }
      // Get all the data from the "example" table
      $result = mysql_query("SE LECT * FROM picksheet WHERE week='1'")
      or die(mysql_error ());
      echo "<table border='1'<td class='v8'>" ;
      echo "<tr><th>PoolMe ister</th> <th>Game01</th><th>Game02</th> <th>Game03</th> <th>Game04</th> <th>Game05</th> <th>Game06</th> <th>Game07</th> <th>Game08</th> <th>Game09</th> <th>Game10</th> <th>Game11</th> <th>Game12</th> <th>Game13</th> <th>Game14</th> <th>Game15</th> <th>Game16</th> <th>survivor</th> <th>margin</th> <th>Tiebreake r</th><th> Total Wins</th></tr>";
      // keeps getting the next row until there are no more to get
      while($row = mysql_fetch_arr ay( $result )) {
      // Print out the contents of each row into a table






      {


      echo "<tr><td class='v8'>";
      echo $row['user_name'];
      echo "</td><td class='v8'>";

      if('$Game01'==' $Game1Winner')
      {
      print '<TH bgcolor="green" >\n';
      }
      else
      {
      print '<TH bgcolor="red">\ n';
      }

      echo $row['Game01'];
      echo "</td><td class='v8'>";
      echo $row['Game02'];
      echo "</td><td class='v8'>";
      echo $row['Game03'];
      echo "</td><td class='v8'>";
      echo $row['Game04'];
      echo "</td><td class='v8'>";
      echo $row['Game05'];
      echo "</td><td class='v8'>";
      echo $row['Game06'];
      echo "</td><td class='v8'>";
      echo $row['Game07'];
      echo "</td><td class='v8'>";
      echo $row['Game08'];
      echo "</td><td class='v8'>";
      echo $row['Game09'];
      echo "</td><td class='v8'>";
      echo $row['Game10'];
      echo "</td><td class='v8'>";
      echo $row['Game11'];
      echo "</td><td class='v8'>";
      echo $row['Game12'];
      echo "</td><td class='v8'>";
      echo $row['Game13'];
      echo "</td><td class='v8'>";
      echo $row['Game14'];
      echo "</td><td class='v8'>";
      echo $row['Game15'];
      echo "</td><td class='v8'>";
      echo $row['Game16'];
      echo "</td><td class='v8'>";
      echo $row['Survivor'];
      echo "</td><td class='v8'>";
      echo $row['Margin'];
      echo "</td><td class='v8'>";
      echo $row['Tiebreaker'];
      echo "</td></tr>";
      }
      }

      echo "</table>";
      ?>



      </u>[/PHP]

      Comment

      • patsman77
        New Member
        • Nov 2007
        • 27

        #4
        Line 4 <table> tag is closed at bottom of code...


        It is also not giving right color code for value assigned?


        Thanx for the reply!!

        Patsman77

        Comment

        • patsman77
          New Member
          • Nov 2007
          • 27

          #5
          After adding a second name with values of teams picked, the second name doesnt add up the win total correctly either...ughhh


          Patsman77

          I am very new to this, so please be patient with me..I am trying very hard to learn this!!

          Comment

          • patsman77
            New Member
            • Nov 2007
            • 27

            #6
            I figured it all out!!! Thanx for the help guys!

            Patsman77

            Comment

            Working...