使用完整路径 (URLS) 和数据库中的值生成嵌套树

发布于 2024-12-07 14:11:21 字数 1582 浏览 2 评论 0原文

我从昨天开始就在研究这个问题。更具体地说 - 我在数据库中有一些值,如下所示:

____________________________________
| id | parentId |   name   |  url  |
------------------------------------
| 1  |     0    | Nieuws   | url_1 |
------------------------------------
| 2  |     0    | Reviews  | url_2 |
------------------------------------
| 3  |     0    | Meuk     | url_3 |
------------------------------------
| 4  |     1    | Games    | url_4 |
------------------------------------
| 5  |     1    | Internet | url_5 |
------------------------------------
| 6  |     5    | Browsers | url_6 |
------------------------------------

我希望看到基于这些值生成的树。目标形状应如下所示:

<ul>
    <li><a href="url_1">Nieuws</a>
        <ul>
            <li><a href="url_1/url_4">Games</a></li>
            <li><a href="url_1/url_5">Internet</a>
                <ul>
                    <li><a href="url_1/url_5/url_6">Browsers</a></li>
                </ul>
            </li>
        </ul>
    </li>
    <li><a href="url_2">Reviews</a></li>
    <li><a href="url_3">Meuk</a></li>
</ul>

对我来说,将所有斜线包含在树内(包含所有父级和子级的完整路径)非常重要。

我想补充一点,我在此页面上找到了代码: http://crisp.tweakblogs.net/blog/317/formatting-a-multi-level-menu-using-only-one-query.html 但我可以不是按照上述方式重做。我将非常感谢任何帮助,因为申请截止日期即将到来:(

I work on this issue since yesterday. More specifically - I have some values ​​in the database, which look as follows:

____________________________________
| id | parentId |   name   |  url  |
------------------------------------
| 1  |     0    | Nieuws   | url_1 |
------------------------------------
| 2  |     0    | Reviews  | url_2 |
------------------------------------
| 3  |     0    | Meuk     | url_3 |
------------------------------------
| 4  |     1    | Games    | url_4 |
------------------------------------
| 5  |     1    | Internet | url_5 |
------------------------------------
| 6  |     5    | Browsers | url_6 |
------------------------------------

I would like to see generated tree on the basis of these values. Target shape should look like this:

<ul>
    <li><a href="url_1">Nieuws</a>
        <ul>
            <li><a href="url_1/url_4">Games</a></li>
            <li><a href="url_1/url_5">Internet</a>
                <ul>
                    <li><a href="url_1/url_5/url_6">Browsers</a></li>
                </ul>
            </li>
        </ul>
    </li>
    <li><a href="url_2">Reviews</a></li>
    <li><a href="url_3">Meuk</a></li>
</ul>

It's important for me to have all the slashes inside the tree (full path with all the parents & children).

I would like to add, that I found the code on this page: http://crisp.tweakblogs.net/blog/317/formatting-a-multi-level-menu-using-only-one-query.html but I can not redo it in such a manner as described above. I'll be very grateful for any help, because the application deadline is approaching very fast :(

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

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

发布评论

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

评论(2

表情可笑 2024-12-14 14:11:21

仅一处修改。我觉得这个方法简单多了。我没有使用数组来存储 URL,而是传递一个字符串作为参数(包含基本 url)。

// menu builder function, parentId 0 is the root 
function buildMenu($parentId, $menuData, $urlRoot) 
{ 
    $html = ''; 

    if (isset($menuData['parents'][$parentId])) 
    {
    $html = '<ul>'; 
    foreach ($menuData['parents'][$parentId] as $itemId) 
    { 
        $url   = $urlRoot . $itemId['url'] . '/';
        $html .= '<li><a href="$url">' . $menuData['items'][$itemId]['name'] . '</a>'; 

        // find childitems recursively 
        $html .= buildMenu($itemId, $menuData, $url); 

        $html .= '</li>'; 
    } 
    $html .= '</ul>'; 
    } 

    return $html; 
} 

// output the menu 
echo buildMenu(0, $menuData, '');

Only one modification. I think this way is much simpler. Instead of using an array to store the URL, I pass a string as an argument (containing the base url).

// menu builder function, parentId 0 is the root 
function buildMenu($parentId, $menuData, $urlRoot) 
{ 
    $html = ''; 

    if (isset($menuData['parents'][$parentId])) 
    {
    $html = '<ul>'; 
    foreach ($menuData['parents'][$parentId] as $itemId) 
    { 
        $url   = $urlRoot . $itemId['url'] . '/';
        $html .= '<li><a href="$url">' . $menuData['items'][$itemId]['name'] . '</a>'; 

        // find childitems recursively 
        $html .= buildMenu($itemId, $menuData, $url); 

        $html .= '</li>'; 
    } 
    $html .= '</ul>'; 
    } 

    return $html; 
} 

// output the menu 
echo buildMenu(0, $menuData, '');
天邊彩虹 2024-12-14 14:11:21

根据链接中的函数,如果将 url 添加到查询中,类似这样的操作应该有效:

<?php
// menu builder function, parentId 0 is the root
function buildMenu($parentId, $menuData, $nodes=false)
{
    $html = '';

    if (isset($menuData['parents'][$parentId]))
    {
        $html = '<ul>';
        if($nodes === false){
            $nodes = array();
        } elseif(!is_array($nodes)) {
            $nodes = array($nodes);
        } else {
            $nodes[] = $menuData['items'][$parentId]['url'];
        }
        foreach ($menuData['parents'][$parentId] as $itemId)
        {
            $html .= '<li><a href="/' . implode('/', $nodes) . '">';
            $html .= $menuData['items'][$itemId]['name'] . '</a>';

            // find childitems recursively
            $html .= buildMenu($itemId, $menuData, $nodes);

            $html .= '</li>';
        }
        $html .= '</ul>';
        array_pop($nodes);
    }

    return $html;
}

// output the menu
echo buildMenu(0, $menuData);
?>

based on the function in your link, if you add url to the query, something like this should work:

<?php
// menu builder function, parentId 0 is the root
function buildMenu($parentId, $menuData, $nodes=false)
{
    $html = '';

    if (isset($menuData['parents'][$parentId]))
    {
        $html = '<ul>';
        if($nodes === false){
            $nodes = array();
        } elseif(!is_array($nodes)) {
            $nodes = array($nodes);
        } else {
            $nodes[] = $menuData['items'][$parentId]['url'];
        }
        foreach ($menuData['parents'][$parentId] as $itemId)
        {
            $html .= '<li><a href="/' . implode('/', $nodes) . '">';
            $html .= $menuData['items'][$itemId]['name'] . '</a>';

            // find childitems recursively
            $html .= buildMenu($itemId, $menuData, $nodes);

            $html .= '</li>';
        }
        $html .= '</ul>';
        array_pop($nodes);
    }

    return $html;
}

// output the menu
echo buildMenu(0, $menuData);
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文