-1

我有一张表,我需要在其中显示一个部门学生的成绩。我创建了测试矩阵编号并为它们分配了分数。假设 400Level 有 8 名学生,8 名学生中有 3 名没有结转,而其他 5 名学生有。该表应该显示 8 名学生注册的不同课程及其相应分数。如果未注册特定课程,则应显示默认值。

以下是实现此结果的示例脚本

<?php
require_once "scripts/connect_to_mysql.php";
$sq = mysql_query("SELECT DISTINCT course_code FROM tbl_result_400");
//$r = mysql_fetch_assoc($sq);
$k = 1;
echo "<table border = 1 bgcolor = 'white' class='member_tableheader'><tr>";
                        echo "<td width = '50px' align='left' valign='top'>COURSES<BR>UNITS</td>";
                        while ($r = mysql_fetch_assoc($sq)){
                                $v=$r['course_code'];
                                $sq1 = mysql_query("SELECT course_unit FROM tbl_result_400 WHERE course_code = '$v'");
                                $rr = mysql_fetch_assoc($sq1);//$v = "sup";
                                echo "<td width = '100px' align='center' valign='top'>".$r['course_code']."<BR>"."(".$rr['course_unit'].")"."<BR>"."</td>";
                                ++$k;
                        }
                        echo "</tr>";

                        echo "<tr>";
                        $sq2 = mysql_query("SELECT DISTINCT matric_no FROM tbl_registered_courses_400 ORDER BY matric_no");
                        $rrr = mysql_fetch_assoc($sq2);

                                do{
                                        $vv = $rrr['matric_no']; $t = 1;
                                        $sq4 = mysql_query("SELECT * FROM tbl_result_400 where matric_no = '$vv'");
                                        $n = mysql_num_rows($sq4);
                                $sq3 = mysql_query("SELECT score FROM tbl_result_400 where matric_no = '$vv'");

                                echo "<td width = '100px' align='center' valign='top'>".$vv."</td>";
                                while($rs = mysql_fetch_assoc($sq3)){
                                        if($t <= $n){
                                        if(empty($rs['score'])){
                                            $emptyScore = "&nbsp; --- &nbsp;";
                                        echo "<td width = '100px' align='center' valign='top'>".$emptyScore."</td>";    
                                        }else{
                                                echo "<td width = '100px' align='center' valign='top'>".$rs['score']."</td>";
                                                }
                                        };
                                        if($t == $n ){echo "<tr>";}
                                        /*if($t > $n){
                                                echo "<td width = '100px' align='center'
valign='top'>".$rs['score']."</td>";
                                        }*/
                                        ++$t;
                                }
                                }while($rrr = mysql_fetch_assoc($sq2));
                                echo "</tr>";
                        echo "</tr>";
echo "</table>";
?>

下图显示了我得到的结果: 在此处输入图像描述

想要的结果如下图所示: 在此处输入图像描述

从上面的图片可以看出,matric_no 03/55ec101 的学生没有注册 PHY 152 课程,因此没有 PHY 152 的默认分数。

据推测,矩阵编号为 06/55ec101 的学生除了 PHY 152 之外的所有课程都没有分数。我无法将我的代码修复为破折号的默认分数。

请帮我解决这个问题。

下面是不同矩阵数的示例 SQL:

    --
    -- Table structure for table `tbl_registered_courses_400`
    --

    CREATE TABLE IF NOT EXISTS `tbl_registered_courses_400` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `matric_no` varchar(15) NOT NULL,
      `course_code` varchar(15) NOT NULL,
      `course_unit` varchar(15) NOT NULL,
      `semester_registered` varchar(6) NOT NULL,
      `session_registered` varchar(15) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=19 ;

    --
    -- Dumping data for table `tbl_registered_courses_400`
    --

    INSERT INTO `tbl_registered_courses_400` (`id`, `matric_no`, `course_code`, `course_unit`, `semester_registered`, `session_registered`) VALUES
    (1, '03/55ec101', 'CSC 402', '6', '1', '2015/2016'),
    (2, '03/55ec101', 'CSC 499', '3', '1', '2015/2016'),
    (3, '03/55ec101', 'CHM 401', '3', '1', '2015/2016'),
    (4, '03/55ec101', 'CSC 470', '2', '1', '2015/2016'),
    (5, '03/55ec101', 'CSC 414', '3', '1', '2015/2016'),
    (6, '03/55ec101', 'CSC 411', '2', '1', '2015/2016'),
    (7, '03/55ec101', 'CSC 403', '3', '1', '2015/2016'),
    (8, '03/55ec101', 'BLY 401', '2', '1', '2015/2016'),
    (9, '03/55ec101', 'PHY 152', '3', '1', '2015/2016'),
    (10, '06/55ec101', 'CSC 402', '6', '1', '2015/2016'),
    (11, '06/55ec101', 'CSC 499', '3', '1', '2015/2016'),
    (12, '06/55ec101', 'CHM 401', '3', '1', '2015/2016'),
    (13, '06/55ec101', 'CSC 470', '2', '1', '2015/2016'),
    (14, '06/55ec101', 'CSC 414', '3', '1', '2015/2016'),
    (15, '06/55ec101', 'CSC 411', '2', '1', '2015/2016'),
    (16, '06/55ec101', 'CSC 403', '3', '1', '2015/2016'),
    (17, '06/55ec101', 'BLY 401', '2', '1', '2015/2016'),
    (18, '06/55ec101', 'PHY 152', '3', '1', '2015/2016');

这是结果表的 SQl:

    --
    -- Table structure for table `tbl_result_400`
    --

    CREATE TABLE IF NOT EXISTS `tbl_result_400` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `department_id` int(11) NOT NULL,
      `matric_no` varchar(15) NOT NULL,
      `session` varchar(15) NOT NULL,
      `semester` varchar(15) NOT NULL,
      `level` varchar(10) NOT NULL,
      `course_code` varchar(10) NOT NULL,
      `course_unit` varchar(10) NOT NULL,
      `ca` varchar(4) NOT NULL,
      `exam` varchar(4) NOT NULL,
      `score` varchar(10) NOT NULL,
      `grade` varchar(2) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=52 ;

    --
    -- Dumping data for table `tbl_result_400`
    --

    INSERT INTO `tbl_result_400` (`id`, `department_id`, `matric_no`, `session`, `semester`, `level`, `course_code`, `course_unit`, `ca`, `exam`, `score`, `grade`) VALUES
    (1, 2, '03/55EC101', '2015/2016', '1', '400', 'CSC 499', '3', '', '', '51', ''),
    (2, 2, '06/55EJ101', '2015/2016', '1', '400', 'CSC 499', '3', '', '', '69', ''),
    (3, 2, '06/55EJ102', '2015/2016', '1', '400', 'CSC 499', '3', '', '', '60', ''),
    (4, 2, '06/55EJ103', '2015/2016', '1', '400', 'CSC 499', '3', '', '', '54', ''),
    (5, 2, '06/55EJ104', '2015/2016', '1', '400', 'CSC 499', '3', '', '', '73', ''),
    (6, 2, '03/55EC101', '2015/2016', '1', '400', 'CSC 470', '3', '', '', '72', ''),
    (7, 2, '06/55EJ101', '2015/2016', '1', '400', 'CSC 470', '3', '', '', '68', ''),
    (8, 2, '06/55EJ102', '2015/2016', '1', '400', 'CSC 470', '3', '', '', '87', ''),
    (9, 2, '06/55EJ103', '2015/2016', '1', '400', 'CSC 470', '3', '', '', '53', ''),
    (10, 2, '06/55EJ104', '2015/2016', '1', '400', 'CSC 470', '3', '', '', '45', ''),
    (11, 2, '03/55EC101', '2015/2016', '1', '400', 'CSC 414', '2', '', '', '55', ''),
    (12, 2, '06/55EJ101', '2015/2016', '1', '400', 'CSC 414', '2', '', '', '65', ''),
    (13, 2, '06/55EJ102', '2015/2016', '1', '400', 'CSC 414', '2', '', '', '76', ''),
    (14, 2, '06/55EJ103', '2015/2016', '1', '400', 'CSC 414', '2', '', '', '59', ''),
    (15, 2, '06/55EJ104', '2015/2016', '1', '400', 'CSC 414', '2', '', '', '77', ''),
    (16, 2, '03/55EC101', '2015/2016', '1', '400', 'CSC 411', '2', '', '', '56', ''),
    (17, 2, '06/55EJ101', '2015/2016', '1', '400', 'CSC 411', '2', '', '', '65', ''),
    (18, 2, '06/55EJ102', '2015/2016', '1', '400', 'CSC 411', '2', '', '', '66', ''),
    (19, 2, '06/55EJ103', '2015/2016', '1', '400', 'CSC 411', '2', '', '', '76', ''),
    (20, 2, '06/55EJ104', '2015/2016', '1', '400', 'CSC 411', '2', '', '', '67', ''),
    (21, 2, '03/55EC101', '2015/2016', '1', '400', 'CSC 403', '3', '', '', '66', ''),
    (22, 2, '06/55EJ101', '2015/2016', '1', '400', 'CSC 403', '3', '', '', '65', ''),
    (23, 2, '06/55EJ102', '2015/2016', '1', '400', 'CSC 403', '3', '', '', '68', ''),
    (24, 2, '06/55EJ103', '2015/2016', '1', '400', 'CSC 403', '3', '', '', '79', ''),
    (25, 2, '06/55EJ104', '2015/2016', '1', '400', 'CSC 403', '3', '', '', '90', ''),
    (26, 2, '03/55EC101', '2015/2016', '1', '400', 'CSC 402', '4', '', '', '65', ''),
    (27, 2, '06/55EJ101', '2015/2016', '1', '400', 'CSC 402', '4', '', '', '72', ''),
    (28, 2, '06/55EJ102', '2015/2016', '1', '400', 'CSC 402', '4', '', '', '48', ''),
    (29, 2, '06/55EJ103', '2015/2016', '1', '400', 'CSC 402', '4', '', '', '80', ''),
    (30, 2, '06/55EJ104', '2015/2016', '1', '400', 'CSC 402', '4', '', '', '69', ''),
    (31, 2, '03/55EC101', '2015/2016', '2', '400', 'CSC 498', '6', '', '', '61', ''),
    (32, 2, '06/55EJ101', '2015/2016', '2', '400', 'CSC 498', '6', '', '', '55', ''),
    (33, 2, '06/55EJ102', '2015/2016', '2', '400', 'CSC 498', '6', '', '', '59', ''),
    (34, 2, '06/55EJ103', '2015/2016', '2', '400', 'CSC 498', '6', '', '', '52', ''),
    (35, 2, '06/55EJ104', '2015/2016', '2', '400', 'CSC 498', '6', '', '', '60', ''),
    (36, 2, '03/55EC101', '2015/2016', '2', '400', 'CSC 419', '4', '', '', '72', ''),
    (37, 2, '06/55EJ101', '2015/2016', '2', '400', 'CSC 419', '4', '', '', '62', ''),
    (38, 2, '06/55EJ102', '2015/2016', '2', '400', 'CSC 419', '4', '', '', '45', ''),
    (39, 2, '06/55EJ103', '2015/2016', '2', '400', 'CSC 419', '4', '', '', '37', ''),
    (40, 2, '06/55EJ104', '2015/2016', '2', '400', 'CSC 419', '4', '', '', '50', ''),
    (41, 2, '03/55EC101', '2015/2016', '2', '400', 'MAT 412', '3', '', '', '56', ''),
    (42, 2, '06/55EJ101', '2015/2016', '2', '400', 'MAT 412', '3', '', '', '45', ''),
    (43, 2, '06/55EJ102', '2015/2016', '2', '400', 'MAT 412', '3', '', '', '46', ''),
    (44, 2, '06/55EJ103', '2015/2016', '2', '400', 'MAT 412', '3', '', '', '57', ''),
    (45, 2, '06/55EJ104', '2015/2016', '2', '400', 'MAT 412', '3', '', '', '67', ''),
    (46, 2, '03/55EC101', '2015/2016', '2', '400', 'CSC 412', '3', '', '', '67', ''),
    (47, 2, '06/55EJ101', '2015/2016', '2', '400', 'CSC 412', '3', '', '', '55', ''),
    (48, 2, '06/55EJ102', '2015/2016', '2', '400', 'CSC 412', '3', '', '', '60', ''),
    (49, 2, '06/55EJ103', '2015/2016', '2', '400', 'CSC 412', '3', '', '', '69', ''),
    (50, 2, '06/55EJ104', '2015/2016', '2', '400', 'CSC 412', '3', '', '', '59', ''),
    (51, 0, '06/55EC101', '2015/2016', '1', '400', 'PHY 152', '3', '20', '58', '78', 'A');

关于如何在第二张图像中获得结果的任何有用线索,我将不胜感激。谢谢。

4

1 回答 1

1

你的意思是这样的:

<?php

    $db = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'root', '');
    $stmt = $db->query("SELECT DISTINCT course_code FROM tbl_result_400");

    $k = 1;
    echo "<table border = 1 bgcolor = 'white' class='member_tableheader'><tr>\n";
    echo "<td width = '50px' align='left' valign='top'>COURSES<BR>UNITS</td>\n";
    $coursesArray = array();
    while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $v = $row['course_code'];
        $stmt2 = $db->query("SELECT course_unit FROM tbl_result_400 WHERE course_code = '$v'");
        $rr = $stmt2->fetch(PDO::FETCH_ASSOC);
        echo "<td width = '100px' align='center' valign='top'>".$row['course_code']."<BR>"."(".$rr['course_unit'].")"."<BR>"."</td>\n";
        $k++;
        $coursesArray[$k] = $row['course_code'];
    }

    $flipArray = array_flip($coursesArray);
    $createEmpty = array();
    foreach($flipArray as $num => $value){
        $createEmpty[$num] = 0;
    }

    echo "</tr><tr>";
    $stmta = $db->query("SELECT DISTINCT matric_no FROM tbl_registered_courses_400 ORDER BY matric_no");

    $z = 0;
    $newDisplay = array();
    while( $row = $stmta->fetch(PDO::FETCH_ASSOC)) {
        $vv = $row['matric_no'];
        $t = 1;
        $stmt = $db->query("SELECT * FROM tbl_result_400 where matric_no = '$vv'");
        $n = $stmt->rowCount();
        echo "<td width = '100px' align='center' valign='top'>".$vv."a</td>\n";


        $newDisplay[$z] = $createEmpty;

        while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
            foreach($coursesArray as $num => $name){
                if($row['course_code'] == $name){
                    $newDisplay[$z][$name] = $row['score'];
                }
            }
        }


        foreach($newDisplay[$z] as $num => $value){
            if($value){
                echo "<td width = '100px' align='center' valign='top'>".$value."</td>\n";    
            } else {
                echo "<td width = '100px' align='center' valign='top'> &nbsp; --- &nbsp; </td>\n";
            }
        }

        echo "</tr><tr>";


        $z++;

    }
    echo "</tr>";
    echo "</table>";

?>
于 2012-08-24T11:56:47.533 回答