0

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

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

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>

我问这是否是一个好方法

对不起我的英语

4

1 回答 1

1

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

于 2012-07-06T17:00:54.070 回答