mySQL output question

O

old.ollyitus

Guest
Ok. Below is the code which I use to display a results table for clan matches. What I want to do is to be able to vary the background colour of each row in the table depending on whether we won or lost.

In the database there is a column called "result" which is set to Win Lose or Draw

What I want to do is set a value $bg to be a certain HEX code for each possible value of result:

ie: if the match was won colour the row blue.

I know how to colour the row from a value (ie: $bg). What I cant manage to do is to set the value.

Presumably its something like (just a guess):

$bg='#006699' when $myResults["result"]=win;
$bg='#FFFFFF' when $myResults["result"]=lose;

Can someone please explain to me how to do this.

Thanks

Lagz


======code======

<?php

$dbAddress = 'localhost';
$dbUsername = 'diss';
$dbPassword = '**************';
$dbName = 'diss';

$dbConnection = mysql_connect($dbAddress,$dbUsername,$dbPassword) or die ("Critical Error 001: Unable to connect to MySQL host");
mysql_select_db($dbName,$dbConnection) or die ("Critical Error 002: Unable to connect to the database");

$myQuery = mysql_query("SELECT * FROM results ORDER BY date DESC",$dbConnection);
$myResults = mysql_fetch_array($myQuery);

printf('<table border="0" cellpadding="3" width="720" cellspacing="1" bgcolor="#000000">');
printf('<tr>');
printf(' <td width="160" td bgcolor="#AOAOCD" align="center"><font face="Arial" color="#FFFFFF" size="2">Opposition</font></th>');
printf(' <td width="160" td bgcolor="#AOAOCD" align="center"><font face="Arial" color="#FFFFFF" size="2">Map</font></th>');
printf(' <td width="100" td bgcolor="#AOAOCD" align="center"><font face="Arial" color="#FFFFFF" size="2">Rounds Won</font></th>');
printf(' <td width="100" td bgcolor="#AOAOCD" align="center"><font face="Arial" color="#FFFFFF" size="2">Rounds Lost</font></th>');
printf(' <td width="100" td bgcolor="#AOAOCD" align="center"><font face="Arial" color="#FFFFFF" size="2">Round Diff</font></th>');
printf(' <td width="100" td bgcolor="#AOAOCD" align="center"><font face="Arial" color="#FFFFFF" size="2">Result</font></th>');
printf('</tr>');

do {

$diff = $myResults["roundswon"]-$myResults["roundslost"];

printf('<tr>');
printf(' <td bgcolor="#FFFFFF" align="center"><font face="Arial" size="2" color="#000000">%s</td>', $myResults["opposition"]);
printf(' <td bgcolor="#FFFFFF" align="center"><font face="Arial" size="2" color="#000000">%s</td>', $myResults["map"]);
printf(' <td bgcolor="#FFFFFF" align="center"><font face="Arial" size="2" color="#000000">%s</td>', $myResults["roundswon"]);
printf(' <td bgcolor="#FFFFFF" align="center"><font face="Arial" size="2" color="#000000">%s</td>', $myResults["roundslost"]);
printf(' <td bgcolor="#FFFFFF" align="center"><font face="Arial" size="2" color="#000000">%s</td>', $diff);
printf(' <td bgcolor="#FFFFFF" align="center"><font face="Arial" size="2" color="#000000">%s</td>', $myResults["result"]);
printf('</tr>');
} while ($myResults = mysql_fetch_array($myQuery));

echo "</table>";
?>
 
T

Teaser

Guest
PHP:
while ( list ($opp, $map, $rounds_won, $rounds_lost, $result) = mysql_fetch_row ($myQuery) )
{
      echo "<tr bgcolor = #". ($result == "win" ? "006699" : "ffffff") ." align = center>
              <td>$opp</td>
              <td>$map</td>
              <td>$rounds_won</td>
              <td>$rounds_lost</td>
              <td>". $rounds_won - $rounds_lost ."</td>
              <td>$result</td>
           </tr>
            " ;
}

Look-up mysql_fetch_row () and list (). They are far neater than mysql_fetch_array () etc.

Use style sheets instead of repeating the HTML.
 

Users who are viewing this thread

Top Bottom