0

I outputted the following array to the equivalent JavaScript JSON variable, and I tried to create a dynamic based navigation bar.

$navArray = array(
    array('id'=>1,'parent'=>0,'text'=>'1A','href'=>'1a'),
    array('id'=>2,'parent'=>1,'text'=>'2B','href'=>'2b'),
    array('id'=>3,'parent'=>1,'text'=>'3C','href'=>'3c'),
    array('id'=>4,'parent'=>2,'text'=>'4D','href'=>'4d'),
    array('id'=>5,'parent'=>2,'text'=>'5E','href'=>'5e'),
    array('id'=>6,'parent'=>5,'text'=>'6F','href'=>'6f'),
    array('id'=>7,'parent'=>5,'text'=>'7G','href'=>'7g'),
    array('id'=>8,'parent'=>3,'text'=>'8H','href'=>'8h'),
);

The script (JavaScript/jQuery) should get the array and returns HTML based on the parent 'id' as follows:

<ul>
    <li>
        <a href="1a">1A</a>
        <ul>
            <li>
                <a href="2b">2B</a>
                <ul>
                    <li>
                        <a href="4d">4D</a>
                    </li>
                    <li>
                        <a href="/סוכרת">5E</a>
                        <ul>
                            <li>
                                <a href="6f">6F</a>
                            </li>
                            <li>
                                <a href="7g">7G</a>
                            </li>
                        </ul>
                    </li>
                </ul>
            </li>
            <li>
                <a href="3c">3C</a>
                <ul>
                    <li>
                        <a href="8h">8H</a>
                    </li>
                </ul>
            </li>
        </ul>
    </li>
<ul>

The HTML screen result should look like this:

enter image description here

I tried doing something... but that didn't work.

$('li.dropdown').hover(
    function(){
        $(this).find('ul').slideDown();
    },
    function(){
        $(this).find('ul').slideUp();
    });

How do you dynamically create a multi dimensional level HTML navigation bar as described?

2
  • what has the php todo with this question? Commented Jul 11, 2017 at 14:43
  • I mean what you tried is wrong...I mean SOOOOO wrong. I mean if that code was designed to (somehow) boil a kettle it actually generate a flock of ducks and teach them to dance the can can....really that wrong. Tl;Dr delete your "code" read some more and start again Commented Jul 11, 2017 at 14:48

1 Answer 1

1

Sounds like a classic case for recursion, can you change the array objects to include their own children?

$navArray = array(
    array('id'=>1,'text'=>'1A','href'=>'1a', 'children' => array(
      array('id'=>2,'text'=>'2B','href'=>'2b', 'children' => array(
        array('id'=>4,'text'=>'4D','href'=>'4d'),
        array('id'=>5,'text'=>'5E','href'=>'5e', 'children' => array(
          array('id'=>6,'text'=>'6F','href'=>'6f'),
          array('id'=>7,'text'=>'7G','href'=>'7g'),
        )),
      )),
      array('id'=>3,'text'=>'3C','href'=>'3c', 'children' => array(
        array('id'=>8,'text'=>'8H','href'=>'8h')
      )),
    )),
);

Then if you output your array into JS (let arr = <?=json_encode($navArray)?>), you can feed it into your function which will run recursively through the children, something similar to this should work:

function createMenuItems(arr) {
  let output = '<ul>'
  for (let item of arr) {
    output += `<li><a id="${item.id}" href="${item.href}">${item.text}</a></li>`
    if (item.children && item.children.length) {
      output += createMenuItems(item.children);
    }
  }
  output += '</ul>'
  return output
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.