如何使用基于条件的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.
我在html表中有这些项,从我创建的一个食物表的行中提取它们的值。
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.
我想要做的是将到期日期的文本更改为红色,因为当前日期和设置到期日期之间有3天的差异。
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.
$sql2持有用户拥有的所有食物的查询。
So how would I create a conditional that would make this work out?
那么,我该如何创建一个有条件的方案来解决这个问题呢?
2 个解决方案
#1
2
$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.
然后将$class变量添加到每个表单元。如果日期之间的差异为3天或更少,它将添加class=“过期”,否则将不添加任何内容。然后只是样式。过期的类有红色的文本。
You will most likely have to format your $date2
variable, but how to do that depends on the format it's currently in...
您很可能需要格式化$date2变量,但是如何实现这取决于它当前的格式…
更多相关文章
- MySQL -如何在最小/最大日期差异超过3年的情况下选择id
- 你可以在android webview中自动链接日期吗?
- 函数的作用是:在javascript中将时间戳转换为人工日期
- 从服务器(任何服务器)获取当前日期和时间。仅限javascript
- 输入类型=日期的日期显示为dd-mm-yyyy格式
- 我可以使用onBlur事件对TextBox进行日期检查吗?
- Day.js :一个轻量的处理时间和日期的 JavaScript 库
- 使用Isotop过滤日期
- 如何使用Require JS配置具有第三方js依赖项的日期选择器?