Jump to content

[SOLVED] Simple If statement to change backgound color in table


yandoo

Recommended Posts

Hi there,

 

I was hoping for al little help with my php problem. Im slightly annoyed with myself because I really thought it would be quite straightforward...

 

I have table that stores either a 1 or a 0 for each month of the year i.e. Jan, Feb, etc. along with an indexed key (vegeID). All it is meant to do is to show what vegetables can be sown on what month...

 

I then made a html table manually wrote in each month and wanted to to change the background color with a simple if statement (if =1 then output table cell color else leave as white).

 

The results im getting tho are completey mental as they seem to display the wrong results??

 

Heres the query im using:

<?php 
mysql_select_db($database_connect, $connect);
$query_outsow = "SELECT * FROM outdoorsowtime WHERE VegeID='" .
$row_vege['VegeID'] . "'";
$outsow = mysql_query($query_outsow, $connect) or die(mysql_error());
$row_outsow = mysql_fetch_assoc($outsow);
$totalRows_outsow = mysql_num_rows($outsow);
?>

 

Then i made the if statement..

<?php 
if ($row_outsow['Jan'] =1) {
      $colorjan = '#669966';
   } 
   else 
  {
      $colorjan = '#ffffff';
   }
   if ($row_outsow['Feb'] =1) {
      $colorfeb = '#669966';
   } 
   else{
      $colorfeb = '#ffffff';
   }
   
   if ($row_outsow['Mar'] = 1) {
      $colormar = '#669966';
   } 
else
  {
      $colormar = '#ffffff';
   }

if ($row_outsow['Apr'] = 1) {
      $colorapr = '#669966';
   } 
   else{
      $colorapr = '#ffffff';
   }
   
   if ($row_outsow['May'] = 1) {
      $colormay = '#669966';
   } 
   else
{
      $colormay = '#ffffff';
   }
   
   if ($row_outsow['Jun'] = 1) {
      $colorjun = '#669966';
   } else{
      $colorjun = '#ffffff';
   } 
   
   if ($row_outsow['Jul'] = 1) {
      $colorjul = '#669966';
   }  else{
      $colorjul = '#ffffff';
   }
   
   if ($row_outsow['Aug'] = 1) {
      $coloraug = '#669966';
   } else{
      $coloraug = '#ffffff';
   }
   if ($row_outsow['Sep'] = 1) {
      $colorsep = '#669966';
   } 
   else{
      $colorsep = '#ffffff';
   }
   if ($row_outsow['Oct'] = 1) {
      $coloroct = '#669966';
   } 
   else
   {
      $coloroct = '#ffffff';
   }
   
   if ($row_outsow['Nov'] = 1) {
      $colornov = '#669966';
   } 
   else{
      $colornov = '#ffffff';
   }
   
   if ($row_outsow['Dec'] = 1) {
      $colorndec = '#669966';
   } else{ 
      $colordec = '#ffffff';
   }?>

 

 

and the table where its outputted..

<table width="1214" border="1" cellspacing="1" cellpadding="1">
  <tr>
    <td width="1206" height="679"><table width="780" border="1" cellpadding="1" cellspacing="1">
      <tr>
        <td width="59" height="39"><p><strong>Sow:</strong></p>
          <p><strong>Havest:</strong></p></td>
         <td width="60" bgcolor="<?php echo $colorjan; ?>"><p>Jan <?php echo $row_outsow['Jan']; ?> </p>
          </td>
       <td width="60" bgcolor="<?php echo $colorfeb; ?>">Feb<?php echo $row_outsow['Feb']; ?></td>
    <td width="60" bgcolor="<?php echo $colormar; ?>">Mar</td><?php echo $row_outsow['Mar']; ?>
        <td width="60" bgcolor="<?php echo $colorapr; ?>">Apr</td><?php echo $row_outsow['Apr']; ?>
        <td width="60" bgcolor="<?php echo $colormay; ?>">May</td><?php echo $row_outsow['May']; ?>
<td width="60" bgcolor="<?php echo $colorjun; ?>">Jun</td><?php echo $row_outsow['Jun']; ?>
    <td width="60" bgcolor="<?php echo $colorjul; ?>">Jul</td><?php echo $row_outsow['Jul']; ?>
<td width="60" bgcolor="<?php echo $coloraug; ?>">Aug</td><?php echo $row_outsow['Aug']; ?>
   <td width="60" bgcolor="<?php echo $colorsep; ?>">Sep</td><?php echo $row_outsow['Sep']; ?>
       <td width="60" bgcolor="<?php echo $coloroct; ?>">Oct</td><?php echo $row_outsow['Oct']; ?>
        <td width="60" bgcolor="<?php echo $colornov; ?>">Nov</td><?php echo $row_outsow['Nov']; ?>
       <td width="60" bgcolor="<?php echo $colordec; ?>">Dec</td><?php echo $row_outsow['Dec']; ?>
        </tr>
    </table>

 

Even the results in the database are such that Jun and Jul are the only fields set to 1... The output is that ALL of the months are set to 1 apart from Dec which is ALSO set to 1 but displays the background that it set to 0!

 

Im really baffled here...Im probably doing something really wrong but, what was initally thought to be a easy task has become a nightmare...

 

If anybody could help that would be ace!

 

Thanks :)

 

If you do this:

 

$veg = $row_vege['VegeID'];
$query = "SELECT * FROM outdoorsowtime WHERE VegeID = '$veg'";
$result = mysql_query($query) or die(mysql_error());
$while ($row == mysql_fetch_array($result){
echo "Vege ID: ".$row['VegeID']."<br />";
echo "Jan: ".$row['Jan']."<br />";
echo "Feb: ".$row['Feb']."<br />";
echo "Mar : ".$row['Mar']."<br />";
echo "Apr: ".$row['Apr']."<br />";
echo "May: ".$row['May']."<br />";
echo "Jun: ".$row['Jun']."<br />";
echo "Jul: ".$row['Jul']."<br />";
echo "Aug: ".$row['Aug']."<br />";
echo "Sep: ".$row['Sep']."<br />";
echo "Oct: ".$row['Oct']."<br />";
echo "Nov: ".$row['Nov']."<br />";
echo "Dec: ".$row['Dec']."<br />";
}


Does it output the correct values

1. the if uses a == to check for equality, the = will define it.

 

so if ($month = 'Jan') will set $month = jan.

 

also, a switch is faster i believe according to php.net

 

so

 


switch($row_outsow['Jan']){

case 'Jan':
//do jan stuff;

BREAK;

case 'Feb':
//etc
BREAK;

}//end switch


 

should be faster

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.