2013-12-10 66 views
0

我在ul裏有我的代碼的樹結構。現在我有一個小問題,奏響了..下面是我的數組在UI中的UI 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網址只

+0

我是否正確理解唯一的問題是您的鏈接不包含父類別? – rdrkt

+0

是的..我需要添加家長catgories slug到我的鏈接 – Aaru

回答

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; 
    } 

這就是你所追求的?

基本上,您需要繼續將父類別的子彈附加到$shop_url,因爲您在樹的深處進行遞歸處理。

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); 

簡單的功能與遞歸例子顯示的菜單項。