我有这段代码,从mySQL中获取数据并将其作为数组(多维)输出,如下所示:
$res = @mysqli_query($dbc, "SELECT id, subject, exam, marks, remarks FROM log WHERE student = '2222' AND term = 'Term2'");
$studentMarks = array();
while ($row = mysqli_fetch_array($res, MYSQLI_ASSOC))
{
$studentMarks[$row['subject']][$row['exam']] = $row['marks'];
}
//So far the code displays $studentMarks like this:
//$studentMarks = array(
// Maths => array('CAT1' => 50, 'CAT2' => 35)
// , English => array('CAT1' => 20, 'MIDTERM' => 40)
//);
//I wish it to add remarks thus displaying $studentMarks like:(Not sure whether I am right)
//$studentMarks = array(
// Maths => array('CAT1' => array(50,
// "Good"
// ),
// array('CAT2' => array(35,
// "Can do better"
// )
//)
// , English => array('CAT1' => array(20,
// "Good"
// ),
// array('MIDTERM' => array(40,
// "Much better"
// )
//);
echo '<table><thead><tr>';
echo '<td>Student</td><td>CAT1</td><td>CAT2</td><td>MIDTERM</td>';
echo '</tr></thead><tbody>';
foreach($studentMarks as $studentSub => $marks)
{
echo '<tr>';
echo '<td>', $studentSub, '</td>';
echo '<td>', (isset($marks['CAT1']) ? $marks['CAT1'] : ' '), '</td>';
echo '<td>', (isset($marks['CAT2']) ? $marks['CAT2'] : ' '), '</td>';
echo '<td>', (isset($marks['MIDTERM']) ? $marks['MIDTERM'] : ' '), '</td>';
echo '</tr>';
}
echo '</tbody></table>';
//结束代码我正在努力实现这个目标:
科目CAT 1 | CAT 2 |期中|期末
数学,,,, 50,,35,
,,,,,,,,良好的大敌;还能做得更好吗
中文 ,,,,,,,,,, 40
,,,,,,,,我能做得更好,
问题:
如何在/下对显示的每个标记显示注释?
$res = @mysqli_query($dbc, "SELECT id, subject, exam, marks, remarks FROM log WHERE student = '2222' AND term = 'Term2'");
$studentMarks = array();
while ($row = mysqli_fetch_array($res, MYSQLI_ASSOC))
{
$studentMarks[$row['subject']][$row['exam']]['mark'] = $row['marks'];
//This will get the remarks from the database
$studentMarks[$row['subject']][$row['exam']]['remark'] = $row['remarks'];
//This should be used when you chose to compare marks and display appropriate remarks
//$studentMarks[$row['subject']][$row['exam']]['remark'] = ( $row['marks'] < 40)?'can do better':( $row['marks'] < 50)?'do better':'good';
}
echo '<table><thead><tr>';
echo '<td>Student</td><td>CAT1</td><td>CAT2</td><td>MIDTERM</td>';
echo '</tr></thead><tbody>';
foreach($studentMarks as $studentSub => $marks)
{
echo '<tr>';
echo '<td>', $studentSub, '</td>';
echo '<td>', (isset($marks['CAT1']) ? $marks['CAT1']['mark'] : ' '), '</td>';
echo '<td>', (isset($marks['CAT2']) ? $marks['CAT2']['mark'] : ' '), '</td>';
echo '<td>', (isset($marks['MIDTERM']) ? $marks['MIDTERM']['mark'] : ' '), '</td>';
echo '</tr>';
echo '<tr>';
echo '<td> </td>';
echo '<td>', (isset($marks['CAT1']) ? $marks['CAT1']['remark'] : ' '), '</td>';
echo '<td>', (isset($marks['CAT2']) ? $marks['CAT2']['remark'] : ' '), '</td>';
echo '<td>', (isset($marks['MIDTERM']) ? $marks['MIDTERM']['remark'] : ' '), '</td>';
echo '</tr>';
}
echo '</tbody></table>';