学生的分数报告

时间:2012-07-06 16:25:54

标签: php mysql

请帮我写这段代码 我想要显示这样的表格 http://imageshack.us/photo/my-images/443/reportw.jpg/

从三个数据库表生成取决于学生ID

http://img51.imageshack.us/img51/1272/tablesh.jpg

所以我想显示这张表没有任何固定的考试或科目值..我是这样做的,它运作良好,但我希望知道这是不是最好的方式

 <? 


$con = mysql_connect("localhost","root","123"); 
if (!$con) 
  { 
  die('Could not connect: ' . mysql_error()); 
   } 
mysql_select_db("test", $con); 


//////////////////////// 



///////////////// Select the exams and put it in array  
$ex = mysql_query("SELECT * FROM exams");  
$dc=1; 
while ($rowex= mysql_fetch_array($ex)){ 
$exn[$dc]=$rowex['Exam_Title']; 
$exid[$dc]=$rowex['Exam_ID']; 
$dc++; 
} 


/////////////////////////// Select the subjects and put it in array  
$sj = mysql_query("SELECT * FROM subjects");  
$dsj=1; 
while ($rowsj= mysql_fetch_array($sj)){ 
$sjn[$dsj]=$rowsj['Subj_Title']; 
$sjid[$dsj]=$rowsj['Subj_ID']; 
$dsj++; 
} 

////////////////Select the student marks and put it in array with subject id and exam id 

$result = mysql_query("SELECT * FROM stu_marks");  
while ($row= mysql_fetch_array($result)){ 
$arr[$row['Subj_ID']][$row['Exam_ID']]=$row['Grade']; 

} 
/////////////////////// count the exams and the subjects to draw the table 
 $exc=count($exn); 
 $sjc=count($sjn); 
 ?> 

 <table width="400" border="1"> 
  <tr> 



    <? 
///////// display subjects in table rows 
for ($d=0;$d<=$sjc;$d++){ 

if ($d==0){ 
    echo '<td>-</td>'; 
}else{ 
    echo ' <tr><td>'.$sjn[$d].'</td>'; 
} 

 ///////// display exams in table head tds  

for ($p=1;$p<=$exc;$p++){ 
if ($d==0){ 
    echo '<td> '.$exn[$p].'</td>'; 
}else{ 
?> 
 <td> 

 <?=$arr[$sjid[$d]][$exid[$p]]?> 

 </td> 
  <?} 
  } 
 }?> 

  </table>

我问这是否是一个很好的方法

抱歉我的英语

1 个答案:

答案 0 :(得分:0)

如果代码按原样运行,并且您只是想知道是否有更好的方法,那么Code Review: Stack Exchange比这更合适。