简体   繁体   中英

How to color column of an html table using fetched data from database

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  } 
                                        }   ?>

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).

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM