dongying195959 2018-08-18 06:25
浏览 6

PHP从api那天开始排序课程

I'm using my school's api for the cs departments course schedule. It provides me a json file that I'm outputting on my webpage separated by each semester. Currently I'm able to have the output of all the courses in each semester sorted by the course ID. However, I'm trying to sort each semester by the day that the class is offered. Monday and Monday/Wednesday classes would be listed first, then Tuesday and Tuesday/Thursday and so on.

In my printListings function I'm able to print the entire course schedule out. And I'm able to add the line:

// if first character of days is an "M" 
if(strcmp(substr($course->meetingTimes[0]->days, 0, 1), 'M')) continue;

which causes only monday and monday/wednesday courses to appear. However if I try adding a copy of the same line and just change the "M" to a "T" it causes my entire webpage to appear blank. Why is this?

printListing function:

function printListing($apiCall) {

    $json = curl_get_contents($apiCall);
    $obj = json_decode($json);

    if (!($obj->courses == null)) {

        echo "<table border='3' width='100%'>";

        foreach ($obj->courses as $course) {

            // if first character of days is an "M" 
            if(strcmp(substr($course->meetingTimes[0]->days, 0, 1), 'M')) continue;

            //if(strcmp(substr($course->meetingTimes[0]->days, 0, 1), 'T')) continue;
            //if(strcmp(substr($course->meetingTimes[0]->days, 0, 1), 'W')) continue;
            //if(strcmp(substr($course->meetingTimes[0]->days, 0, 1), 'R')) continue;

            $building = strtoupper(trim($_GET['building']));
            $buildingMatch = false;
            $thisBuilding0 = trim($course->meetingTimes[0]->building);
            $thisBuilding1 = trim($course->meetingTimes[1]->building);
            if ($building && ($thisBuilding0 == $building || $thisBuilding1 == $building))
                $buildingMatch = true;
            if (!($building))
                $buildingMatch = true;
            if (!$buildingMatch)
                continue;

            $room = strtoupper(trim($_GET['room']));
            $roomMatch = false;
            $thisroom0 = trim($course->meetingTimes[0]->room);
            $thisroom1 = trim($course->meetingTimes[1]->room);
            if ($room && ($thisroom0 == $room || $thisroom1 == $room))
                $roomMatch = true;
            if (!($room))
                $roomMatch = true;
            if (!$roomMatch)
                continue;

            // different <tr bgcolor=...> for each professor
            switch ($course->meetingTimes[0]->instructor) {
                case "proff1":            // 1
                    $printline = "<tr bgcolor='#B19CD9'>";  // pastel purple
                    break;
                case "proff2":             // 2
                    $printline = "<tr bgcolor='lightblue'>";  // light blue
                    break;
                case "proff3":           // 3 
                    $printline = "<tr bgcolor='pink'>";  // pink
                    break;
                case "proff4":           // 4
                    $printline = "<tr bgcolor='yellow'>";   // yellow
                    break;
                case "proff5":           // 5
                    $printline = "<tr bgcolor='#77DD77'>";  // pastel green (light green)
                    break;
                case "proff6":           // 6
                    $printline = "<tr bgcolor='#FFB347'>";  // pastel orange
                    break;
                default:
                    $printline = "<tr>"; // no background color
            }

            $printline .= "<td width='13%'>" . $course->prefix . " " . $course->courseNumber . "*" . $course->section . "</td>";
            $printline .= "<td width='40%'>" . $course->title . " (" . $course->lineNumber . ")" . "</td>";
            $printline .= "<td width='12%'>Av:" . $course->seatsAvailable . " (" . $course->capacity . ")" . "</td>";

            // print day and time column
            if ($course->meetingTimes[0]->days) {
                $printline .= "<td width='15%'>" . $course->meetingTimes[0]->days . " " . $course->meetingTimes[0]->startTime;
                // $printline .= . "<br /> " . $course->meetingTimes[1]->days . " " . $course->meetingTimes[1]->startTime ;
                $printline .= "</td>";
            } else {
                $printline .= "<td width='15%'>";
                $printline .= $course->meetingTimes[1]->days . " " . $course->meetingTimes[1]->startTime . "</td> ";
            }

            // print building and room column
            $printline .= "<td width='10%'>";
            if (substr($course->section, -2, 1) == "9")
                $printline .= "(Online)";
            else
            if (substr($course->section, -2, 1) == "7")
                $printline .= $course->meetingTimes[1]->building . " " . $course->meetingTimes[1]->room;
            else
                $printline .= $course->meetingTimes[0]->building . " " . $course->meetingTimes[0]->room;
            $printline .= "</td>";

            // print instructor column
            $printline .= "<td width='10%'>" . $course->meetingTimes[0]->instructor . "</td>";
            $printline .= "</tr>";
            echo $printline;
        } // end foreach

        echo "</table>";
        echo "<br/>";
    } // end if (!($obj->courses == null))
    else {
        echo "No courses fit search criteria";
        echo "<br />";
    }
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)
    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号