0

我正在从 MySQL 数据库中提取大学课程和详细信息,并使用 PHP 将它们分类到网页上。

目前我的脚本很简单。它提取几个字段并按类标题组织它们:

班级名称:编程101

  • 学分:4
  • 时间:上午 11:00 - 下午 12:50
  • 天数:MTW
  • 房间号:361

有时我们有 3 或 4 个相同的课程在进行,所以这会创建一个相当长的页面。我正在尝试简化页面的外观。


因此,不要重复类名:

班级名称:编程101

  • 学分:4
  • 时间:上午 11:00 - 下午 12:50
  • 天数:MTW
  • 房间号:361

班级名称:编程101

  • 学分:4
  • 时间:上午 11:30 - 下午 2:50
  • 天数:F
  • 房间号:123

你可以看到这个:

课程名称:编程 101 学分:4

  • 时间:上午 11:00 - 下午 12:50
  • 天数:MTW
  • 房间号:361

  • 时间:上午 11:30 - 下午 2:30
  • 天数:F
  • 房间号:123

课程名称:编程 102 学分:4

  • 时间:上午 1:00 - 下午 2:30
  • 天数:兆瓦
  • 房间号:231

  • 时间:凌晨 2:30 - 下午 4:30
  • 天数:TF
  • 房间号:222

这是我当前的脚本:

$sql = "SELECT crs_title, trm_cde, last_name, first_name, begin_dte, end_dte, crs_cde, begin_tim, end_tim, monday_cde, tuesday_cde, wednesday_cde, thursday_cde, friday_cde, saturday_cde, sunday_cde, bldg_cde, room_cde, udef_5_2_1, crs_capacity, crs_enrollment, section_sts FROM BTC_Web_Schedule_view WHERE yr_cde = 2014 AND trm_cde = 'fa' ORDER BY crs_title, trm_cde";
                $rs = odbc_exec($conn,$sql);

                while (odbc_fetch_row($rs))
                {
                    $crs_title = odbc_result($rs,"crs_title");
                    $trm_cde = odbc_result($rs,"trm_cde");
                    $crs_cde = odbc_result($rs,"crs_cde");
                    $begin_tim = odbc_result($rs,"begin_tim");
                    $end_tim = odbc_result($rs,"end_tim");
                    $begin_time = substr($begin_tim, 11, -7);
                    $end_time = substr($end_tim, 11, -7);
                    $begin_dte = odbc_result($rs,"begin_dte");
                    $end_dte = odbc_result($rs,"end_dte");
                    $monday_cde = odbc_result($rs,"monday_cde");
                    $tuesday_cde = odbc_result($rs,"tuesday_cde");
                    $wednesday_cde = odbc_result($rs,"wednesday_cde");
                    $thursday_cde = odbc_result($rs,"thursday_cde");
                    $friday_cde = odbc_result($rs,"friday_cde");
                    $saturday_cde = odbc_result($rs,"saturday_cde");
                    $sunday_cde = odbc_result($rs,"sunday_cde");
                    $first_name = odbc_result($rs,"first_name");
                    $last_name = odbc_result($rs,"last_name");
                    $fullname = $first_name.$last_name;
                    $bldg_cde = odbc_result($rs,"bldg_cde");
                    $room_cde = odbc_result($rs,"room_cde");
                    $udef_5_2_1 = odbc_result($rs,"udef_5_2_1");
                    $crs_capacity = odbc_result($rs,"crs_capacity");
                    $crs_enrollment = odbc_result($rs,"crs_enrollment");
                    $seats_left = $crs_capacity - $crs_enrollment;
                    $section_sts = odbc_result($rs,"section_sts");

                    echo "<div class='container'><p><h2>$crs_title</h2> | $crs_cde</p></div>";
                    echo "<div class='panel'><p><strong>Time</strong>: ".date('g:ia', strtotime($begin_time))." - ".date('g:ia', strtotime($end_time))."<br>";
                    echo "<strong>Start date</strong>: ".date("F jS, Y",strtotime($begin_dte))."<br>";
                    echo "<strong>End date</strong>: ".date("F jS, Y",strtotime($end_dte))."<br>";
                    echo "<strong>Days</strong>: $monday_cde $tuesday_cde $wednesday_cde $thursday_cde $friday_cde $saturday_cde $sunday_cde <br>";
                    echo "<strong>Instructor</strong>: $fullname <br>";
                    echo "<strong>Building</strong>: $bldg_cde <br>";
                    echo "<strong>Room</strong>: $room_cde <br>";
                    echo "<strong>Fee</strong>: $$udef_5_2_1 <br>";
                    echo "<strong>Seats remaining</strong>: $seats_left <br>";
                    echo "<strong>Enrollment status</strong>: $section_sts</p></div>";
                }
                odbc_close($conn);

我很感激任何建议。谢谢你。

4

2 回答 2

0

有几点需要考虑:

在检索数据之前,更改您的查询以尽可能多地进行排序。例如,您可以ORDER BY首先课程标题(然后其次是其他字段,如开始日期)。

从数据构建一个数组,以便您可以轻松地以您想要的方式输出:例如:

$array = array();
while (odbc_fetch_row($rs)) {
     //  ... get your data (probably use something besides odbc_result so you can get ther whole array at once .. but for this example just assume I am using your same variables.
     $array[$crs_title] = array(
        // place your other course data here in array
     );
}
foreach($array as $course_title => $course_data) {
    // present your data
}
于 2013-09-30T19:05:32.373 回答
0

规范化你的数据库?

除此之外:

while (odbc_fetch_row($rs))
{
    $crs_title = odbc_result($rs,"crs_title");
    $first_name => odbc_result($rs,"first_name");
    $last_name => odbc_result($rs,"last_name");
    $fullname = $first_name.$last_name;
    $begin_tim = odbc_result($rs,"begin_tim");
    $end_tim = odbc_result($rs,"end_tim");
    $crs_capacity = odbc_result($rs,"crs_capacity");
    $crs_enrollment = odbc_result($rs,"crs_enrollment");
    $courses[$crs_title][] = array(
        $trm_cde = odbc_result($rs,"trm_cde")
        'crs_cde' => odbc_result($rs,"crs_cde"),
        'begin_tim' => $begin_tim,
        'begin_time' => substr($begin_tim, 11, -7),
        'end_tim' => $end_tim,
        'end_time' = substr($end_tim, 11, -7),
        'begin_dte' => odbc_result($rs,"begin_dte"),
        'end_dte' => odbc_result($rs,"end_dte"),
        'monday_cde' => odbc_result($rs,"monday_cde"),
        'tuesday_cde' => odbc_result($rs,"tuesday_cde"),
        'wednesday_cde' => odbc_result($rs,"wednesday_cde"),
        'thursday_cde' => odbc_result($rs,"thursday_cde"),
        'friday_cde' => odbc_result($rs,"friday_cde"),
        'saturday_cde' => odbc_result($rs,"saturday_cde"),
        'sunday_cde' => odbc_result($rs,"sunday_cde"),
        'first_name' => $first_name,
        'last_name' => $last_name,
        'fullname' => $first_name.$last_name,
        'bldg_cde' => odbc_result($rs,"bldg_cde"),
        'room_cde' => odbc_result($rs,"room_cde"),
        'udef_5_2_1' => odbc_result($rs,"udef_5_2_1"),
        'crs_capacity' => $crs_capacity,
        'crs_enrollment' => $crs_enrollment,
        'seats_left' = $crs_capacity - $crs_enrollment,
        'section_sts' => odbc_result($rs,"section_sts"),
    );
    foreach ($courses as $crs_title => $data) {
        /* html output */
        $count = count($data);
        foreach ($data as $subdata) {
            //multiple times
        }
    }
}
于 2013-09-30T19:03:08.073 回答