0

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 ;
2
  • 1
    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. Commented Jul 28, 2014 at 19:23
  • thanks for your input. will surely check it. Commented Jul 28, 2014 at 19:49

1 Answer 1

0

This looks like an addon to existing code so the following should work without breaking it

$sub = array();
$query = mysql_query("SELECT regd, name, rollno as 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");
$array = array(); //create new array
while ($row = mysql_fetch_assoc($query)) {
    $sub[$row['regd']][$row['Subject']] = $row['hl'];

//by doing this, you preserve existing code
    $array[$row['regd']]['roll'] = $row['roll'];
    $array[$row['regd']]['name'] = $row['name'];
}
$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>';
echo '<td>roll</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>'.$array[$key]['name'].'</td>';
    echo '<td>'.$array[$key]['roll'].'</td>';
    foreach ($val as $v) { //loop through each Subjects for each regd
        echo '<td>'.$v.'</td>';
    }
    echo "</tr>";
}
echo "</table>";
18
  • it does output anything. I don't change anything. Commented Jul 28, 2014 at 18:06
  • Well if there are no results, then the problem is in your mysql query, this will at the very least add a column... Commented Jul 28, 2014 at 18:08
  • Thanks for your input, only name and roll field go blank. I will recheck my code. Anyway is there anything wrong with this sql query? Commented Jul 28, 2014 at 18:09
  • If there is no error message it could be that no rows match the conditions in the WHERE clause. After $subkey = key($sub), do var_dump($sub) and it will give you an array if there are results. If the array is empty, then you have no results. Then remove conditions 1 by 1 (starting with the group by) until you have results. That way you will know where it breaks. Commented Jul 28, 2014 at 18:12
  • The var_dump(); output everythings. but in the table, name and roll go blank. Commented Jul 28, 2014 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.