0

I have an array having unique 'table_id' that will find 'parent_id' and goes under parent index.
My Array:

Array
(
    [0] => Array
        (
            [table_id] => 7
            [table_name] => Macro
            [parent_table] => 1
        )

    [1] => Array
        (
            [table_id] => 4
            [table_name] => Dise
            [parent_table] => 7
        )

    [2] => Array
        (
            [table_id] => 5
            [table_name] => Cox
            [parent_table] => 7
        )

    [3] => Array
        (
            [table_id] => 6
            [table_name] => Ripo
            [parent_table] => 4
        )

)

Sample output:

Array
(
    [1] => Array
        (
            [table_id] => 7
            [table_name] => Macro
            [parent_table] => 1
            [7] => Array
                (
                    [table_id] => 4
                    [table_name] => Dise
                    [parent_table] => 7
                    [4] => Array
                        (
                            [table_id] => 6
                            [table_name] => Ripo
                            [parent_table] => 4
                        )

                )

            [7] => Array
                (
                    [table_id] => 5
                    [table_name] => Cox
                    [parent_table] => 7
                )

        )

)

Please give some idea to build this.

2
Contributors
1
Reply
2
Views
4 Years
Discussion Span
Last Post by Bachu
0

Try this;

$menuList=array();
        $parentId = 0;
        $result = mysql_query("SELECT * FROM    db_Table  WHERE (parent_table = {$parentId}) ;");
        $mainMenus = mysql_fetch_array($result);
        foreach($mainMenus as $mm) {
            array_push($menuList, $mm);

            $result = mysql_query("SELECT * FROM     db_Table  WHERE  parent_table = {$mm->table_id};");
            $subMenu = mysql_fetch_array($result);

            if(sizeof($subMenu)>0) {
                array_push($menuList, $subMenu);
            }
        }
        print_r($menuList);

This is a basic code. inside it check, if any row with parent id equal to parnt id .. repeate this query

Edited by Bachu

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.