duanliang9288 2014-04-02 23:02 采纳率: 100%
浏览 175
已采纳

如何使用PHP基于条件更改CSS属性

I'm working on a website that manages food. Keeping track of expiration dates and the such.

When a users dashboard loads, it shows a list of the food items they've currently added along with info about them, Brand, Location (Fridge,Pantry,Freezer), and expiration date.

I have the items in a html table, drawing their values from php echos of the rows of a food table I created.

What I want to do is change the text of the expiration date to red when there is about a 3 day difference between the current date and the set expiration date.

Here's my code for putting the values in the table.

       <?php 

while($rows = mysql_fetch_array($sql2))

    {
?> 

            <tr>
              <td><b><?php echo $rows['Name'] ?></td>
              <td><b><?php echo $rows['Brand'] ?></td>
              <td><b><?php echo $rows['Type'] ?></td>
              <td><b><?php echo $rows['Container'] ?></td>
              <td><b><?php echo $rows['ExpirationDate'] ?></td>
              </tr>


            <?php

    }
    ?>

$sql2 holds a query of all the foods owned by the user.

So how would I create a conditional that would make this work out?

  • 写回答

2条回答 默认 最新

  • douluoqiu4538 2014-04-02 23:10
    关注
    $date1 = new DateTime("now");
    $date2 = $rows['ExpirationDate'];
    $difference = $date1->diff($date2);
    $days = $difference->d;
    $class = '';
    if ($days <= 3)
        $class = 'class="expired"';
    

    Then add the $class variable to each table cell. If the difference between dates is 3 days or less, it will add class="expired" otherwise it will add nothing. Then just style the .expired class to have red text.

    You will most likely have to format your $date2 variable, but how to do that depends on the format it's currently in...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记