如何使用PHP在平面HTML表格中显示规范化的MySQL数据?

时间:2013-06-15 12:39:05

标签: php html mysql

我想以表格格式显示此数据,例如

<table border='1'>
    <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>City</th>
        <th>State</th>
        <th>Phone</th>
    </tr>

但我的表格,列式,就像

userdbelemnts_id     userdbelements_field_name      userdbelements_field_value  

180                  user_first_name                Demo
181                  user_last_name                 Agent
183                  City                           Mumbai
184                  zip                            400000
185                  state                          xyz
189                  phone                          123456

如何在上表结构中展平标准化数据?

1 个答案:

答案 0 :(得分:1)

您可以在查询中对输出数据进行非规范化处理,例如:userdb_id上的分组:

SELECT
    MAX(CASE WHEN userdbelements_field_name = 'user_first_name' 
        THEN userdbelements_field_value ELSE NULL END) AS first_name,
    MAX(CASE WHEN userdbelements_field_name = 'user_last_name' 
        THEN userdbelements_field_value ELSE NULL END) AS last_name,
    MAX(CASE WHEN userdbelements_field_name = 'City' 
        THEN userdbelements_field_value ELSE NULL END) AS city,
    MAX(CASE WHEN userdbelements_field_name = 'state' 
        THEN userdbelements_field_value ELSE NULL END) AS state,
    MAX(CASE WHEN userdbelements_field_name = 'zip' 
        THEN userdbelements_field_value ELSE NULL END) AS zip,
    MAX(CASE WHEN userdbelements_field_name = 'phone' 
        THEN userdbelements_field_value ELSE NULL END) AS phone,
FROM userdbelemnts
GROUP BY userdb_id

然后在你的php中,只需像在平面表中一样遍历结果。

<thead>
    <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>City</th>
        <th>State</th>
        <th>Phone</th>
    </tr>
</thead>
<tbody>
<?php while ($row = mysqli_fetch_assoc($result)) { ?>
    <tr>
        <td><?=$row['first_name']?></td>
        <td><?=$row['last_name']?></td>
        <td><?=$row['city']?></td>
        <td><?=$row['state']?></td>
        <td><?=$row['phone']?></td>
    </tr>
<?php } ?>
</tbody>

修改:根据以下评论给出新的表格架构:

SELECT
    orodha_en_userdb.*,
    MAX(CASE WHEN userdbelements_field_name = 'user_first_name' 
        THEN userdbelements_field_value ELSE NULL END) AS first_name,
    MAX(CASE WHEN userdbelements_field_name = 'user_last_name' 
        THEN userdbelements_field_value ELSE NULL END) AS last_name,
    MAX(CASE WHEN userdbelements_field_name = 'City' 
        THEN userdbelements_field_value ELSE NULL END) AS city,
    MAX(CASE WHEN userdbelements_field_name = 'state' 
        THEN userdbelements_field_value ELSE NULL END) AS state,
    MAX(CASE WHEN userdbelements_field_name = 'zip' 
        THEN userdbelements_field_value ELSE NULL END) AS zip,
    MAX(CASE WHEN userdbelements_field_name = 'phone' 
        THEN userdbelements_field_value ELSE NULL END) AS phone,
FROM orodha_en_userdbelements
    INNER JOIN orodha_en_userdb
    ON orodha_en_userdbelements.userdb_id = orodha_en_userdb.userdb_id
WHERE orodha_en_userdb.userdb_id = $id
GROUP BY orodha_en_userdb.userdb_id