如何将结果集转换为嵌套的无序列表并隐藏值为0的子列表项?

时间:2018-03-07 05:51:14

标签: php loops conditional html-lists resultset

我试图在列表和子列表中显示下表值。

[Database table

这是我的for循环显示

$sql ="SELECT *FROM objectives"; 
$result = $conn->query($sql);

$categories = array();

foreach ($result as $result) {
    $category = $result['content'];
    $categories[$category][] = $result['sub_content'];
}
?>
<ul>
<?php foreach ($categories as $category => $subcategories): ?>
  <li>
    <?php echo $category; ?>
    <ul>
<?php foreach ($subcategories as $subcategory):?>
      <li><?php echo $subcategory; ?></li>
<?php endforeach; ?>
    </ul>
  </li>
<?php endforeach; ?>
</ul>

output

数据显示在列表中并带有子列表。我不想在子列表中显示0值。

除了在子列表中显示0外,一切都很好。请指教。

2 个答案:

答案 0 :(得分:2)

如果您不想显示0

,请尝试此操作
<?php echo ($subcategory != '0')? '<li>'.$test.'</li>' : ''; ?>

如果您不想存储在数组中,请将此if条件

foreach ($result as $result) {
    $category = $result['content'];
    if($result['sub_content'] != '0'){
       $categories[$category][] = $result['sub_content'];
    }
}

答案 1 :(得分:1)

简单地实施echo ($subcategory != '0')? '<li>'.$test.'</li>' : '';将导致dom中出现不必要的标记。具体来说,您将使用空<ul></ul>个标记作为嵌套列表,其中只有包含$subcategory的单行为0。 (Demonstration)当应用css /样式时,这些额外的标记位可能会导致时髦的副作用。

作为最佳实践,建议进一步改进:

  • 查询数据库时,只有SELECT您特别需要的列。
  • 使用ORDER BY子句为您的流程添加稳定性,该子句将按content对行进行分组,并可能对sub_content进行排序
  • 永远不要使用超过必要的循环。这个任务可以(因此,理论上应该)在一个循环中执行。

推荐代码:(Demo

$result = $conn->query("SELECT content, sub_content FROM objectives");
$category = null;
$output = '';
foreach ($result as $row) {
    if ($category !== $row['content']) {             // new parent
        if ($category !== null) {                    // not first iteration
            $output .= "<li>$category";              // print parent
            if ($sublist) {
                $output .= "<ul>$sublist</ul>";      // print all children
            }
            $output .= "</li>";
        }
        $category = $row['content'];                 // overwrite $category
        $sublist = '';                               // reset sublist
    }
    if ($row['sub_content'] !== '0'){                // filter row
        $sublist .= "<li>{$row['sub_content']}</li>";
    }
}
if ($result) {                                       // in case the resultset is empty
    echo "<ul>";
        echo $output;                                // print stored markup
        echo "<li>$category";                        // print last parent
        if ($sublist) {
            echo "<ul>$sublist</ul>";                // print all children from last parent
        }
        echo  "</li>";
    echo "</ul>";
}

源代码输出:

<ul>
    <li>Demonstrate where to find the following documentation:
        <ul>
            <li>Operating and Safety Strategy</li>
        </ul>
    </li>
    <li>Explain the different turbine main operating states:
        <ul>
            <li>Power Production</li>
            <li>Idle</li>
            <li>Stop</li>
        </ul>
    </li>
    <li>Explain how to recognise the current operating mode on the display of the operating panel</li>
    <li>Explain the subsystem operating modes:
        <ul>
            <li>Stop</li>
            <li>Manual</li>
        </ul>
    </li>
    <li>Explain the difference between local and remote point of operation</li>
    <li>Explain that only one point of operation can be active at a time</li>
</ul>

渲染输出:(由phptester.net提供)

enter image description here