我有一张表,我需要在其中显示一个部门学生的成绩。我创建了测试矩阵编号并为它们分配了分数。假设 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 = " --- ";
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');
关于如何在第二张图像中获得结果的任何有用线索,我将不胜感激。谢谢。