charlie
charlie

Reputation: 481

Creating CSS menu using MySQL Data

I have this menu structure:

<li class="active"><a href="#">Link <span class="sr-only">(current)</span></a></li>
<li><a href="#">Link</a></li>
<li class="dropdown">
    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <span class="caret"></span></a>
    <ul class="dropdown-menu" role="menu">
        <li><a href="#">Action</a></li>
        <li><a href="#">Another action</a></li>
        <li><a href="#">Something else here</a></li>
        <li><a href="#">One more separated link</a></li>
    </ul>
</li>

I want to be able to generate it from my current table structure:

sequence
page_title
page_url
parent

In the above structure, parent will equal sequence so for all top levels, my query would include WHERE parent = '' then all child items, the query would include: WHERE parent = '[SEQUENCE]'.

Upvotes: 0

Views: 33

Answers (1)

ale8oneboy
ale8oneboy

Reputation: 127

If I'm understanding the question correctly. You're looking to sort your menu items by parent, followed by child menu items.

For the parent items, you could make the parent column equal itself.

Ex.

id | sequence | page_title | page_url | parent
----------------------------------------------
1  | 1        | parent     | url      | 1
3  | 3        | child 2    | url      | 1
2  | 2        | child 1    | url      | 1

The query to sort the results would be:

SELECT * FROM menu_items ORDER BY parent, sequence

In your code to create the parent/child menu, you could check to see if the parent = id to determine if the item is a parent item. This help?

Upvotes: 1

Related Questions