简体   繁体   English

PHP数组多个字段从Mysql循环数组

[英]PHP Array Multiple fields looping Array from Mysql

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. 我已经尝试了7个多小时,但仍然不知道如何操作。

$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. 我想在输出中添加name字段和roll字段。 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 ;

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>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM