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

How to convert resultset to nested unordered list and hide sublist items with a value of 0?

我试图在列表和子列表中显示以下 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>

数据显示在列表和子列表中。我不想在子列表中显示 0 值。

除了在子列表中显示 0 之外,一切正常。请指教

如果你只是不想显示 0,试试这个

<?php echo ($subcategory != '0')? '<li>'.$test.'</li>' : ''; ?>

如果你不想存储在数组中,那么如果条件

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

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

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

  • 查询数据库时,仅 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 提供)