Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter Tree Menu
#1

[eluser]neillyons.co[/eluser]
CodeIgniter Tree Menu

Using CodeIgniter's ul function ( line 61 ../system/helpers/html_helper.php ) to render the output for the menu, I need to generate and format an array to pass as the first parameter, can anyone help?

Each menu item has a parent, which is the id of the parent menu item. If a menu has no parent selected it will default to 0. Zero is the root.


Database schema:

Code:
menu
id, name, parent, weight, created, updated


Format of the array to pass:

Code:
$data["Animal"]["Mammal"][] = "Cat";
$data["Animal"]["Mammal"]["Dog"][] = "Terrier";
$data["Animal"]["Mammal"]["Dog"][] = "Sheep dog";
$data["Animal"]["Bird"]["Owl"][] = "Barn Owl";

The above array when passed to ul function would produce the following HTML.

Code:
<ul>
  <li>Animal
    <ul>
      <li>Mammal
        <ul>

          <li>Cat</li>
          <li>Dog
            <ul>
              <li>Terrier</li>
              <li>Sheep dog</li>
            </ul>
          </li>
        </ul>

      </li>
      <li>Bird
        <ul>
          <li>Owl
            <ul>
              <li>Barn Owl</li>
            </ul>
          </li>
        </ul>

      </li>
    </ul>
  </li>
</ul>


I need to dynamicly produce this array from the information in the database.

Let me know if you can help.
#2

[eluser]trice22[/eluser]
Without throwing you a complete piece of code (which I wouldn't even have atm.) a quick hint: "Recursion" will allow you to climb the tree up, until you reach the end for each branch (parent == 0, e.g.).
Google and the search in here are your friend and I'm happy to help out where I can as well.

Besides—I believe I've seen a navigation helper, which did something similar, around. You might want to have a look in the wiki as well.

Good luck,
trice

P.S.: @Admin: Would it be possible to rename this forum? 20%–50% of the stuff that comes in (via my feed reader) has absolutely nothing to do with "Ignited Code" and I'd blame the name. Mostly Big Grin .




Theme © iAndrew 2016 - Forum software by © MyBB