数据库记录中的多级菜单 [英] Multilevel menu from database records

查看:75
本文介绍了数据库记录中的多级菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有关PHP的一些帮助.我有一个可以正常工作的多级css菜单,但是现在我想根据数据库中的记录进行生成.

I need some help with PHP. I have a multilevel css menu that works fine, but now I want to generate according to the records from a database.

菜单代码:

<div id="page-wrap">
    <ul class="dropdown">
        <li><a href="#">Link 1</a> </li>
        <li><a href="#">Link 2</a> </li>
        <li><a href="#">Link 3</a> </li>
        <li><a href="#">Link 4</a>
        <ul class="sub_menu">
            <li><a href="#">Link 4 - 1</a></li>
            <li><a href="#">Link 4 - 2</a></li>
            <li><a href="#">Link 4 - 3</a></li>
        </ul>
        </li>
        <li><a href="#">Link 5</a></li>
        <li><a href="#">Link 6</a> </li>
    </ul>
</div>

在数据库中,每个记录都有一个名为 main_manu 的字段,如果该字段的值是 Yes 并且值是,则它成为主链接.否,然后它具有另一个字段( sub_menu ),该字段指示应在其中放置此链接的父菜单.参见屏幕截图. 数据库字段

所以,现在我不知道如何做将获得记录的php片段,如果main_menu的值为yes,它将创建一个顶层菜单,并且如果 main_menu 的值否,它将根据 sub_menu 字段的值创建一个子菜单.

In the database every record has a field called main_manu which makes it a main link if the value of this field is Yes, and if the value is No then it has another field (sub_menu) which says the parent menu in which this link should be placed.! See the screenshot. DB Fields

SO, now I don't know how to do the php piece that will get the records and if the value of the main_menu is yes, it will create a top level menu, and if the value of main_menu is no, it will create a sub level menu according to the value of sub_menu field.

非常感谢

更新

这是我到目前为止的代码,它可以正常工作.如果我可以使用单个查询而不是多个嵌套查询,那就太好了.

This is the code I have so far and it works. It would be nice if I could use a single query instead of multiple nested queries.

<div id='page-wrap'>
    <ul class='dropdown'>
<?

$sql_menu = "SELECT * FROM content WHERE visible = '1' and main_menu='yes' ";
$result_menu = mysql_query($sql_menu); 
while($row = mysql_fetch_assoc($result_menu))
 {
$id=$row['id'];
$menu_top=$row['menu_name'];
$menu_url=$row['menu_url'];

print "<li><a href='$menu_url'>$menu_top</a>";

$sql_sub = "SELECT * FROM content WHERE sub_menu = '$menu_url' ";
$result_sub = mysql_query($sql_sub);
$num_rows = mysql_num_rows($result_sub);

while($row = mysql_fetch_assoc($result_sub))
 {
$id=$row['id'];
$menu_sub=$row['menu_name'];
$sub_url=$row['menu_url'];
If ($num_rows != 0)
{
print "<ul class='sub_menu'>
            <li><a href='$sub_url'>$menu_sub</a></li>
       </ul>";
}
}
print "</li>";
}
?>
</ul>
</div>

推荐答案

用于此目的的代码如下所示(无论您与数据库进行交互的方式如何,都需要对此进行更改):

The code for this would be something like the following (This will need to be changed for whatever way you interact with the database etc.):

// Here we do a query to get all the rows from the table
$db_result = db_execute_query('SELECT * FROM `menu_table` ORDER BY `order_no`');

// Here we take the rows and put it into a structured array
$items = array();
$hierarchy = array('' => array());
while ($row = db_fetch_row($db_result)) {
    $items[$row['menu_name']] = $row['menu_name_en'];
    if ($row['main_menu'] == 'yes') {
        $hierarchy[''][] = $row['menu_name'];
    } else {
        if (!isset($hierarchy[$row['sub_menu']]) {
            $hierarchy[$row['sub_menu']] = array();
        }
        $hierarchy[$row['sub_menu']][] = $row['menu_name'];
    }
}

// Here we define a recursive function to run through our $hierarchy array;
function show_menu($name = '') {
    if (isset($hierarchy[$name])) {
        if ($name == '') {
                echo '<ul class="dropdown">';
        } else {
                echo '<ul class="sub_menu">';
        }

        foreach ($hierarchy[$name] as $sub) {
            echo '<li><a href="#">' . $items[$sub] . '</a>';
            show_menu($sub);
            echo '</li>';
        }

        echo '</ul>';
    }
}

// Here we execute the recursive function on the main menu
show_menu('');

尝试了解我在这里所做的事情,而不仅仅是逐字地执行它.一旦您了解了递归函数,就可以为您打开一个全新的世界.

Try to understand what I'm doing here instead of just implementing it verbatim. Once you get to know recursive functions, a whole new world can open for you.

还请注意,可以更改您的数据库表以使此代码更简单

Also note that your db table could be changed to make this code simpler

这篇关于数据库记录中的多级菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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