I build a static website (no CMS), DooMeHome, but would like to make the menu dynamic. I also set up a DB with the following structure:
I created a database with the following setup:
Some more detail would be good. For example, are all of these individual pages, or only those where the “parent” is zero? Are you needing a hierarchical menu, or just a single level? By that I mean
Page 2, Services, for example, would you show only those menu entries where “parent” = 2, or would it also iterate to show those entries where parent=2, and then for each of those, show a sub-menu where parent=6, 7, 8, to 13, and then keep going for “x” levels?
I’d suggest the first thing to do is go for the simple single-level menu and get that working. Then expand on your PHP learning to get sub-levels working if you need to. Some would say that using Ajax to dynamically recover the second and subsequent menu levels would be a good choice, but that’s for later.
Some pseudo-code
query = "SELECT menu, link FROM yourtable WHERE parent = pagevalue ORDER BY child"
execute query
for each row returned {
display menu and link
}
That’s pretty much it, if I’ve understood the question. Once you have that working, you can add deeper levels of nesting if you need to. The ORDER BY clause will sort the results in the order you specify.
I am in the process of creating a new menu system which includes a dynamic menu
I want to replace an old menu and add some SVG diagonals, triangles, etc.
Currently the menu under construction uses links instead of being driven from a database. Once complete I should be able to call one single “home.php” page and pass database variables.
This topic has some simple examples of menus from php. I gave an example using an array instead of a database to store item info.
Though I’m currently working on refactoring a site (from procedural to oop) where I have changed the menu to take item data from a database table of pages on the site. So the menu part of that uses a couple of classes, menu and menuitem and a couple of methods to build the menus and output them.
If you take that route, how that happens will depend on your intended menu structure.
Thank you for the hint, appreciated.
However, I can’t just change the site layout because I am not good enough to create a hierarchical menu using PHP.
Then I keep it static and change every single page’s header/menu with every change in pages (e.g. adding a new page).
The most simple way to get around this if you are not a confident php programmer is to simply have a stand-alone, static html menu saved in a file which is an include in your pages/page-template. This won’t be so “dynamic” but it’s simple for a beginner and will save yo making multiple edits.
Taking it to the next step is using an array. This can be useful, because with a nested hierarchy of menus like you have, you can define that structure as a multi-dimensional array.
You will then use foreach loops, nested to render the html lists.
To edit the menu (add/remove/re-name pages) you only need edit the array.
Then the next level is using a database. Then it does get a bit more complex as you need a way of defining the hierarchy and then constructing the menu in that way.
I would say the array method may be easier and more efficient. So I would only use a database if I was going to also build/use some kind of admin interface to manage the entries via forms, like a mini CMS, rather than manually editing code. Otherwise I would see it as an over-complication.
Take a look at this dynamic menu which is the top part with the royal blue background. It is common to every page and accepts the $title, top $group links and sub $page links. Once modularized (spelling?) it will be populated from a database table.