3
\$\begingroup\$

I want to display a table similar to this:

Desired Table

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

Information

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>
\$\endgroup\$
5
\$\begingroup\$
////////////////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.

\$\endgroup\$
4
\$\begingroup\$

Probably the biggest thing that I would suggest you improve upon, is your spacing. For example, compare the following two code blocks.

Code Block 1

$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++; 
}

Code Block 2

$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++; 
}

I believe it's obvious which is easier to read and more pleasant to look at. You should always make sure to indent blocks (code between {}) and to leave space between operators (+, -, =, etc.)

One way to rectify this would be to pick a style guide and stick with it. According to Wikipedia, a style guide is.

A style guide is a set of rules or guidelines used when writing the source code for a computer program.

You should preferably choose a style guide from a reputable PHP project. Such as the following projects.

  1. CodeIgniter User Guide Version 2.1.2
  2. PEAR Manual Coding Standards
  3. Programmer's Reference Guide

Of the three, the Programmer's Reference Guide is from the Zend Framework Manual. Zend is the company behind PHP and so most PHP programmers would be more apt to recognize their style guide as the official one. Ergo, I would probably choose the Programmer's Reference Guide as my personal reference guide. Hope this helps.

\$\endgroup\$
  • \$\begingroup\$ Thank you .. just i wrote it to display the idea and i will use codeigniter for my application .. so just i want to ask is this way -arrays- a good way or not ? \$\endgroup\$ – HawkEye Jul 7 '12 at 8:11

protected by Jamal Aug 1 '18 at 3:48

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.