PHP多维数组到无序列表,建立URL路径 [英] PHP Multidimensional array to unordered list, building up url path

查看:83
本文介绍了PHP多维数组到无序列表,建立URL路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由icio和ftrotter的出色示例制作的PHP多维数组(我在数组变量中使用ftrotterrs数组):

I have a multidimensional array in PHP produced by the great examples of icio and ftrotter (I am use ftrotterrs array in arrays variant):

将数据库结果转换为数组

通过这种方法,我将其设置为无序列表宽度:

I have made this into a unordered list width this method:

public function outputCategories($categories, $startingLevel = 0)
{
  echo "<ul>\n";
  foreach ($categories as $key => $category)
  {
    if (count($category['children']) > 0)
    {
      echo "<li>{$category['menu_nl']}\n";
      $this->outputCategories($category['children'], $link
                              , $start, $startingLevel+1);
      echo "</li>\n";
    }
    else
    {
      echo "<li>{$category['menu_nl']}</li>\n";
    }
  }
  echo "</ul>\n";
}

到目前为止一切都很好.

So far so good.

现在,我想使用url_nl字段来构建用作菜单中链接的URL. url必须通过在树中向下的每个步骤加/url_nl来反映树中链接的位置.

Now I want to use the url_nl field to build up the url's used as links in the menu. The url has to reflect the dept of the link in de tree by adding up /url_nl for every step it go's down in the tree.

我的目标:

- item 1 (has link: /item_1)
    * subitem 1 (has link: /item_1/subitem_1)
    * subitem 2 (has link: /item_1/subitem_1)
        * subsubitem 1 (has link: /item_1/subitem_2/subsubitem_1)
- item 2 (has link: /item_2)

桌子

id
id1 (parent id)
menu_nl
url_nl
title_nl
etc

到目前为止我所拥有的:

What I have so far:

public function outputCategories($categories, $link, $start, $startingLevel = 0)
{
  // if start not exists
  if(!$start)
    $start = $startingLevel;

  echo "<ul>\n";
  foreach ($categories as $key => $category)
  {
    $link.= "/".$category['url_nl'];

    if($start != $startingLevel)
      $link = strrchr($link, '/');

    if (count($category['children']) > 0)
    {
      echo "<li>".$start." - ".$startingLevel.
           "<a href='$link'>{$category['menu_nl']}</a> ($link)\n";
      $this->outputCategories($category['children'], $link
                              , $start, $startingLevel+1);
      echo "</li>\n";
    }
    else
    {
      $start = $startingLevel+1;
      echo "<li>".$start." - ".$startingLevel.
           "<a href='$link'>{$category['menu_nl']}</a> ($link)</li>\n";
    }
  }
  echo "</ul>\n";
}

如您在示例中看到的,我使用了url_nl字段,该字段是递归添加的,因此列表的每个级别都有一个链接,该链接带有用作url的路径.

As you see in the example I have used a url_nl field which is recursively added so every level of the list has a link with a path which is used as a url.

无论如何,我在建立这些链接时遇到问题,因为在循环到层次结构列表时无法正确重置它们.进入de list中的孩子之后,第一个是正确的,但是第二个不是.

Anyhow, I have problems with building up these links, as they are not properly reset while looping to the hierarchical list. After going down to the child in de list the first one is right but the second one not.

我被困在这里...

推荐答案

似乎您在foreach循环中修改了$ link变量,因此将item1添加到$ link,循环通过其子项并返回到第一个迭代,将item2添加到变量中...

It looks like you modify the $link variable inside the foreach loop, So you add item1 to $link, loop thru its subitems and return to the first iteration and add item2 to the variable...

替换此

$link   .= "/".$category['url_nl']; 

$insidelink   = $link . "/".$category['url_nl']; 

(并将循环内剩余的$ link更改为$ insidelink)

(and change remaining $link inside the loop to $insidelink)

添加:$ startingLevel也是如此.不要修改它,请使用+1内联:

Adding: This is also true for $startingLevel. Do not modify it, use +1 inline:

echo "<li>".$start." - ".$startingLevel +1.
    "<a href='$link'>{$category['menu_nl']}</a> ($link)</li>\n";

这篇关于PHP多维数组到无序列表,建立URL路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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