title: Tree menu using PHP and XML
body: <p>I am trying to make a tree menu by using PHP and XML.</p>
  
  <pre><code>    &lt;MARKET&gt;
        &lt;weapons&gt;
          &lt;class title="Bagi Warrior" div="bagi"&gt;
            &lt;weapon name="Gauntlet" div="gauntlet"&gt;
            &lt;/weapon&gt;
          &lt;/class&gt;
          &lt;class title="Segita Hunter" div="hunter"&gt;
            &lt;weapon name="Bow" div="bow"&gt;
            &lt;/weapon&gt;
            &lt;weapon name="Crossbow" div="xbow"&gt;
            &lt;/weapon&gt;
            &lt;weapon name="Dagger" div="dagger"&gt;
            &lt;/weapon&gt;
          &lt;/class&gt;
          &lt;class title="Incar Magician" div="mage"&gt;
            &lt;weapon name="Wand" div="wand"&gt;
            &lt;/weapon&gt;
            &lt;weapon name="Staff" div="staff"&gt;
            &lt;/weapon&gt;
          &lt;/class&gt;
          &lt;class title="Azure Knight" div="ak"&gt;
            &lt;weapon name="1h Axe" div="1ha"&gt;
            &lt;/weapon&gt;
            &lt;weapon name="2h Axe" div="2ha"&gt;
            &lt;/weapon&gt;
            &lt;weapon name="1h Mace" div="1hm"&gt;
            &lt;/weapon&gt;
            &lt;weapon name="2h Mace" div="1hm"&gt;
            &lt;/weapon&gt;
            &lt;weapon name="1h Sword" div="1hs"&gt;
            &lt;/weapon&gt;
            &lt;weapon name="2h Sword" div="1hs"&gt;
            &lt;/weapon&gt;
            &lt;weapon name="Shield" div="shield"&gt;
            &lt;/weapon&gt;
          &lt;/class&gt;
          &lt;class title="Vicious Summoner" div="summy"&gt;
            &lt;weapon name="Twin Blades" div="tb"&gt;
            &lt;/weapon&gt;
            &lt;weapon name="Staff" div="staff"&gt;
            &lt;/weapon&gt;
          &lt;/class&gt;
          &lt;class title="Segnale" div="seg"&gt;
            &lt;weapon name="Whip" div="whip"&gt;
            &lt;/weapon&gt;
          &lt;/class&gt;
          &lt;class title="Aloken" div="alo"&gt;
            &lt;weapon name="Spear" div="spear"&gt;
            &lt;/weapon&gt;
          &lt;/class&gt;
          &lt;class title="Seguriper" div="ripper"&gt;
            &lt;weapon name="Scythe" div="scythe"&gt;
            &lt;/weapon&gt;
          &lt;/class&gt;
          &lt;class title="Concerra Summoner" div="concerra"&gt;
            &lt;weapon name="Duel Blades" div="db"&gt;
            &lt;/weapon&gt;
            &lt;weapon name="Staff" div="staff"&gt;
            &lt;/weapon&gt;
          &lt;/class&gt;
          &lt;class title="Black Wizard" div="wizard"&gt;
            &lt;weapon name="Orb" div="orb"&gt;
            &lt;/weapon&gt;
          &lt;/class&gt;
          &lt;class title="Half Bagi" div="hb"&gt;
            &lt;weapon name="Great Falchion" div="gf"&gt;
            &lt;/weapon&gt;
            &lt;weapon name="Katar" div="katar"&gt;
            &lt;/weapon&gt;
          &lt;/class&gt;
        &lt;/weapons&gt;
      &lt;/MARKET&gt;
  </code></pre>
  
  <p>And the PHP I am trying to use:</p>
  
  <pre><code>&lt;?php $xml = simplexml_load_file('market.xml'); ?&gt;
  &lt;ul&gt;
    &lt;?php
      foreach ($xml-&gt;weapons-&gt;class as $classes) {
        $class = $classes["title"];
        $div = $classes["div"]; 
    ?&gt;
    &lt;li&gt;&lt;a onClick="document.getElementById('&lt;?=$div ?&gt;').style.display=(document.getElementById('&lt;?=$div ?&gt;').style.display =='none')?'':'none'"&gt;&lt;?=$class?&gt;&lt;/a&gt;&lt;/li&gt;
      &lt;div id="&lt;?=$div ?&gt;" class="tree" style="display:none"&gt;
        &lt;ul&gt;
          &lt;?php
          foreach ($xml-&gt;weapons-&gt;$classes-&gt;weapon as $cl_weapon) {
            $weapon = $cl_weapon["name"];
            $weap_div = $cl_weapon["div"];
          ?&gt;
          &lt;li&gt;&lt;a onClick="document.getElementById('&lt;?=$weapon ?&gt;').style.display=(document.getElementById('&lt;?=$weapon ?&gt;').style.display =='none')?'':'none'"&gt;&lt;?=$weapon ?&gt;&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;
        &lt;div id="&lt;?=$weap_div ?&gt;" style="display:none"&gt;
          &lt;ul&gt;
            &lt;?php
              foreach ($xml-&gt;weapons-&gt;$classes-&gt;$cl_weapon-&gt;item as $item) {
                $name = $item-&gt;name;
                $level = $item-&gt;level;
                echo "&lt;li&gt;&lt;a name='".$name."' level='".$level."'&gt;".$name." (".$level.")&lt;/a&gt;&lt;/li&gt;";
              }
            ?&gt;
          &lt;/ul&gt;
        &lt;/div&gt;
      &lt;/li&gt;
      &lt;? } ?&gt;
    &lt;/ul&gt;
  &lt;/div&gt;
    &lt;? } ?&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>The expected outcome should be:</p>
  
  <pre><code>Bagi Warrior
  -&gt;Gauntlet
  ---&gt;Item (not in XML yet)
  ---&gt;Item
  Segita Hunter
  -&gt;Bow
  ---&gt;Item (not in XML yet)
  ---&gt;Item
  -&gt;Crossbow
  ---&gt;Item
  ---&gt;Item
  </code></pre>
  
  <p>So far, it is giving me all the main items (Bagi, Hunter, etc) but when I click to show the children of that item (gauntlets, bow, crossbow, etc), it gives me this error: Warning: Invalid argument supplied for foreach() in test.php on line 14.</p>
  
  <p>I know it has something to do with the <code>$classes</code> in </p>
  
  <pre><code>foreach ($xml-&gt;weapons-&gt;$classes-&gt;weapon as $cl_weapon) {`
  </code></pre>
  
  <p>I just cant think of another way to get the children in only that section though (if that makes sense).</p>