foreach()循环输出数据和创建布局

时间:2017-11-11 01:58:06

标签: php loops foreach

我目前正在尝试使用PHP从我的Sql Server数据库中创建一个类计划,我正在尝试获取布局输出以及数据,因为我正在对资源进行分组。

这些分组是嵌套的,例如:

-DAY
--TIME
---CLASS
----STUDENTS

应输出如下: enter image description here

但是,我得到了这个: enter image description here

我当前的输出正在工作,但是,它只是在第一个循环,然后一切都变得混乱。我假设我的代码中某处有一个错误的</div>标签,但我无法找到它。

我的php代码是delcare这样的函数:

<div class="mainScheduleWrapper">
    <?php daySchedule(); ?>
</div>

我的PHP代码是这样的:

function daySchedule() {
    global $conn;
    $dayScheduleQuery = 'SET DATEFIRST 1
                SELECT [DAY].[DAY] AS [DAY], CLASS.CLASSTIME AS CLASSTIME, CLASSLEVEL.CLASSLEVEL AS CLASSLEVEL, CLASS.MAXSTUDENT AS MAXSTUDENT, INSTRUCTOR.FIRSTNAME AS INSTRUCTOR, STUDENT.FIRSTNAME AS STUDENTFIRST, STUDENT.SURNAME AS STUDENTLAST, STUDENT.DOB AS STUDENTDOB
                FROM STUDENT JOIN BOOKING ON STUDENT.ID = BOOKING.STUDENTID JOIN CLASS ON CLASS.ID = BOOKING.CLASSID JOIN CLASSLEVEL ON CLASS.CLASSLEVELID = CLASSLEVEL.ID JOIN [DAY] ON CLASS.CLASSDAY = [DAY].ID JOIN INSTRUCTOR ON CLASS.INSTRUCTORID = INSTRUCTOR.ID
                WHERE   [DAY].ID = (DATEPART(dw, GETUTCDATE() AT TIME ZONE \'AUS Eastern Standard Time\'))
                ORDER BY CLASS.CLASSTIME ASC, INSTRUCTOR.FIRSTNAME ASC, CLASSLEVEL.CLASSLEVEL ASC';

    // COUNTERS
    $t = 0;
    $i = 0;

    //VARIABLES FOR DAY SCHEDULE
    $classDay = NULL;
    $classTime = NULL;
    $classInstructor = NULL;
    $closeClass = false;
    $closeAll = false;
    $queryConnector = $conn->query($dayScheduleQuery);

    foreach ($queryConnector as $schedule) {

        // CLASS DAY HEADER
        if ($classDay != $schedule['DAY']) {

            echo '<div class="grid-1">';
            echo '<h1>' . $schedule['DAY'] . '</h1>';
            echo '</div><!-- Day closed! -->';
            $classDay = $schedule['DAY'];

        }

        // CLASS TIME HEADER
        if ($classTime != $schedule['CLASSTIME']) {

            if($classTime != $schedule['CLASSTIME'] && $t > 0) {
                $closeAll = true;
                goto closeAll;
            }

            echo '<div class="grid-12-noGutter scheduleContainer">'; //NON-CLOSED
            echo '<h1>' . 'T = ' .  $t . '</h1>';
            echo '<div class="grid-middle-center col scheduleTimeTab">';
                // FIX 3 DIGIT MILITARY TIME
                if (strlen($schedule['CLASSTIME']) < 4) {
                    $classScheduleTime = '0' . $schedule['CLASSTIME'];
                } else {
                    $classScheduleTime = $schedule['CLASSTIME'];
                }
                echo '<p>' . date('g:i A', strtotime($classScheduleTime)) . '</p>';
            echo '</div>'; //CLOSE TIME TAB

            echo '<div class="innerSchedule">'; // NON-CLOSED

            $classTime = $schedule['CLASSTIME'];
            $t += 100;

        }
        // INSTRUCTOR HEADER
        if ($classInstructor != $schedule['INSTRUCTOR']) {

            if ($classInstructor != $schedule['INSTRUCTOR'] && $i > 0) {
                $closeClass = true;
                goto closeClassWrapper;
            }

            echo '<div class="classWrapper">';
            echo '<h1>' . 'I =' . $i . 'T = ' .  $t . '</h1>';
                echo '<div class="grid-3-middle classHeader">';
                    echo '<div class="col classHeaderCell' . classLevelColour($schedule['CLASSLEVEL']) . '">' . $schedule['CLASSLEVEL'] . '</div>';
                    echo '<div class="col classHeaderCell">' . $schedule['INSTRUCTOR'] . '</div>';
                    echo '<div class="col classHeaderCell">Max' . ' ' . $schedule['MAXSTUDENT'] . '</div>';
                echo '</div>';
                echo '<div class="grid-4-middle" id="studentHeaders">';
                    echo '<div class="col"><h6>Student Name</h6></div>';
                    echo '<div class="col"><h6>Student Birthday</h6></div>';
                    echo '<div class="col"><h6>Class Level</h6></div>';
                    echo '<div class="col"><h6>Attendance</h6></div>';
                echo '</div>';

            $classInstructor = $schedule['INSTRUCTOR'];
            $i += 100;

        }
        echo '<div class="grid-4 studentRow">';
            echo '<div class="col">';
                echo '<span class="studentCell">' . $schedule['STUDENTFIRST'] . ' ' . $schedule['STUDENTLAST'] . '</span>';
            echo '</div>';
            echo '<div class="col">';
                echo '<span class="studentCell">' . $schedule['STUDENTDOB'] . '</span>';
            echo '</div>';
            echo '<div class="col">';
                echo '<span class="studentCell">' . $schedule['CLASSLEVEL'] . '</span>';
            echo '</div>';
            echo '<div class="col">';
                echo '<span class="studentCell">--</span>';
            echo '</div>';
        echo '</div>';

        // GOTO TAGS
        closeClassWrapper: {
            if ($closeClass === true) {
                echo '</div>';
                $closeClass = false;
                $i = 0;
            }
        }

        closeAll: {
            if ($closeAll === true) {
                echo '</div>';
                echo '</div>';
                echo '</div>';
                $closeAll = false;
                $t = 0;
                $i = 0;
            }
        }
    }
}

任何帮助都会非常感激 - 即使是告诉我,我会以完全错误的方式解决这个问题。

最基本的问候 迈克尔Z

1 个答案:

答案 0 :(得分:0)

我不会说你完全错误的方式,但是你的代码中有一些红旗跳出来。

  1. 使用goto跳跃是不好的做法。它会阻止你的程序流程,并迫使你分离不应该分开的任务。你标记了代码的部分&#34; // NON CLOSED&#34;当有</div>失踪时,有什么目的吗?您如何知道goto部分是否可靠?

  2. echo <div class="col">之类的内容,而无需转义双引号(如 \&#34; ,每个&#34} ; 字符),可能会有问题。您的代码可能会在PHP端或HTML端被破坏或误解。

  3. 像其他人所说的那样,PHP的使用在这里可能有些过分。除了发送JSON之外,其余的都可以用JavaScript处理。