0

I want to color the column according to department and month in a table. My problem is if one department having more than one month am getting only one column colored. for eg: In IT am having april and june. But i am getting color only in june column. Can any one help me plz?

code is given below

$q_fn = mysql_query("SELECT deptname FROM functiontb ") ; 
$storeArray = Array();
while ($row = mysql_fetch_array($q_fn, MYSQL_ASSOC)) {
    $storeArray[] =  $row['deptname'];  
}
foreach($storeArray as $sa)
                            { ?>

                                 <tr >

                                <td><?php echo $sa ?></td>

                            <?php   
                                $q_audit=mysql_query("SELECT * FROM scheduletb where department = '$sa' ") );   
                                while($r= mysql_fetch_assoc($q_audit)){


                                        $month=date("F",strtotime($r['tdate']));?>

                                <td <?php 
                                    if($month == 'January'){
                                    ?> bgcolor="#1E90FF">
                                    <?php } ?>
                                </td>

                                <td <?php
                                    if($month == 'February'){
                                    ?> bgcolor="#1E90FF">
                                <?php } 
                                 ?></td>

                                <td <?php 
                                    if($month == 'March'){
                                    ?> bgcolor="#1E90FF">
                                <?php } 
                                 ?></td>

                                <td <?php 
                                    if($month == 'April'){
                                    ?> bgcolor="#1E90FF">
                                <?php } 
                                 ?></td>

                                <td <?php 
                                    if($month == 'May'){
                                    ?> bgcolor="#1E90FF">
                                <?php } 
                                 ?></td>

                                <td <?php 
                                    if($month == 'June'){
                                    ?> bgcolor="#1E90FF">
                                <?php } 
                                 ?></td>

                                <td <?php 
                                    if($month == 'July'){
                                    ?> bgcolor="#1E90FF">
                                <?php } 
                                 ?></td>

                                <td <?php 
                                    if($month == 'August'){
                                    ?> bgcolor="#1E90FF">
                                <?php } 
                                 ?></td>

                                <td <?php 
                                    if($month == 'September'){
                                    ?> bgcolor="#1E90FF">
                                <?php } 
                                 ?></td>

                                <td <?php 
                                    if($month == 'October'){
                                    ?> bgcolor="#1E90FF">
                                <?php } 
                                 ?></td>

                                <td <?php 
                                    if($month == 'November'){
                                    ?> bgcolor="#1E90FF">
                                <?php } 
                                 ?></td>

                                <td <?php 
                                    if($month == 'December'){
                                    ?> bgcolor="#1E90FF">
                                <?php } 
                                 ?> </td>
                                 </tr>

                                    <?php  } 
                                        }   ?>
Micku
  • 71
  • 1
  • 12
  • 2
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jul 31 '15 at 16:15

2 Answers2

0

Yes, it's a great advice from Jay, you should consider that, you can use Mysqli too.

For set the color you could try:

Set the color using var:

if($month == 'January'){
   var $color = '#ff9900';
}else if(...){}

and then echo the info:

echo '<td bgcolor="'.$color.'"></td>';

I think when you break html tags it gets a little confusing. Hope it helps.

(you can try to use css style tag or class to get the colors too).

0

Aside from that you are using the deprecated mysql_ functions, I see two problems related to the issue you ask about.

First, the wall of if-statements where you open and close PHP tags has resulted in you leaving off closing > from your HTML and getting invalid HTML syntax. It would be better to do something like:

<td bgcolor='<?php echo getColorByMonth($month); ?>'>put something here</td>

And write a function that just returns the color you want based on the month.

Second, if you don't include something inside your TD, the browser may not apply the style. In other words <td bgcolor='blue'></td> will probably not even show up but <td bgcolor='blue'>yo</td> will. If you want a blank cell to actually show up reliably then put a non-breaking space (&nbsp;) in it <td bgcolor='blue'>&nbsp;</td>

Third, you could also CSS, define a css class for each month, then just print the month:

<style>
.January { background-color: blue; }
.February { background-color: red; }
</style>
...
...
<td bgcolor='<?php echo $month; ?>'>&nbsp;</td>
developerwjk
  • 8,619
  • 2
  • 17
  • 33