I am building a calender in PHP.
In the controller, I have detected the amount of days in a given month, and set that range into an array: daysInMonthArray.
In the view, I then foreach this array outputting each number into a <td>:
<tr>
<?php
// output the number of days in the month
foreach($this->daysInMonthArray as $days1){
foreach($days1 as $key => $object){
echo "<td>" . $object . "</td>";
}
} ?>
</tr>
I would like to start a new <tr> every 8th number, as there are 7 days in a week and need to start a new row to begin a new week.
I have tried enclosing an if statement that detected the remainder of the output if divided by 8. if the output was 0 then new line, if not then carry on. However, this didnt work because the <tr> tags are outside the php statement.
Following answers and comments I have updated my code to:
<tr>
<?php
// output the number of days in the month
foreach($this->daysInMonthArray as $days1){
foreach($days1 as $key => $object){
if($object % 8 == 0){
echo "</tr><tr><td>" . $object . "</td>";
}else {
echo "<td>" . $object . "</td>";
}
}
} ?>
</tr>
This very nearly works, except for the middle two weeks in a month. It puts 8 days in the middle 2 weeks but 7 on the first and last week.
You’ve pretty much answered this one yourself with the following:
You have to get the
<tr>tags inside the loop.This is untested code and could be more concise but hopefully you get the idea.