使用更简单的方法构建无序列表导航菜单 - 代码美化问题 [英] Building unordered list navigation menu with an easier way - Code Beautifying Question

查看:184
本文介绍了使用更简单的方法构建无序列表导航菜单 - 代码美化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个动态菜单,基于数据库上的三个表,反映在下面的框架之下:

I wrote a dynamic menu based on three table on db which reflects below level frame:

> Section
>> Categories
>>> Subcategory

这是我的代码:

include("connDB.php");

echo '<ul>';
$q1 = mysql_query("SELECT * FROM section ORDER BY section_name ASC");

while($getSection = mysql_fetch_array($q1)) {
    echo "<li><a href='content.php?sec={$getSection['section_id']}&cat='>{$getSection['section_name']}</a>";

    $q2 = mysql_query("SELECT * FROM category WHERE section_id = '{$getSection['section_id']}'");
    if(mysql_num_rows($q2) > 0) {
        echo "<ul>";
        while($getCat = mysql_fetch_array($q2)) {
            echo "<li><a href='content.php?sec={$getSection['section_id']}&cat={$getCat['category_id']}&scat='>{$getCat['category_name']}</a>";

            $q3 = mysql_query("SELECT * FROM subcategory WHERE category_id = '{$getCat['category_id']}'");
            if(mysql_num_rows($q3) > 0) {
                echo "<ul>";
                while($getSubCat = mysql_fetch_array($q3)) {
                    echo "<li><a href='content.php?sec={$getSection['section_id']}&cat={$getCat['category_id']}&scat={$getSubCat['subcategory_id']}&getDetail='>{$getSubCat['subcategory_name']}</a></li>";
                }
                echo "</ul>";
                echo "</li>";
            }
        }
        echo "</ul>";
        echo "</li>";
    }
}
echo '</ul>';

我想知道如果我能找到任何帮助,以更好地美化这个基本级别的代码,更专业的方式?
感谢您的帮助。

I am wondering if I can find any help to beautify this basic-level code to a better way, more professional way? Thanks for help.

推荐答案

所以我知道这是一个老问题。然而,对于任何绊脚石的人来说,都有一个解决方案。它与其他答案相同,但有时也被称为修改的预订树遍历(MPTT)

So I know this is an old question. However, for anyone who stumbles upon this there is a solution. It's identical to the other answer however it's also sometimes referred to as Modified Preordered Tree Traversal (MPTT)

一些网络框架,如 CakePHP 和Django,将其内置并将其称为树,它将大大增加菜单的灵活性以及保留顺序。

Some web frameworks, like CakePHP and Django, have it built in and refer to it as a "Tree" it would greatly increase the flexibility of your menu as well as preserve order.

这篇关于使用更简单的方法构建无序列表导航菜单 - 代码美化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆