使用可选的空字段显示整个表

时间:2015-08-28 21:15:44

标签: php mysql sql mysqli

以下是数据库的外观 enter image description here

所以我想像

一样显示它
Champion name 
name of the column e.g. Q name of the spell - Surging Tides 
the rest of spells for that champion
Next Champion etc., 

这是我现在显示冠军名称的方式

$champions = $conn->prepare("SELECT *
                             FROM champions 
                             Where Patch_No = ?");
$champions->bind_param('s', $Patch_No);
$champions->execute();
$champions_result = $champions->get_result();

while($row = $champions_result->fetch_assoc()){
    echo $row['Champion'].' '.$row['NumNotNull'].'<br>';    
}

我无法想到用尽可能少的查询来做到这一点的简单方法。

这是另一个示例,它应该是什么样的

enter image description here

1 个答案:

答案 0 :(得分:1)

$row是一个关联数组,因此您可以使用foreach遍历它并测试该列是否为空。

while($row = $champions_result->fetch_assoc()){
    echo $row['Champion'].' '.$row['NumNotNull'].'<br>';
    foreach ($row as $column_name => $column) {
        if ($column_name == 'Champion' || $column_name == 'NumNotNull') {
            continue; // These fields were already displayed above
        }
        if (!empty($column)) {
            echo "$column_name $column<br>";
        }
    } 
}