Search code examples
phpmysqlcodeigniter-4

Cannot create deep level menu


I'm trying to create a deep level menu, so I have defined in my database this table structure:

id | parent | menu_order
15      0         0
22     15         0
26     15         0
30     15         0
47     22         0
49     22         0
51     22         0
68     26         0
69     26         0

What am I tryng to achieve is generate the code that allow me to correctly set the menu_order column, in the example above the final result should be:

id | parent | menu_order
15     0          0
22     15         1
26     15         5
30     15         8
47     22         2
49     22         3
51     22         4
68     26         6
69     26         7

The result is pretty simple to understand, essentially 22 have as parent 15, so menu_order is 1. The same concept is applied to the nested level of 22, which are (47,49,51).

Actually my code looks like this:

<?php

$posts = [
    ['id' => 15, 'parent' => 0, 'menu_order' => 0],
    ['id' => 22, 'parent' => 15, 'menu_order' => 0],
    ['id' => 26, 'parent' => 15, 'menu_order' => 0],
    ['id' => 30, 'parent' => 15, 'menu_order' => 0],
    ['id' => 47, 'parent' => 22, 'menu_order' => 0],
    ['id' => 49, 'parent' => 22, 'menu_order' => 0],
    ['id' => 51, 'parent' => 22, 'menu_order' => 0],
    ['id' => 68, 'parent' => 26, 'menu_order' => 0],
    ['id' => 69, 'parent' => 26, 'menu_order' => 0],
];

$currOrder = -1;

 foreach ($posts as $key => $p) {

    $currOrder++;

    $test[] = [
        'id'    => $p['id'],
        'parent' => $p['parent'],
        'menu_order' => $currOrder
    ];

    hasChild($p['id'], $currOrder, $test);
}

    
function hasChild($postId, &$currOrder, $test)
{
    // get childs post
    $childs = $this->where('parent', $postId)
        ->get()
        ->getResult();

    foreach ($childs as $c) {

        $currOrder++;
        
        $test[] = [
            'id'    => $c['id'],
            'parent' => $c['parent'],
            'menu_order' => $currOrder
        ];

         // check nested levels
        if ($c->parent != 0) {
           hasChild($c['id'], $currOrder, $test);
        }
    }
}

the problem's that I get duplicated posts for the nested level and the menu_order is comopletely messed up. I guess I'm overcomplicating the logic, could someone help me to achieve this?

Thanks


Solution

  • There are a few ways to handle recursive functions. This is how I do it.

    <?php
    // example code
    
    $posts = [
        ['id' => 15, 'parent' => 0, 'menu_order' => 0],
        ['id' => 22, 'parent' => 15, 'menu_order' => 0],
        ['id' => 26, 'parent' => 15, 'menu_order' => 0],
        ['id' => 30, 'parent' => 15, 'menu_order' => 0],
        ['id' => 47, 'parent' => 22, 'menu_order' => 0],
        ['id' => 49, 'parent' => 22, 'menu_order' => 0],
        ['id' => 51, 'parent' => 22, 'menu_order' => 0],
        ['id' => 68, 'parent' => 26, 'menu_order' => 0],
        ['id' => 69, 'parent' => 26, 'menu_order' => 0],
    ];
    
    
    function recursePosts($pid, $order, $tally, $posts) {
        foreach($posts as $p) {
            if ($p['parent']===$pid) {
                $p['menu_order'] = $tally  ;
                $order['id'.$p['id']] = $p;
                $tally++;
                $o = recursePosts($p['id'], $order, $tally, $posts);  
                $tally = $o[1];
                $order = $o[0];
            }
            
        }
        return [$order, $tally];
    }
    $o =recursePosts(0, [], 0, $posts);
    $order = $o[0];
    ksort($order);
    print_r(array_values($order));
    

    https://www.tehplayground.com/uNY7Bn0VyDv6JIUS

    There is also PHP's native RecursiveIterator