查询结果表中的多级无序列表

发布于 2024-11-04 00:32:50 字数 513 浏览 0 评论 0原文

我做了一个查询,我可以使用该查询的所有行用以下 thead 填充一个表。

section_id | section | category_id | category | subcategory_id | subcategory

现在我试图从这个填充的表创建一个多级无序列表。有可能做到吗?结果应该是这样的:

<ul>
  <li>Section Name
    <ul>
      <li>Category name for above section
        <ul>
          <li>Subcategory name for above category and section</li>
        </ul>
      </li>
    </ul>
  </li>
</ul>

感谢您的帮助。

I made a query which i can populate a table with following theads using all rows of that query.

section_id | section | category_id | category | subcategory_id | subcategory

Now I am trying to create a multilevel unordered list from this populated table. Is it possible to do it? Result should like this:

<ul>
  <li>Section Name
    <ul>
      <li>Category name for above section
        <ul>
          <li>Subcategory name for above category and section</li>
        </ul>
      </li>
    </ul>
  </li>
</ul>

Thank you for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

碍人泪离人颜 2024-11-11 00:32:50

也许是这样的(未经测试):

$tree = array();
while ($row = mysql_fetch_assoc($result)) {
    $tree[$row['section_id']]['name'] = $row['section'];
    $tree[$row['section_id']]['categories'][$row['category_id']]['name'] = $row['category'];
    $tree[$row['section_id']]['categories'][$row['category_id']]['subcategories'][$row['subcategory_id']]['name'] = $row['subcategory'];
}

echo '<ul>';
foreach ($tree as $sec_id => $section) {
    printf('<li><a href="test.php?sec=%d">%s</a><ul>', $sec_id, $section['name']);
    foreach ($section['categories'] as $cat_id => $category) {
        printf('<li><a href="test.php?sec=%d&cat=%d">%s</a><ul>', $sec_id, $cat_id, $category['name']);
        foreach ($category['subcategories'] as $scat_id => $subcategory) {
            printf('<li><a href="test.php?sec=%d&cat=%d&scat=%d">%s</a></li>', $sec_id, $cat_id, $scat_id, $subcategory['name']);
        }
        echo '</ul></li>';
    }
    echo '</ul></li>';
}
echo '</ul>';

Something like this, perhaps (untested):

$tree = array();
while ($row = mysql_fetch_assoc($result)) {
    $tree[$row['section_id']]['name'] = $row['section'];
    $tree[$row['section_id']]['categories'][$row['category_id']]['name'] = $row['category'];
    $tree[$row['section_id']]['categories'][$row['category_id']]['subcategories'][$row['subcategory_id']]['name'] = $row['subcategory'];
}

echo '<ul>';
foreach ($tree as $sec_id => $section) {
    printf('<li><a href="test.php?sec=%d">%s</a><ul>', $sec_id, $section['name']);
    foreach ($section['categories'] as $cat_id => $category) {
        printf('<li><a href="test.php?sec=%d&cat=%d">%s</a><ul>', $sec_id, $cat_id, $category['name']);
        foreach ($category['subcategories'] as $scat_id => $subcategory) {
            printf('<li><a href="test.php?sec=%d&cat=%d&scat=%d">%s</a></li>', $sec_id, $cat_id, $scat_id, $subcategory['name']);
        }
        echo '</ul></li>';
    }
    echo '</ul></li>';
}
echo '</ul>';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文