Create PHP Infinite Menu

Asked

Viewed 153 times

2

I’m trying to create an infinite menu, I’ve assembled the object the way I need to, but I can’t print it. It must be something like this:

User
User > User1
User > User2
User > User2 > User3
User > User2 > User4
User > User3
User > User3 > User5
.....

My Object:

[
(int) 0 => object(App\Model\Entity\User) {

    'id' => (int) 1,
    'nome' => 'Administrador',
    'parent_id' => null,
    'children' => [
        (int) 0 => object(App\Model\Entity\User) {

            'id' => (int) 2,
            'nome' => 'Revenda Teste 1',
            'parent_id' => (int) 1,
            'children' => []
        },
        (int) 1 => object(App\Model\Entity\User) {

            'id' => (int) 3,
            'nome' => 'Revenda Teste 2',
            'parent_id' => (int) 1,
            'children' => []
        },
        (int) 2 => object(App\Model\Entity\User) {

            'id' => (int) 6,
            'nome' => 'Revenda Teste 3',
            'parent_id' => (int) 1,
            'children' => [],

        },
        (int) 3 => object(App\Model\Entity\User) {

            'id' => (int) 7,
            'nome' => 'Revenda Teste 4',
            'parent_id' => (int) 1,
            'children' => [
                (int) 0 => object(App\Model\Entity\User) {

                    'id' => (int) 8,
                    'nome' => 'Revendedor Teste 5',
                    'parent_id' => (int) 7,
                    'children' => [
                        (int) 0 => object(App\Model\Entity\User) {

                            'id' => (int) 15,
                            'nome' => 'Revendedor do Rev 5',
                            'parent_id' => (int) 8,
                            'children' => [],

                },
                (int) 1 => object(App\Model\Entity\User) {

                    'id' => (int) 9,
                    'nome' => 'Revenda Teste 6',
                    'parent_id' => (int) 7,
                    'children' => [
                        (int) 0 => object(App\Model\Entity\User) {

                            'id' => (int) 14,
                            'nome' => 'Revenda Teste 7',
                            'parent_id' => (int) 9,
                            'children' => []
                        }
                    ]

                }
            ]

        }
    ]

}
]

My code:

public function menuList($lists) {
    $n = "";
    if(!is_array($lists)){
        $lists = $lists->toArray();
    }
    echo "<br>";
    foreach($lists as $element) {
        echo $n .=  $element->nome . " > ";
        if($element->children){
            $this->menuList($element->children);
        }
    }
 }

My way out:

Administrador >
Revenda Teste 1 > Revenda Teste 1 > Revenda Teste 2 > Revenda Teste 1 >
Revenda Teste 2 > Revenda Teste 3 > Revenda Teste 1 > Revenda Teste 2 >
Revenda Teste 3 > Revenda Teste 4 > 
Revendedor Teste 5 >
Revendedor do Rev 5 > Revendedor Teste 5 > Revenda Teste 6 >
Revenda Teste 7 >
  • And how did you try to do it? You know what recursion is?

  • yes, I tried with recursiveness, but I could not reach the intended result, I know that this is the way... but I do not know how to reach.

  • Ask the question your attempt and the result you got.

  • ready... added the code and output.

1 answer

2


Try it this way:

public function menuList($lists, $n = "") {
    if(!is_array($lists)){
        $lists = $lists->toArray();
    }
    for($x = 0; $x < count($lists); $x++){
        $element = $lists[$x];
       echo $n.$element->nome . "<br>";
        if($element->children){
            $n .= $element->nome . " > ";
            $this->menuList($element->children, $n);
            $count = strlen($element->nome . " > ");
            $n = substr($n, 0, -$count);
        } 
    }
 }
  • Just one observation. I ended up using for because I was doing otherwise. But I can also use foreach.

  • The output was not as expected, in addition to repeating the values, was inline, in had arrived at something similar, but its was closer, I believe that it lacks some simple detail....

  • @Laercionunes sorry... I made an edit. When I went to cast for your code I forgot to change a part. Take a look now.

  • 1

    perfect is the word... I stay exactly as I need. Thank you very much!!!!

  • @Calm Laercionunes. Do not forget to evaluate the answer. = ) Hug!

  • I evaluated, but as my reputation is still low, the following message appeared: Thanks for the feedback! Votes cast by those with Less than 15 Reputation are Recorded, but do not change the publicly displayed post score

  • It was! Thanks, man!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.