2

我正在尝试在表格中绘制学生班级,其中表格单元格可以包含班级,也可以为空。表格行以一天中的时间(以分钟为单位)为标题,列表示一周中的哪一天(星期日 = 1、星期三 = 4、星期六 = 7 等)。

当三个类同时出现时,我的算法似乎存在问题,因为打印了太多的表格单元格。我认为问题与$printed成为一个boolean(它可能应该是从 7 开始的倒数)有关,但我不确定。

有人可以阐明我哪里出错了吗?这是我的代码: http: //phpfiddle.org/main/code/q6z-s93。点击F9示例输出。

<?php
    /**
     * Created by JetBrains PhpStorm.
     * User: Marco
     * Date: 8/22/13
     * Time: 9:49 AM
     */

    $name   = "Table Name";

    $blocks = array(
        array(
            "id"        => 23,
            "name"      => "test",
            "day"       => 2,
            "startTime" => (9 * 60) + 30,
            "endTime"   => 720
        ),
        array(
            "id"        => 12,
            "name"      => "test 2",
            "day"       => 3,
            "startTime" => (10 * 60) + 30,
            "endTime"   => 720
        ),
        array(
            "id"        => 2,
            "name"      => "test 2",
            "day"       => 4,
            "startTime" => (9 * 60) + 30,
            "endTime"   => (10 * 60) + 30
        )

    );

    $tableContent = "";

    $tableContent.= "
                <h1 id='1' class='table-name'>$name</h1>
                <table class='table table-bordered' data-id='1' border='1'>
                    <thead>
                    <tr>
                        <th>&nbsp;</th>
                        <th>Sunday</th>
                        <th>Monday</th>
                        <th>Tuesday</th>
                        <th>Wednesday</th>
                        <th>Thursday</th>
                        <th>Friday</th>
                        <th>Saturday</th>
                    </tr>
                    </thead>
                    <tbody>
            ";

    $time = mktime(0, 0, 0, 1, 1);

    $earliest   = 720;
    $latest     = 1140;


    for( $i = 0; $i < 1440; $i += 30 ) {
        // print table rows

        $rowContent = ""; // Holds table cells and content
        $styles     = ""; // holds `class="foo"` (row class)



        for ($j = 1; $j < 8; $j++) {
            // print row columns

            $printed = FALSE;

            if ( $i + 30 < $earliest ) {
                $rowContent .= "<td> &nbsp; </td>";
                $styles     = "class='hiddenTopRow'";
                $printed    = TRUE;
            } else if ( $i > $latest ) {
                $rowContent .= "<td> &nbsp; </td>";
                $styles     = "class='hiddenBottomRow'";
                $printed    = TRUE;
            }


            foreach ( $blocks as $block ) {
                // cycle through Courses and check if there is one scheduled at this time
                if ( ( $block["day"] == $j ) && ( $block["startTime"] == $i ) ) {
                    // class starts on this day at this time

                    $rowspan    = ( ( $block["endTime"] - $block["startTime"] ) / 30 );
                    $content    = $block["name"];
                    $blockID    = $block["id"];
                    $rowContent .= "\t" . "<td rowspan='$rowspan'
                                                        data-id='$blockID'
                                                        class='block-cell'>$content</td>" . "\r\n";

                    $printed = TRUE;

                } else if ( ( $block["day"] == $j ) &&  // Class starts this day
                    ( $block["startTime"] < $i ) &&     // after this time
                    ( $block["endTime"] >= $i + 30) ) { // but isn't finished
                    // class is continuing
                    $printed = TRUE;
                } else {
                    // no class at this time
                }
            }

            if (!$printed) $rowContent .= "<td> &nbsp; </td>";

        }

        /* Print content */

        $tableContent .= "<tr $styles>" . "\r\n";

        $heading = sprintf("\t" . '<th class="time">%1$s</th>' . "\r\n",
            date( 'g:i a', $time + ( $i * 60 ) ) );

        $tableContent .= $heading . $rowContent;

        $tableContent .= "\t" . "</tr>" . "\r\n";

    }
    $tableContent .= '
                    </tbody>
                </table>
            ';

    ?>
    <!DOCTYPE html>
    <html lang="en">
    <body>
    <?php echo $tableContent ?>
    </body>
    </html>
4

1 回答 1

0

要查看发生了什么,只需在第 84 行打印索引 $j:

    if ( $i + 30 < $earliest ) {
        $rowContent .= "<td> $j </td>";
        $styles     = "class='hiddenTopRow'";
        $printed    = TRUE;
    }

问题是这样的:当您为单元格/块设置行跨度时,在以下行上,您不知道应该跳过哪个单元格/块。

您可以使用数组来跟踪此信息(http://phpfiddle.org/main/code/03w-0v6)。例如,在下面的代码中,我添加了变量 $rowJump。如果我设置 $rowJump[2] = 2,这意味着我必须在接下来的两行中跳过单元格 2。

$rowJump = Array();

for( $i = 0; $i < 1440; $i += 30 ) {
    // print table rows

    $rowContent = ""; // Holds table cells and content
    $styles     = ""; // holds `class="foo"` (row class)



    for ($j = 1; $j < 8; $j++) {
        // print row columns

        if (isset($rowJump[$j]) && $rowJump[$j]>0)  {
        $rowJump[$j]--;
        continue;
    }

    $printed = FALSE;

    if ( $i + 30 < $earliest ) {
        $rowContent .= "<td> $j </td>";
        $styles     = "class='hiddenTopRow'";
        $printed    = TRUE;
    } else if ( $i > $latest ) {
        $rowContent .= "<td> &nbsp; </td>";
        $styles     = "class='hiddenBottomRow'";
        $printed    = TRUE;
    }


    foreach ( $blocks as $block ) {
        // cycle through Courses and check if there is one scheduled at this time
        if ( ( $block["day"] == $j ) && ( $block["startTime"] == $i ) ) {
            // class starts on this day at this time

            $rowspan    = ( ( $block["endTime"] - $block["startTime"] ) / 30 );
            $content    = $block["name"];
            $blockID    = $block["id"];
            $rowContent .= "\t" . "<td rowspan='$rowspan'
                                                data-id='$blockID'
                                                class='block-cell'>$content !!</td>" . "\r\n";

            $printed = TRUE;

            $rowJump[$j] = $rowspan-1;
            $j++;               


        } else 
        [...]
于 2013-08-22T17:36:15.283 回答