Student marks report I want to display a table similar to this:


  


This table is generated from three tables depending on the student ID:


  


I don't want the table to depend on fixed values. For example, I should be able to add a subject or exam in the database and see the changes reflected in the table without touching the code. Any suggestions?

&lt;? 


$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); 
?&gt; 

 &lt;table width="400" border="1"&gt; 
 &lt;tr&gt; 



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

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

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

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

&lt;?=$arr[$sjid[$d]][$exid[$p]]?&gt; 

 &lt;/td&gt; 
 &lt;?} 
  } 
  }?&gt; 

  &lt;/table&gt;
////////////////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']; 
} 


I don't fully understand why you are pulling data out of an array to put it back into an array.  You should consider writing better, more specific sql queries so that the result of the individual SQl query is as close to what you want your table to look like as possible.  From there, you can then loop through the already made (via mysql_fetch_array) result and reduce the amount of redundancy.  

Also, use PDO, don't use or die (probably not ready for these two yet) and for the love of god, NAME YOUR VARIABLES DESCRIPTIVELY.  I can barely tell whats going on and it's not in any way complicated.