php 如何在< tr>每个月的最后一天在我的行上添加一个类来单独显示月份?

rbl8hiat  于 2023-03-11  发布在  PHP
关注(0)|答案(2)|浏览(103)

我的数据库中有一个表,它有一个名为“Creation_date”的列,因此每个条目都有一个创建日期。在我的视图中,我创建了一个查询,将它们全部显示在一个表中,因此每个<tr>都是数据库中每个条目或行的while循环。
我想在一个月的最后一天添加一个类名(.last),这样我就可以在CSS中添加一个底部边距来分隔每个月。现在它都粘在一起了。我还需要每3个月分隔一次,以便更好地演示,比如将它们分组。
我试图有一个不同的视觉与CSS,所以我想组同一个月的每一天,并作出一个空间组下个月等。
有人能帮我想想这背后的逻辑吗?

2022年

1月

  • 条目#1
  • 条目#2
  • 条目#3 [一个br]

二月

  • 条目#1
  • 条目#2
  • 条目#3 [一个br]

三月

  • 条目#1
  • 条目#2
  • 条目#3 [两个br br]

========
四月

  • 进入... [one br]

五月

  • 进入... [one br]

六月

  • 进入... [两个br br]

========
七月

  • 进入... [one br]

八月

  • 进入... [one br]

九月

  • 进入... [两个br br]

========
十月

  • 进入... [one br]

十一月

  • 进入... [one br]

十二月份

  • 进入... [两个br br]

谢谢
我不知道从哪里开始,我有一个变量中的月份就是这样...

编辑:

下面是一个例子:

<?php
  $query = '
    SELECT
      Project_ID,
      Creation_date,
      DATE_FORMAT(Creation_date, "%m") Creation_date_M,
      Project_name
    FROM Projects
    ORDER BY Creation_date DESC
  ';
  $result = $conn->query($query);
?>

<table>
  <thead>
    <tr>
      <th>Creation Date</th>
      <th>Project name</th>
    </tr>
  </thead>
  
  <tbody>
    <?php while ($row = $result->fetch_assoc()): ?>
      <?php $month = $row['Creation_date_M']; ?>
      <tr>
        <td><?= $row['Creation_date_full']; ?></td>
        <td><?= $row['Project_name']; ?></td>
      </tr>
    <?php endwhile; ?>
  </tbody>
</table>
fwzugrvs

fwzugrvs1#

在css中,你有一个伪类:last-child,它允许你将css样式应用到最后一个子类。Documentation here
例如这将在每个TR的最后一个TD上应用2px边界。

td:last-child {
    border: 2px solid orange
}

你也可以使用它与类或任何css选择器

.myClass:last-child {
    border: 2px solid orange
}

我不太确定是否理解您的代码和结果,但我认为这个示例应该足以解决您的问题。
另一种方法是使用<ul><li>而不是表格(取决于你想如何显示它),并在ul元素上添加你自定义的css分隔符。
希望这对你有帮助

r9f1avp5

r9f1avp52#

一种方法是从数据库中提取所有行,这样就可以比较当前元素和下一个元素,如果$current['Creation_date_M']$next['Creation_date_M']不同,那么就将last-day-of-month类设置为当前tr
但是,由于你一次只取一个元素,不知道下一个元素是什么,你可以把$current['Creation_date_M']存储在一个变量中,作为$previousMonth,并在下一次迭代中与下一个元素进行比较,使用与上面相同的逻辑,如果$current['Creation_date_M']$previousMonth不同,然后将一个first-day-of-month类设置为当前的tr。注意,您正在样式化一个月的第一天而不是最后一天。
示例可以在此PHP沙箱中看到:https://onlinephp.io/c/b6b5a
你的代码看起来像这样:

<?php
  $query = '
    SELECT
      Project_ID,
      Creation_date,
      DATE_FORMAT(Creation_date, "%m") Creation_date_M,
      Project_name
    FROM Projects
    ORDER BY Creation_date DESC
  ';
  $result = $conn->query($query);
?>

<table>
  <thead>
    <tr>
      <th>Creation Date</th>
      <th>Project name</th>
    </tr>
  </thead>
  
  <tbody>
    <?php
      // create a variable to store the previous month to compare with current one
      $previousMonth = null;
      while ($row = $result->fetch_assoc()):
        $month = $row['Creation_date_M'];
        if (!$previousMonth) $previousMonth = $month; 

        // if there is a change of month, define the class to be set
        if ($month != $previousMonth) {
          $previousMonth = $month;
          $trClass = "first-day-of-month";
        } else {
          $trClass = "";
        } 
    ?>
      <tr class="<?= $trClass ?>">
        <td><?= $row['Creation_date_full']; ?></td>
        <td><?= $row['Project_name']; ?></td>
      </tr>
    <?php endwhile; ?>
  </tbody>
</table>

相关问题