如何在 PHP 中将行数据转换为列

问题描述 投票:0回答:1

我有一个包含数据的学术表。

例如:

级别 百分比
学位 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
php html pivot fetch
1个回答
0
投票

试试这个:

<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>
© www.soinside.com 2019 - 2024. All rights reserved.