2012-08-24 39 views
-1

我有一張表,我需要在部門中顯示學生的結果。我創建了測試基數和給他們分配的分數。假設8名學生處於400水平,8名學生中有3名沒有結轉,而其他5名學生有結餘。該表應該顯示8名學生註冊的不同課程及其相應的分數。如果某個課程沒有註冊,它應該顯示默認值。在MySQl中不顯示默認結果表

下面是實現這一結果

<?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>"; 
?> 

示例腳本下面的圖像顯示的是我所得到的結果是: enter image description here

期望的結果是在下面的圖片: enter image description here

從上面的圖片可以看出,matric_no 03/55ec101的學生沒有註冊課程PHY 152的註冊表,因此沒有註冊掉話ult得分爲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'); 

我將不勝感激任何有用的線索,我怎樣才能得到第二幅圖像中的結果。 謝謝。

+0

你應該總結你的實際問題。你的問題到底是什麼? – feeela

+0

請不要使用'mysql_ *'函數來編寫新的代碼。他們不再維護,社區已經開始[棄用程序](http://goo.gl/KJveJ)。查看[*紅色框*](http://goo.gl/GPmFd)?相反,您應該瞭解[準備好的語句](http://goo.gl/vn8zQ)並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli的)。如果你不能決定哪些,[這篇文章](http://goo.gl/3gqF9)會幫助你。如果你選擇PDO,[這裏是很好的教程](http://goo.gl/vFWnC)。 –

回答

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>"; 

?> 
+0

Lorga,我以前沒有用過PDo。讓我複製你的代碼,並粘貼,看看我是否會得到預期的結果 –

+0

你應該開始使用PDO和'mysql_ *'得到不推薦使用:) –

+0

lorga:是的,你又做了。我會去學習如何使用PDO。它工作正常,並且如預期的那樣。請我需要你的私人導師。感謝一個偉大的交易。在這裏在Stackoverflow Pple已經非常有幫助。 –