我有一个包含数据的学术表。
例如:
级别 | 百分比 |
---|---|
学位 | 50 |
文凭 | 70 |
文凭后 | 67 |
这是我的代码,但不起作用:
<table>
<tr>
<th>Degree</th>
<th>Diploma</th>
$stmtc = $conn->prepare("SELECT
MAX(CASE WHEN level = 'Degree' THEN percentage END) AS $degree,
MAX(CASE WHEN level = 'Diploma' THEN percentage END) AS $diploma
FROM tbl_academic_qualification WHERE member_no = '$emp_id'");
$stmtc->execute();
$resultc = $stmtc->fetchAll();
foreach($resultc as $rowc){
$degree = $rowc['percentage'];
$diploma = $rowc['percentage'];
?>
<tr>
<td><?php echo "$degree"; ?></td>
<td><?php echo "$diploma"; ?></td>
</tr>
</table>
我想这样显示:
学位 | 文凭 | 文凭后 |
---|---|---|
50 | 70 | 67 |
试试这个:
<table border="1">
<tr>
<th>Degree</th>
<th>Diploma</th>
<th>Post Diploma</th>
</tr>
<?php
$stmt = $conn->prepare("SELECT
MAX(CASE WHEN level = 'Degree' THEN percentage END) AS Degree,
MAX(CASE WHEN level = 'Diploma' THEN percentage END) AS Diploma,
MAX(CASE WHEN level = 'Post Diploma' THEN percentage END) AS Post_Diploma
FROM tbl_academic_qualification WHERE member_no = :emp_id");
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>";
echo "<td>" . $row['Degree'] . "</td>";
echo "<td>" . $row['Diploma'] . "</td>";
echo "<td>" . $row['Post_Diploma'] . "</td>";
echo "</tr>";
}
?>
</table>