Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7876621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:11:42+00:00 2026-06-03T03:11:42+00:00

I am building a calender in PHP. In the controller, I have detected the

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:11:43+00:00Added an answer on June 3, 2026 at 3:11 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building a kind of calender web app I have set up the
building a site using PHP and MySQL that needs to store a lot of
I'm building my first application that requires a calendar. I have it working great
I have a calendar that I generate. Currently it makes the entire month and
I am building a Calendar app. I have Navigation based app template. Navigation controller
I'm building a webapp that leverages gdata-api to bring a user's calendars into the
I'm building calendar events in php. I have stored all the events in database.
I am building a Calendar object. It is important that I have both a
I'm building a PHP calendar app for a photography group. They need to be
Im building a calendar and to find out the first day of the month

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.