这是我的数据表1.我希望这个表更改为table2.我必须使用PHP.我使用foreach编写了一些代码,但这样做不正常. C
表格1
|-------+------------+--------------+---------+--------|
|Seq No | Student Id | Subject Name | Exams | Marks |
|-------+------------+--------------+---------+--------|
| 1 | 200301 | maths | Exam 1 | 25 |
| 2 | 200301 | maths | Exam 2 | 45 |
| 3 | 200301 | art | Exam 1 | 76 |
| 4 | 200301 | art | Exam 2 | 42 |
| 5 | 200302 | maths | Exam 1 | 71 |
| 6 | 200302 | maths | Exam 2 | 78 |
| 7 | 200302 | art | Exam 1 | 35 |
| 8 | 200302 | art | Exam 2 | 61 |
|-------+------------+--------------+---------+--------|
我想用PHP创建类似table2.
表2
|-------+------------+-----------------+-----------------+
| | | maths | art |
|Seq No | Student Id |-----------------|-----------------|
| | | Exam 1 | Exam 2 | Exam 1 | Exam 2 |
|-------+------------+-----------------+-----------------+
| 1 | 200301 | 25 | 45 | 76 | 42 |
| 2 | 200302 | 71 | 78 | 35 | 61 |
|-------+------------+--------+--------+--------+--------|
**我写了这样的一些.但这不能正常工作. **
$output .= "<table border=1>
<tr bgcolor=#ffffff><td>SeqNo</td>
<td>Student Id</td>
<td>maths</td>
<td>art</td>
<td>Exam 1</td>
<td>Exam 2</td></tr>";
$studentCounter = 0;
foreach($result as $item)
{
$output .= "<tr><td>" . ++$studentCounter . " </td>
<td>" . $item[STUDENT_ID] . "</td>
<td>" . $item[MATHS] . "</td>
<td>" . $item[ART] . "</td>
<td>" . $item[EXAM_1] . "</td>
<td>" . $item[EXAM_2] . "</td></tr>";
}
$output .= "</table>";
这是我的阵列
Array([1] => Array([STUDENT_ID] => 200301
[SUBJECT_NAME] => maths
[ASSIGNMENT_TITLE] => exam_1
[MARKS] => 25 )
[2] => Array([STUDENT_ID] => 200301
[SUBJECT_NAME] => maths
[ASSIGNMENT_TITLE] => exam_2
[MARKS] => 45 )
[3] => Array([STUDENT_ID] => 200301
[SUBJECT_NAME] => art
[ASSIGNMENT_TITLE] => exam_1
[MARKS] => 76 )
[4] => Array([STUDENT_ID] => 200301
[SUBJECT_NAME] => art
[ASSIGNMENT_TITLE] => exam_2
[MARKS] => 42 )
[5] => Array([STUDENT_ID] => 200302
[SUBJECT_NAME] => maths
[ASSIGNMENT_TITLE] => exam_1
[MARKS] => 71 )
[6] => Array([STUDENT_ID] => 200302
[SUBJECT_NAME] => maths
[ASSIGNMENT_TITLE] => exam_2
[MARKS] => 78 )
[7] => Array([STUDENT_ID] => 200302
[SUBJECT_NAME] => art
[ASSIGNMENT_TITLE] => exam_1
[MARKS] => 35 )
[8] => Array([STUDENT_ID] => 200302
[SUBJECT_NAME] => art
[ASSIGNMENT_TITLE] => exam_2
[MARKS] => 61 )
)
这是我的数组数据.我必须使用这个数组创建类似table2.任何人都可以帮助我.
解决方法:
如果我理解正确,你希望表2如图所示那么基于我将回答你的问题.
您可以使用下面的HTML,它看起来像您想要的表格.
<table border=1>
<tr bgcolor=#ffffff>
<td>SeqNo</td>
<td>Student Id</td>
<td colspan = "2">maths</td>
<td colspan = "2">art</td>
</tr>
<tr>
<td colspan = "2"></td>
<td>Exam 1</td>
<td>Exam 2</td>
<td>Exam 1</td>
<td>Exam 2</td>
</tr>
<tr>
<td></td>
<td></td>
<td>Exam 1</td>
<td>Exam 2</td>
<td>Exam 1</td>
<td>Exam 2</td>
</tr>
</table>
你没有发布你的sql,你不需要使用转发,你可以循环它,如下所示.
<table border=1>
<tr bgcolor=#ffffff>
<td>SeqNo</td>
<td>Student Id</td>
<td colspan = "2">maths</td>
<td colspan = "2">art</td>
</tr>
<tr>
<td colspan = "2"></td>
<td>Exam 1</td>
<td>Exam 2</td>
<td>Exam 1</td>
<td>Exam 2</td>
</tr>
<?PHP while($row = $stmt -> fetch(PDO::FETCH_ASSOC){ ?>
<tr>
<td>$row["seqNo"]</td>
<td>$row["studen_id"]</td>
<td>$row["Exam1"]</td>
<td>$row["Exam2"]</td>
<td>$row["Exam1"]</td>
<td>$row["Exam2"]</td>
</tr>
<?PHP } ?>
</table>
请记住,这不是你的sql,所以调整它以满足你的需要,并在PDO中.