簡體   English   中英

如果滿足條件,如何更改div的背景顏色?

[英]How can I change the background color of div if the condition is met?

這是通知代碼的代碼:

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

這是通知視圖,默認背景顏色為白色。 因此,我希望在滿足條件時將背景顏色設為紅色。

通知檢視

嘗試這個:

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

HTML:

<div <?php echo $style ?>>

</div>

如果您的條件在PHP中為true,則可以在代碼中添加以下行:

<tr bgcolor="#FF0000">

我不知道我的HTML代碼是否會出現。 請參考這個網站:

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

您可以這樣做-

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

請嘗試此代碼,我希望這是您的條件

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

嘗試

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM