I want to display a table similar to this:

> 
> ![Desired Table](https://i.stack.imgur.com/bvdeh.jpg)
> 
> 
> 

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

> 
> ![Information](https://i.stack.imgur.com/MIXvr.jpg)
> 
> 
> 

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?

```
<? 

$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>

```