Aaru
Aaru

Reputation: 813

Ul li tree structure in php

I have the tree structure of my code in ul li. now i'm struck up with one small issue.. below is my array

 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
        )

)

And my php code is below :

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

My HTML will look like this :

<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

Now i want to display a href is my problem : ie

I just want to display the href url like below :

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

I just want to modify the a href url only

Upvotes: 0

Views: 1663

Answers (2)

KumarA
KumarA

Reputation: 1378

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

Simple function to display menu items recursively with example.

Upvotes: 0

rdrkt
rdrkt

Reputation: 138

I think you're 99% of the way there.

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

Is that what you're after?

Basically, you need to keep appending the parent category's slug to the $shop_url as you recurse deeper into the tree.

Upvotes: 1

Related Questions