簡體   English   中英

創建 <tr> 在for循環中標記

[英]creating <tr> tag inside a for loop

我正在創建一個日歷。 所以,我在網上發現了一些東西,現在我試圖讓它按照我需要的方式工作。 問題是天數是以橫向方式創建的。

這是它的樣子:

在此輸入圖像描述

那么,你怎么看到只有<td>標簽被創建了。

我試圖每6天添加<tr>標簽,因此日歷看起來像真正的日歷。

例如,第7天應該在星期一,第8天應該在星期二,依此類推。

我需要幫助。 謝謝。

PS:如果您認為問題不夠明確,請在關注此問題之前告訴我。 我真的很想知道發生了什么。

這是代碼:

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;

}

我不確定我完全理解這個問題,但你的意思是這樣嗎?

                  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示例:

php代碼:

    $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> 

很可能這樣:除了星期一'$ i = -2之外的其他日子很容易開始; (例)';

這只是一個基本的小例子,不要復制和粘貼;-)

創建一個$count變量,一旦達到7,就會關閉該行並打開一個新行,並將$count重置為0.在循環內將$count遞增,並使if語句檢查是否已達到7在那個循環中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM