Below are 2 functions that will handle the Query and rending of the menu from MySQL Database.

This function Loops through the array. Takes in 2 arguments.

    function loop_section($array, $parent_id = 0){
        echo '<ul>';
            foreach($array as $section){
                echo '<li>';
                echo $section->section_name;
                echo '</li>';
            }
        echo '</ul>';
    }

This function handles the Querying of the menu from the table section in the database.

function fetch_sections(){
        global $db;
        $array = array();
        $query = $db->SELECT("SELECT * FROM sections");
        $array = $db->ROWS();
        loop_section($array);
    } 

At the moment when I run this, I get all the menu as the same, I mean all of them renders as first level format. What am I missing over here?

Well you will do as you are selecting all records and echoing them out through one loop all in the same unordered list. What exactly are you trying to do?

Check what the $array content is. Is it an array of objetcs (this is how you reffer to each row)?

To check the contents of the array insert this temporary debug code after line 5 in the second code snippet:

die(print_r($array, 1));

This will display the contents of the $array variable and stop the script. Now you can inspect the elements displayed and try to debug it. You can also paste the output here for us to see.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.