根据第一行输出在表中创建多个列

时间:2018-10-06 13:12:57

标签: php

我只是建立了一个简单的PHP脚本,该脚本根据今天的日期回显10周,向前回响5周,向后回响5周。

第一个<TR>就像一个超级按钮,它回显所有<TD>并带有相应的星期编号。问题在于,我希望它在多行中回显星期几,然后查询该星期已报告的小时数。我如何才能从第一个<TD>复制<TR>的数量,并在每行中保留$ from的值,以便以后可以计算该周的小时数?

当前输出:

-------------------------------------------------------------------------
/  *******  / W10 / W11 / W12 / W13 / W14 / W15 / W16 / W17 / W18 / W19 /                    
-------------------------------------------------------------------------
/ PROJECT 1 / 
-------------------------------------------------------------------------

我想要的输出:

[-5 WEEKS] / [+5 WEEKS]
-------------------------------------------------------------------------
/  *******  / W10 / W11 / W12 / W13 / W14 / W15 / W16 / W17 / W18 / W19 /                    
-------------------------------------------------------------------------
/ PROJECT 1 /  1h /  4h /  1h /  4h /  3h /  8h /  3h /  4h /  1h /  8h /
-------------------------------------------------------------------------
/ PROJECT 2 /  4h /  2h /  2h /  1h /  8h /  3h /  8h /  3h /  4h /  7h /
-------------------------------------------------------------------------
/ PROJECT 3 /  1h /  3h /  4h /  1h /  5h /  2h /  7h /  1h /  7h /  5h /
-------------------------------------------------------------------------

我的代码:

<?php
//Get current date, and go 5w forward
$now = new DateTime;
$now->modify("+5 week");

//Get current date, and go 5w backward
$from = new DateTime;
$from->modify("-5 week");
$from->modify("thursday this week"); // see ISO 8601 

echo '<table class="view-calendar" cellspacing="0" cellpadding="0" border="0">';
    echo '<tbody>';
        //The first row with week numbers
        echo '<tr>';
            echo '<td style="background: #f2f2f2">';
                echo '';
            echo '</td>';
            while($from < $now) {
                echo '<td valign="top">';
                    echo 'v'.$from->format('W');
                echo '</td>';
            $from->modify("+1 week");
            }
        echo '</tr>';
        //The rest of the rows with project hours in each column
        echo '<tr>';
            echo '<td>';
                echo 'Project 1';
            echo '</td>';
            // ECHO THE REST OF <TD> based on week from first row
        echo '</tr>';
    echo '</tbody>';
echo '</table>';
?>

2 个答案:

答案 0 :(得分:3)

希望它会有所帮助

<?php
//Get current date, and go 5w forward
$now = new DateTime;
$now->modify("+5 week");

//Get current date, and go 5w backward
$from = new DateTime;
$from->modify("-5 week");
$from->modify("thursday this week"); // see ISO 8601 

echo '<table class="view-calendar" cellspacing="0" cellpadding="0" border="1">';
    echo '<tbody>';
        //The first row with week numbers
        echo '<tr>';
            echo '<td style="background: #f2f2f2">';
                echo '';
            echo '</td>';
            while($from < $now) {
                echo '<td valign="top">';
                    echo 'v'.$from->format('W');
                echo '</td>';
            $from->modify("+1 week");
            }
        echo '</tr>';
        //The rest of the rows with project hours in each column
        $project=1;
        while($project<10) {
            echo '<tr>';
                echo '<td>';
                    echo 'Project '.$project;
                echo '</td>';
                $from->modify("-11 week");
                   while($from < $now) {
                    echo '<td valign="top">';
                        echo 'v'.$from->format('W');
                    echo '</td>';
                $from->modify("+1 week");
                }
                // ECHO THE REST OF <TD> based on week from first row
            echo '</tr>';
            $project++;
        }
    echo '</tbody>';
echo '</table>';
?>

答案 1 :(得分:1)

我不确定我是否了解每个单元格中需要的内容,但是像这样的内容将为您提供结构。

<snip> 
for ($i=1, $i<4, $i++) {  
echo '<tr>';
            echo '<td>';
                echo 'Project ' . $i;
            echo '</td>';
            from->modify("-10 week");
            while($from < $now) {
                echo '<td valign="top">';
                //This is where I don't understand what you want. Where is the hourly data coming from?
                echo 'v'.$from->format('W');
                echo '</td>';
                $from->modify("+1 week");
            }
    echo '</tr>';
    }
<snip>