如果符合条件,如何更改div的背景颜色?

时间:2017-03-16 06:59:31

标签: php codeigniter

以下是通知代码的代码:

<?php if(isset($notification) && $notification->result() >0 ){?>
    <div class="panel-heading pull-right col-md-6"
    style="height:140px; margin-top:-140px; background-color:white; overflow-y: scroll;">
        <h5><strong>Student with 3 Consecutive Absences</strong></h5>
            <table class="table">
                <thead>
                    <th>Course Code-Section</th>
                    <th>Student Name</th>
                    <th>View</th>
                </thead>
            <tbody>
                <?php foreach($notification->result() as $notif)
                {
                    if($notif->Total >=3)
                    {
                    ?>
                        <tr>
                            <td><?php echo $notif->Course_Code_Section?</td>
                            <td><?php echo $notif->Student?></td>
                            <td>
                                <form action="<?php echo base_url();?>index.php/attendance/check" method="post" target="_blank">
                                    <input type="hidden" name="CID" value="<?php echo $notif->CID;?>">
                                    <button class="btn btn-xs btn-success"><i class="icon-eye-open"></i> View</button>
                                <?php echo form_close();?>
                            </td>
                        </tr>
                        </div>
                        <?php }?>
                    <?php }?>
                </tbody>
            </table>
    </div>
<?php }?>

这是通知视图,默认背景颜色为白色。因此,我希望它在满足条件时使背景颜色变为红色。

Notification View

5 个答案:

答案 0 :(得分:1)

试试这个:

<?php
$style = '';
if(isset($notification) && $notification->result() > 0 )
{
    $style = 'style="color:red"';
    // Put your css style property here
}
?>

HTML:

<div <?php echo $style ?>>

</div>

答案 1 :(得分:0)

如果您的条件在PHP中为真,那么您可以在标记中添加以下内容:

<tr bgcolor="#FF0000">

我不知道我的HTML代码是否会出现。请参考以下网站:

https://www.w3schools.com/tags/att_tr_bgcolor.asp

答案 2 :(得分:0)

你可以做 -

<?php
if(your condition)
{
 ?>   
   <style>
#your_div
{
 background:red !important;
}
</style>
<?php
}
else
{
?>   
   <style>
#your_div
{
 background:white !important;
}
</style>
<?php
}
?>

答案 3 :(得分:0)

请尝试此代码,我希望这是您的条件

 <?php 
$bcolor ='background-color:white';
if(isset($notification) && $notification->result() >0 ){
$bcolor ='background-color:white';
 }?>
    <div class="panel-heading pull-right col-md-6"
    style="height:140px; margin-top:-140px; <?php echo $bcolor ;?> overflow-y: scroll;">
        <h5><strong>Student with 3 Consecutive Absences</strong></h5>
            <table class="table">
                <thead>
                    <th>Course Code-Section</th>
                    <th>Student Name</th>
                    <th>View</th>
                </thead>
            <tbody>
                <?php foreach($notification->result() as $notif)
                {
                    if($notif->Total >=3)
                    {
                    ?>
                        <tr>
                            <td><?php echo $notif->Course_Code_Section?</td>
                            <td><?php echo $notif->Student?></td>
                            <td>
                                <form action="<?php echo base_url();?>index.php/attendance/check" method="post" target="_blank">
                                    <input type="hidden" name="CID" value="<?php echo $notif->CID;?>">
                                    <button class="btn btn-xs btn-success"><i class="icon-eye-open"></i> View</button>
                                <?php echo form_close();?>
                            </td>
                        </tr>
                        </div>
                        <?php }?>
                    <?php }?>
                </tbody>
            </table>
    </div>

答案 4 :(得分:0)

试试

<?php 
    $bg_red = '';
    if (condition) {
       $bg_red = '<style="background-color: red;">';
    }
?>

<tr <php? echo $bg_red; ?>>
   <td></td>
   <td></td>
   <td></td>
</tr>