Raheel
Raheel

Reputation: 9024

Creating menu with sub menus from two arrays in PHP

    Array
    (
        [0] => stdClass Object
            (
                [id] => 1
                [name] => Tops
                [parent_id] => 
            )

        [1] => stdClass Object
            (
                [id] => 2
                [name] => Trousers
                [parent_id] => 
            )

        [2] => stdClass Object
            (
                [id] => 3
                [name] => Dresses
                [parent_id] => 
            )

        [3] => stdClass Object
            (
                [id] => 4
                [name] => Skirts
                [parent_id] => 
            )

        [4] => stdClass Object
            (
                [id] => 5
                [name] => Accessories
                [parent_id] => 
            )

        [5] => stdClass Object
            (
                [id] => 6
                [name] => Coats & Jackets
                [parent_id] => 
            )

)

Above is the parent categories i fetch from db.

And below are the sub categories

 Array
(
    [0] => stdClass Object
        (
            [id] => 8
            [name] => Mini Tops
            [parent_id] => 1
        )

    [1] => stdClass Object
        (
            [id] => 9
            [name] => Long Tops
            [parent_id] => 2
        )

    [2] => stdClass Object
        (
            [id] => 10
            [name] => Cargo
            [parent_id] => 2
        )

    [3] => stdClass Object
        (
            [id] => 11
            [name] => Bermuda
            [parent_id] => 2
        )

    [4] => stdClass Object
        (
            [id] => 12
            [name] => Formal Dresses
            [parent_id] => 3
        )

    [5] => stdClass Object
        (
            [id] => 13
            [name] => Casual Dresses
            [parent_id] => 3
        )

    [6] => stdClass Object
        (
            [id] => 14
            [name] => Party Wear
            [parent_id] => 3
        )

    [7] => stdClass Object
        (
            [id] => 15
            [name] => Jewelry 
            [parent_id] => 4
        )

    [8] => stdClass Object
        (
            [id] => 16
            [name] => Bracelets
            [parent_id] => 4
        )

    [9] => stdClass Object
        (
            [id] => 17
            [name] => Caps
            [parent_id] => 4
        )

)

below is how i am making my navitaion bar

             <ul>
                <li>
                  <a href="search.html">Tops</a>
                  <ul class="sub-menu">
                        <li><a href="search.html">Tshirts</a></li>
                        <li><a href="search.html">Jumpers</a></li>
                        <li><a href="search.html">Cardigans</a></li>
                        <li><a href="search.html">Knitwear</a></li>
                    </ul>
                </li>
              </ul>

As my logics are not so much good i actually trying to make navitaion with these two arrays in a way that the first array which are main links should be on top order where as all respective childs will appear in submenu of there respective parent_id example

Long Top, Cargo and Bermuda should be falling under Trousers Link.

I need guidance how can i achieve this .. I tried foreach loop but stucked in middle

Thanks alot

Upvotes: 0

Views: 254

Answers (1)

C. E.
C. E.

Reputation: 10607

If you're in control you should try to use a different choice of arrays because now you will be doing an unnecessary amount of looping. To demonstrate:

foreach($categories as $cat) {
    echo "<li>".$cat->name."<ul>";
    foreach($subcategories as $subcat) {
        if($subcat->parent_id == $cat->id) echo "<li>".$subcat->name."</li>"
    }
    echo "</ul></li>";
}

You have to loop over all subcategories for each category. But if you instead stored subcategories in an array where the index represented the parent ID, you could easily retrieve them.

If you want to solve this with just one array you could make the list of subcategories a property of the corresponding parent category. This would be easy to handle and semantic.

Upvotes: 1

Related Questions