I want to add more columns to my output in the following code. I have been trying more than 7 hours and still could not figure out how.
$sub=array();
$query=mysql_query("SELECT regd, name, roll, Subject,
SUM( Mark_score ) / SUM( Full_Mark ) *100 AS hl,
SUM( Full_Mark ) AS hm
FROM entry
WHERE Year = '2013'
AND Section = 'A'
AND Name_of_exam = 'First Term Exam'
GROUP BY regd, Subject");
while ($row=mysql_fetch_assoc($query)){
$sub[$row['regd']][$row['Subject']] = $row['hl'];
}
$subkey=key($sub); //get the 1st regd key, to be used to get the Subject keys
echo "<table border=1>";
echo "<tr>";
echo '<td>regd</td>';
echo '<td>name</td>';
foreach($sub[$subkey] as $keys=>$vals){
echo "<td>".$keys."</td>";
}
echo "</tr>";
foreach($sub as $key=>$val){ //loop through each regd value, creating a row
echo "<tr>";
echo '<td>'.$key.'</td>';
echo '<td>'.$name.'</td>';//How do I put name array here?
foreach($val as $v){ //loop through each Subjects for each regd
echo '<td>'.$v.'</td>';
}
echo "</tr>";
}
echo "</table>";
I want to add name
field and roll
field to the output. Thanks for your input.
Updated with table:
CREATE TABLE IF NOT EXISTS `entry` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`regd` bigint(20) NOT NULL,
`name` varchar(30) NOT NULL,
`rollno` int(11) NOT NULL,
`section` varchar(5) NOT NULL,
`univ_roll` varchar(50) NOT NULL,
`year` year(4) NOT NULL,
`subject` varchar(50) NOT NULL,
`Name_of_exam` varchar(40) NOT NULL,
`Mark_score` int(11) NOT NULL,
`Full_Mark` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `regd` (`regd`),
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;
mysql_query
is an obsolete interface and should not be used in new applications and will be removed in future versions of PHP. A modern replacement like PDO is not hard to learn. If you're new to PHP, a guide like PHP The Right Way can help explain best practices.