Eds
Eds

Reputation: 575

creating menu structure from mysql database

I'm just wondering on peoples opinions here, and perhaps some guidance on how best to implement something.

What I have, is a very basic intranet, simply designed to serve up company wide documents and files. It has a basic menu bar across the top of the page, that has a top level item, and then sub menus/items. Obviously the menu can cascade down many levels, and is a simple javascript I got of dynamic drive, that turns an html < li > structure into the menu.

Basic idea is this:

<li><a href='#'>Top Level 1</a>
    <ul>
        <li><a href='#'>Item 1</a></li>
        <li><a href='#'>Item 2</a></li>
        <li><a href='#'>Sub menu 1</a>
            <ul>
                <li><a href='#'>Sub Item 1</a></li>
            </ul>
        </li>
    </ul>
</li>

So this is used to create the menu. Every time you start a new < ul > element, it will create a sub menu.

This works fine, but is a bit of a pain as I have to manually go in an create new links, delete old ones etc. everytime there is a change in documents on the site.

What I am looking to do, is move this structure into my database, so that I can use basic html forms, and PHP scripts to insert, update and delete links when needed.

It also gives managers the ability to upload new files if I am not available to do so.

I have had a quick play with this, and have found an option that kind of works, but is a bit clunky and could be a pain to maintain going forward.

Basically, I would have a table with the columns listed below:

  • Doc ID
  • Filename
  • File path
  • Menu
  • Sub 1
  • Sub 2
  • Sub 3
  • Label
  • Open In
  • What I can do, is to have a unique ID for reference, input the filename and file path that together can be used for the href link to point to the files location. I can then use the menu and 3 sub menu fields to tell it where to place that item in the menu structure. The label just gives a name to the link, and the open in tells the page where to open the document, i.e. _new, frame, ifram etc.

    The PHP can then use a mysql query to find all items in a menu and echo them out. The problem comes that I would need a seperate MySQL query for each menu and sub menu that I have, meaning that the entire menu may well end up being full of MySQL queries.

    Can anyone advise on a better solution to this, or help make the PHP as simple and flexible as possible?

    Many thanks

    Eds

    Edit:

    Can provide some examples of PHP if required, but they are quite large.

    Upvotes: 0

    Views: 5759

    Answers (2)

    Jim Garbe
    Jim Garbe

    Reputation: 1

    I developed this "Git" menu because I couldn't find anything MySQL driven either. https://github.com/jgarbe/DDrop_Menu I hope someone can use it!

    Upvotes: 0

    Anton
    Anton

    Reputation: 1061

    best practise for this as i know is nested tree structure, which logic is in two words:

    table required fields: id, parent_id, left, right

    then, in adding first row, you got this values

    1, 0, 1, 2
    

    next will improve left and right fields like:

    2, 0, 3, 4
    3, 0, 5, 6
    

    and if we need to make subdirectory for first item, the full structure will be this:

    1, 0, 1, 4
    2, 0, 5, 6
    3, 0, 7, 8
    4, 1, 2, 3
    

    so you can easily take all the tree ordered by left, and then just check every row parent level value, if parrent exists, to calculate its own level.

    Upvotes: 2

    Related Questions