简体   繁体   中英

creating <tr> tag inside a for loop

I am creating a calendar. So, I found some stuff online and now I am trying to make it work the way I need it. The problem is that the days are created in a horizontal way.

This is how it looks like:

在此输入图像描述

So, how you can see only <td> tag have been created.

I was trying to add <tr> tags for each 6 days so the calendar would look like a real calendar.

For example, day 7 should be under Monday, and day 8 on Tuesdays and so on.

I need help please. Thanks.

PS: if you think the question is not clear enough please let me know before downvote this question. I really would like to know what is going on.

Here is the code:

PHP:

<?php

 class Calendar {  

/**
 * Constructor
 */
public function __construct(){     
    $this->naviHref = htmlentities($_SERVER['PHP_SELF']);
}

/********************* PROPERTY ********************/  
private $dayLabels = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");

private $currentYear=0;

private $currentMonth=0;

private $currentDay=0;

private $currentDate=null;

private $daysInMonth=0;

private $naviHref= null;

/********************* PUBLIC **********************/  

/**
* print out the calendar
*/
public function show() {
    $year  = null;

    $month = null;

    if(null==$year&&isset($_GET['year'])){

        $year = $_GET['year'];

    }else if(null==$year){

        $year = date("Y",time());  

    }          

    if(null==$month&&isset($_GET['month'])){

        $month = $_GET['month'];

    }else if(null==$month){

        $month = date("m",time());

    }                  

    $this->currentYear=$year;

    $this->currentMonth=$month;

    $this->daysInMonth=$this->_daysInMonth($month,$year);  

    $content='<fieldset>'. 
             '<legend class="cyan bold">Tutors Schedule</legend>'.

                    //head nav '<' & '>' buttons & month 
                    $this->_createNavi().
                    //end head


                    '<table class="table table-bordered">'.

                        '<thead>'.
                             '<tr>'.$this->_createLabels().'</tr>' .
                        '</thead>' .

                        '<tbody>'.
                          '<tr>';


                            $weeksInMonth = $this->_weeksInMonth($month,$year);
                            // Create weeks in a month

                            for( $i=0; $i<$weeksInMonth; $i++ ){
                                 //create <tr> tags 

                                    //Create days in a week
                                    for($j=1;$j<=7;$j++){

                                       $content.=$this->_showDay($i*7+$j);

                                    }  
                                    //Close </tr>
                            }



                         '</tbody>'.
                    '</table>';

   '</fieldset>';
    return $content;   
}

/********************* PRIVATE **********************/ 
/**
* create the li element for ul
*/
private function _showDay($cellNumber){

    if($this->currentDay==0){

        $firstDayOfTheWeek = date('N',strtotime($this->currentYear.'-'.$this->currentMonth.'-01'));

        if(intval($cellNumber) == intval($firstDayOfTheWeek)){

            $this->currentDay=1;

        }
    }

    if( ($this->currentDay!=0)&&($this->currentDay<=$this->daysInMonth) ){

        $this->currentDate = date('Y-m-d',strtotime($this->currentYear.'-'.$this->currentMonth.'-'.($this->currentDay)));

        $cellContent = $this->currentDay;

        $this->currentDay++;   

    }else{

        $this->currentDate =null;

        $cellContent=null;
    }


    return '<td id="td-'.$this->currentDate.'" class=" td-top-text '.($cellNumber%7==1?' start ':($cellNumber%7==0?' end ':' ')).
            ($cellContent==null?'mask':'').'"><div class="inside">'.$cellContent.'</div></td>';
}

}

CSS:

 /*Event Calendar Month */

.td-top-text {
 vertical-align: top;
 text-align: right;
 }

.inside-date, .td-top-text {
    text-align: right;
}
.inside-event {
    text-align: left;

}



td.td-top-text {
    width:14.2857142857%; /* 100% divided by 7 */
    position:relative;
    height: 100%;

}

td.td-top-text:before {
    content:'';
    display:block;
    margin-top:100%;
    position: absolute;

}

td.td-top-text .inside {
    font-size: 15px;
    position: relative;
    top:2px;
    bottom:2px;
    left:2px;
    right:2px;
    overflow-x: hidden;

}

I am not sure I completely understand the question, but do you mean something like this?

                  for( $i=0; $i<$weeksInMonth; $i++ ){
                             $content .= "<tr>";

                                //Create days in a week
                                for($j=1;$j<=7;$j++){

                                   $content.=$this->_showDay($i*7+$j);

                                }  
                                $content .= "</tr>";
                        }

CSS example:

php code:

    $i = 1;
$content='<fieldset><legend class="cyan bold">Tutors Schedule</legend>';



foreach($dayName as $dd) {
    $content .= '<div>'.$dd.'</div>';
}
while($i <= $this->daysInMonth) {
    $content .= '<div>'.$i.'</div>';
}   

$content .= '</fieldset>';

 div:nth-child(7n+1) { clear: left; } .wrapper > div { display: inline-block; float: left; width: 40px } 
 <div class="wrapper"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> <div>7</div> <div>8</div> <div>9</div> <div>10</div> <div>11</div> <div>12</div> <div>13</div> <div>14</div> <div>15</div> <div>16</div> <div>17</div> <div>18</div> <div>19</div> <div>20</div> <div>21</div> <div>22</div> <div>23</div> <div>24</div> <div>25</div> <div>26</div> <div>27</div> <div>28</div> <div>29</div> <div>30</div> <div>31</div> </div> 

Alot is possible this way: easy start on other days than monday '$i = -2; (example)';

This is just a basic little example don't copy and paste ;-)

创建一个$count变量,一旦达到7,就会关闭该行并打开一个新行,并将$count重置为0.在循环内将$count递增,并使if语句检查是否已达到7在那个循环中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM