Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jul 2009
    Posts
    23
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Exclamation help build a dynamic parent-child-subchild menu in php

    Hello, I would like to build a menu system in php only, without any jquery or ajax, where the menus will be read from a table in my db and populated in different divs when loaded.
    The primary menus will be loaded in a "primary-block", and if a primary menu is activated and has a child menu, then the child must be loaded in a block "child-block". If the child has a url link, then it opens that link, else if it has a child of its own, then it must load the latter in the "subchild-block".
    Attached are 2 files, one for the table structure, and one with a sample parent-child-subchild data, where the first column is the menuid, then the menu name, a sorting value to display the order, and the column before last being the one which keeps track of the parent menuid.

    How can I proceed with this in an MVC fashion?
    Attached Thumbnails Attached Thumbnails help build a dynamic parent-child-subchild menu in php-menutable.jpg   help build a dynamic parent-child-subchild menu in php-parentchild.jpg  

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,543
    Thanks
    80
    Thanked 4,617 Times in 4,581 Posts
    The database structure you have chosen does not lend itself well to doing this kind of thing with MySQL (or any SQL for that matter).

    So you'd probably be well off to think about doing this all in JavaScript. Just dump the table, pretty much as is, into a JS array of objects. And then let JS build the "tree" of parents/kids.

    Yes, you could do it in PHP, but since you will want to use JS to do the show/hide blocks you might as well do it all in JS.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Jul 2009
    Posts
    23
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Thumbs up

    Quote Originally Posted by Old Pedant View Post
    The database structure you have chosen does not lend itself well to doing this kind of thing with MySQL (or any SQL for that matter).

    So you'd probably be well off to think about doing this all in JavaScript. Just dump the table, pretty much as is, into a JS array of objects. And then let JS build the "tree" of parents/kids.

    Yes, you could do it in PHP, but since you will want to use JS to do the show/hide blocks you might as well do it all in JS.
    Thanks for the info, in fact I have built one in jquery and ajax using the tables and it is working, however the person handling the project does not want it this way, and wanted only php to do the menus... The tables etc come from a custom made cms, done by another person, who is no longer maintaining the system while am using it only to dev websites from it

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,543
    Thanks
    80
    Thanked 4,617 Times in 4,581 Posts
    Well, then, use the JavaScript code that you have working as a model for the PHP code.

    The easiest way to do this, in my opinion, is to read the entire set of records into an array and then build the nested levels from that array, using a recursive function. Probably very similar to how you did it in JS coding.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,543
    Thanks
    80
    Thanked 4,617 Times in 4,581 Posts
    By the by, the person in charge of the project is clearly an idiot. If you didn't need to use JS to hide/show the blocks, it would make sense to avoid JS for building them. Then the page works even for people who don't enable JS. But given that the page won't work at all with JS disabled, why not build the blocks in JS?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •