在PHP中,如何根据单元格值有条件地格式化行

问题描述 投票:0回答:1

鉴于以下代码,我想突出显示$listing->Full == '1'表的行。

<table id="datatable-responsive" cellspacing="0" width="100%">
   <thead>
      <tr>
         <th align="center">Qty</th>
         <th align="center">Posted Date</th>
         <th align="center">Expiration</th>
         <th align="center">Full Pkg</th>
      </tr>
   </thead>
   <tbody>
      <?php foreach($listings as $listing): 
         ?>
      <tr>
         <td align="center"><?php echo $listing->quantity; ?></td>
         <td align="center"><?php 
            $pdate = new DateTime($listing->posted_at);
            echo $pdate->format('m/d/y'); ?></td>
         <td align="center"><?php
            $date = new DateTime($listing->expdate);
            echo $date->format('m/d/y'); 
            ?>  
         </td>
         <td align="center"><?php 
            if($listing->full == '1'):
            ?>
            <?php echo "Yes"; ?>
            <?php else:
               ?>
            <?php echo "No"; ?>
            <?php endif; ?>
         </td>
         <?php endif; ?>
      </tr>
      <?php endforeach; ?> 
   </tbody>
</table>

理想情况下,高光颜色将是FFFFE6。任何帮助是极大的赞赏。这是一个MVC站点,因此更改CSS并不方便。

php html-table format row
1个回答
3
投票

在设置样式之前进行测试。这是一个内联示例:

<?php
if($listing->full == '1'):
?>
<td align="center" style="background:#FFFFE6"> 
    <?php echo "Yes"; ?>
    <?php else: ?>
<td align="center"> 
    <?php echo "No"; ?>
    <?php endif; ?>
</td>
<?php endif; ?>

以下是在CSS中使用类的示例:

CSS

.highlight {
    background: #FFFFE6;
}

PHP / HTML

<?php
if($listing->full == '1'):
?>
<td align="center" class="highlight"> 
    <?php echo "Yes"; ?>
    <?php else: ?>
<td align="center"> 
    <?php echo "No"; ?>
    <?php endif; ?>
</td>
<?php endif; ?>
© www.soinside.com 2019 - 2024. All rights reserved.