gauntlets
gauntlets

Reputation: 11

PHP Changing cell color when pulling in mysql table

I've been searching and searching for this answer, but either I'm not understanding it completely (probable as I'm new to php) or I can't get the right answer.

I have one php page that pulls in information from my sql table that has many different columns. This is the code......

$sql = "SELECT * FROM 2012_TNAfigures ORDER BY id DESC"; 
$query = mysql_query($sql) or die(mysql_error());
echo "<table class='tableDressing' id='2012table'><tbody>";
while ($row = mysql_fetch_array($query)){ 
$render2012 =  "<tr><td class='cellDressing'>".$row['weekday']."</td>
   <td class='cellDressing'>".$row['month']."</td>
   <td class='cellDressing'>".$row['day']."</td>
   <td class='cellDressing'>".$row['price']."</td>
   <td class='cellDressing'>".$row['month']."</td>
   <td class='cellDressing'>".$row['month']."</td>
   <td class='cellDressing'>".$row['month']."</td>
   <td class='cellDressing'>".$row['month']."</td>
   <td class='cellDressing'>".$row['month']."</td>
   <td class='cellDressing'>".$row['month']."</td>
   <td style=\"color: $openColor\">".$row['open']."</td>
   <td class='cellDressing'>".$row['close']."</td>
   <td class='cellDressing'>".$row['high']."</td>
   <td class='cellDressing'>".$row['low']."</td>
   <td class='cellDressing'>".$row['changePercentage']."</td>
   <td class='cellDressing'>".$row['volume']."</td></tr>";
echo $render2012;   
}

When that pulls the numbers in, the columns will each have different numbers that I need to apply color changes depending on the values in that cell. Here is what I've created for making the color changes....

if (($row >= 1) && ($row <= 46))
$openColor = $red;
else if (($row >= 46.01) && ($row <= 60))
$openColor = $green;

As you can see, $row doesn't work. What I don't know how to do is pull in a column, say "open", verify the value of that column in my function and then have that determine which color to use. There will be many rows thrown out for each column as well, with changing values. I assume I need to assign that column to a variable, then parse that variable through the function, but I can't seem to figure it out.

Thanks to everyone in advance. Let me know if I should supply more information.

Upvotes: 1

Views: 1142

Answers (2)

user466764
user466764

Reputation: 1176

if (($row >= 1) && ($row <= 46))
$openColor = 'Red';
else if (($row >= 46.01) && ($row <= 60))
$openColor = 'Green';

add to your td class name like this

<td class="td<?php echo $openColor; ?>" >table cell data</td>

in your css stylesheet file assign the colour values you want. I've used descriptive names here but change these to the hex colour codes you want.

.tdGreen{
   background-color: green;
}

.tdRed {
   background-color: red;
}

Upvotes: 1

Robert Wilson
Robert Wilson

Reputation: 669

$row in it self is an array. You can't test an array with a number.

if (($row['your_number'] >= 1) && ($row['your_number'] <= 46)) {
    // Do your action
}

Upvotes: 1

Related Questions