2017-03-16 285 views
0

这里是通知代码的代码:如果条件符合,我如何更改div的背景颜色?

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

+1

conditio最后在标签内添加一个类,比如'class =“red”',然后在你的css中声明'.red {background-color;}'。在写这个问题之前你有什么尝试吗?你有研究过吗?这是一个低价值的问题,请考虑删除它以减少SO上的问题。 – mickmackusa

+0

你的条件是什么 - @ Ronnel Gonzales – ubm

回答

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> 
0

你可以做 -

<?php 
if(your condition) 
{ 
?> 
    <style> 
#your_div 
{ 
background:red !important; 
} 
</style> 
<?php 
} 
else 
{ 
?> 
    <style> 
#your_div 
{ 
background:white !important; 
} 
</style> 
<?php 
} 
?> 
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> 
0

尝试

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

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