1

Been trying since yesterday and today finaly am close to achieving it.

The Problem

My PHP scripts hangs up, what I means is that it seems that it loops infinite which causing the script to hang up. In the database theirs only 11 records.

This function loops through the array of data that was passed in the function from generate_module and generates the menu and submenu

function module_keys($array = array(), $modules_parent = 0){
    echo '<ul class="nav navbar-nav side-nav">';
    foreach($array as $item){
        echo '<li>';
        echo '<a>';
        echo $item->modules_name;
        echo '</a>';
        module_keys($array, $item->modules_id);
        echo '</li>';
    }
    echo '</ul>';
}

This function queries the database and stores the results in the array which is then passed to module_keys to generate the menu and submenu.

function generate_module(){
    global $db;
    $array = array();
    $sql = "SELECT * FROM module";
    $query = $db->SELECT($sql);
    $array = $db->ROWS();
    module_keys($array, $modules_parent = 0);
}

Here is the database structure

enter image description here

1 Answer 1

1

The problem lies here:

    echo '</a>';
    module_keys($array, $item->modules_id);
    echo '</li>';
}

The second call to modules array is passing the original array into the module_keys function which is causing it to loop forever. where in the array are your sub menu items stored because you will need to pass that into the second function call.

What you really need to do is get each of the child menu items and assign them to the original array.

function generate_module(){
    global $db;
    $array = array();
    $sql = "SELECT * FROM module";
    $query = $db->SELECT($sql);
    $array = $db->ROWS();

    //iterate over the menu items and get each of the child menu items
    //add them into the original array then pass that
    $newArray = array();
    foreach($array as $menuItem) {
        $submenu = //some additional query to collect the data
        $temp = array();
        $temp = $menuItem;
        $temp['submenu'] = $submenu;
    }

    module_keys($newArray, $modules_parent = 0);
}

Then you need to make sure that the function call in module_keys passes the sub-array elements so the sub menu can be constructed.

Like:

module_keys($item['submenu'], $item->modules_id);

I hope this helps

Note I have used arrays but it appears you are using objects, you may need to convert what i suggest into an object so it will appear the same as what you are doing

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.