I have a table in my database with a column called "Creation_date" so every entry has a creation date. In my view, I created a query to display them all in a table so every <tr>
is a while loop of every entries or row of my database.
I would like to add class name (.last) on a every last day of a month so like that I can go add a margin-bottom in CSS to separate every month. Now it’s all stuck together. Also I need to separate every 3 months for a better presentation, like grouping them.
I’m trying to have a different visual with CSS so I want to group every day of the same month and make a space to group the next month etc.
Can someone help me to think about the logic behind that ?
Year 2022
January
- Entry #1
- Entry #2
- Entry #3
[one br]
February
- Entry #1
- Entry #2
- Entry #3
[one br]
March
- Entry #1
- Entry #2
- Entry #3
[two br br]
========
April
- Entry…
[one br]
May
- Entry…
[one br]
June
- Entry…
[two br br]
========
July
- Entry…
[one br]
August
- Entry…
[one br]
September
- Entry…
[two br br]
========
October
- Entry…
[one br]
November
- Entry…
[one br]
December
- Entry…
[two br br]
Thank you
I don’t know where to start, I have the month in a variable that’s it…
EDIT:
Here is an example:
<?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>
2
Answers
In css you have a pseudo class called
:last-child
wich allows you to apply css style to the last child. Documentation herefor example, this will apply a 2px border on the last td of every tr.
you can also use it with classes or any css selector
I am not really sure to understand your code and the result that you have, but I think this example should be enough for you to solve your issue.
Another way of doing this, would be to use
<ul><li>
instead of a table (depends on how you wanna display it), and add your custom css separator on yourul
element.Hope this helped
One way to do it would be to fetch all the rows from the database, so you can compare the current element with the next one. If
$current['Creation_date_M']
is different from$next['Creation_date_M']
, then set alast-day-of-month
class to the currenttr
.But, since you are fetching one item at a time, and don’t know what is comming next, you can store
$current['Creation_date_M']
in a variable as$previousMonth
and compare with the next element in the next iteration. Using the same logic as above, if$current['Creation_date_M']
is different from$previousMonth
, then set afirst-day-of-month
class to the currenttr
. Note that you are styling the first day of the month instead of the last.Example can be seen in this PHP sandbox: https://onlinephp.io/c/b6b5a
Your code would look something like this: