RSM
RSM

Reputation: 15118

Every 8th instance go to new tr

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.

Upvotes: 1

Views: 110

Answers (2)

ltiong_dbl
ltiong_dbl

Reputation: 3216

One prob that you're gonna run into is that you're nesting your table in an existing table. Try:

<tr><td>
    <?php
      // output the number of days in the month
      foreach($this->daysInMonthArray as $days1){
        echo "<table>";

        $dayofweek = 0;
        foreach($days1 as $key => $object){
          if($dayofweek%7 == 0)
            echo "<tr>";

          echo "<td>" . $object . "</td>";

          if($dayofweek%7 == 0)
            echo "</tr>";

          $dayofweek++;               
        }

        if($dayofweek%7 != 0) //last tr
          echo "</tr>";

        echo "</table>";
      }
    ?>
</td></tr>

Upvotes: 0

Matt
Matt

Reputation: 9433

You've pretty much answered this one yourself with the following:

this didnt work because the tags are outside the php statement

You have to get the <tr> tags inside the loop.

<?php
    $daysInRow = 0;
    // output the number of days in the month
    foreach($this->daysInMonthArray as $days1)
    {
        foreach($days1 as $key => $object)
        {
            if($daysInRow % 7 === 0)
            {
                echo '<tr>';
            }

            echo "<td>"  . $object . "</td>"; 

            if($daysInRow % 7 === 0)
            {
                echo '</tr>';
            }

            if($daysInRow % 7 === 0)
            {
                $daysInRow = 0;
            }
            else
            {
                $daysInRow++;
            }
        }
    }
?>

This is untested code and could be more concise but hopefully you get the idea.

Upvotes: 1

Related Questions