在李的Ul li树结构

时间:2013-12-10 10:42:12

标签: php html-lists

我的代码的树结构在ul li中。现在我有一个小问题..下面是我的阵列

 Array
(
    [5] => Array
        (
            [parent] => 0
            [name] => Kitchen
            [cat_id] => 5
            [slug] => kitchen
            [children] => Array
                (
                    [6] => Array
                        (
                            [parent] => 5
                            [name] => Knife
                            [cat_id] => 6
                            [slug] => knife
                        )

                )

        )

    [6] => Array
        (
            [parent] => 5
            [name] => Knife
            [cat_id] => 6
            [slug] => knife
        )

    [13] => Array
        (
            [parent] => 0
            [name] => Stainless Steel
            [cat_id] => 13
            [slug] => stainless
            [children] => Array
                (
                    [18] => Array
                        (
                            [parent] => 13
                            [name] => Plate
                            [cat_id] => 18
                            [slug] => plate
                        )

                )

        )

    [16] => Array
        (
            [parent] => 0
            [name] => Cloth
            [cat_id] => 16
            [slug] => cloth
        )

    [17] => Array
        (
            [parent] => 0
            [name] => Dinner Set
            [cat_id] => 17
            [slug] => dinner-set
        )

    [18] => Array
        (
            [parent] => 13
            [name] => Plate
            [cat_id] => 18
            [slug] => plate
        )

    [19] => Array
        (
            [parent] => 0
            [name] => Plastic
            [cat_id] => 19
            [slug] => plastic
            [children] => Array
                (
                    [20] => Array
                        (
                            [parent] => 19
                            [name] => Cups
                            [cat_id] => 20
                            [slug] => cups
                        )

                )

        )

    [20] => Array
        (
            [parent] => 19
            [name] => Cups
            [cat_id] => 20
            [slug] => cups
        )

    [26] => Array
        (
            [parent] => 0
            [name] => Items
            [cat_id] => 26
            [slug] => items
        )

)

我的php代码如下:

function toULlistproduct($arr,$shop_url='')
{

   $html = '<ul >'.PHP_EOL;
   foreach ($arr as $v)
   {

              $html .= '<li><a href="'.site_url().$shop_url.'/category/'.$v['slug'].'" id="' . $v['cat_id'] . '"  >' . $v['name'].'</a>' ;

              if (array_key_exists('children', $v))
                  $html .= toULlistproduct($v['children'],$v['cat_id'],$shop_url);
              $html .= '</li>'.PHP_EOL;

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

   return $html;
  }

我的HTML将如下所示:

<ul>
<li><a id="5" href="http://localhost/myproject/category/kitchen">Kitchen</a><ul>
<li><a id="6" href="http://localhost/myproject/category/knife">Knife</a></li>
</ul>
</li>
<li><a id="6" href="http://localhost/myproject/category/knife">Knife</a></li>
<li><a id="13" href="http://localhost/myproject/category/stainless">Stainless Steel</a><ul>
<li><a id="18" href="http://localhost/myproject/category/plate">Plate</a></li>
</ul>
</li>
<li><a id="16" href="http://localhost/myproject/category/cloth">Cloth</a></li>
<li><a id="17" href="http://localhost/myproject/category/dinner-set">Dinner Set</a></li>
<li><a id="18" href="http://localhost/myproject/category/plate">Plate</a></li>
<li><a id="19" href="http://localhost/myproject/category/plastic">Plastic</a><ul>
<li><a id="20" href="http://localhost/myproject/category/cups">Cups</a></li>
</ul>
</li>
<li><a id="20" href="http://localhost/myproject/category/cups">Cups</a></li>
<li><a id="26" href="http://localhost/myproject/category/items">Items</a></li>
</ul>

enter image description here

现在我想显示一个href是我的问题:即

我只想显示如下的href网址:

    <li><a id="6" href="http://localhost/myproject**/category/knife**">Knife</a></li>

this should be display like below 

     <li><a id="6" href="http://localhost/myproject/**category/kitchen/knife**">Knife</a></li>

我只想修改一个href url

2 个答案:

答案 0 :(得分:1)

我认为你的方式是99%。

function toULlistproduct($arr,$shop_url=false)
{
   $shop_url = $shop_url ?: site_url() . '/category/';

   $html = '<ul >'.PHP_EOL;
   foreach ($arr as $v)
   {

              $html .= "<li><a href=\"{$shop_url}/{$v['slug']}\" id=\"{$v['cat_id']}\">{$v['name']}</a>";

              if (array_key_exists('children', $v))
                  $html .= toULlistproduct($v['children'], $v['cat_id'], "{$shop_url}/{$v['slug']}");
              $html .= '</li>'.PHP_EOL;

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

   return $html;
  }

这就是你要追求的吗?

基本上,当你深入到树中时,你需要继续将父类别的slug附加到$shop_url

答案 1 :(得分:0)

 function toULlistproduct($arr)
 {
     $html = '<ul>'.PHP_EOL;
     foreach ($arr as $k => $v)
     {          
         if(is_array($v)){          
              $html .= '<li><a href="#" id="' . $k . '"  >'.$k.'</a></li>' ;
              $html .= toULlistproduct($v);
         }else{
              $html .= '<li><a href="#" id="' . $k . '"  >'.$v.'</a></li>' ;
         }
    }
    $html .= '</ul>'.PHP_EOL;
    return $html;
}
$arr = array(
    'a' => array(1,2,3,4,5),
    'b' => array(9,8,7,6),
    'c' => 10
);
echo toULlistproduct($arr);

使用示例递归显示菜单项的简单功能。