Monday, 15 April 2013

Tree menu using PHP and XML -



Tree menu using PHP and XML -

i trying create tree menu using php , xml.

<market> <weapons> <class title="bagi warrior" div="bagi"> <weapon name="gauntlet" div="gauntlet"> </weapon> </class> <class title="segita hunter" div="hunter"> <weapon name="bow" div="bow"> </weapon> <weapon name="crossbow" div="xbow"> </weapon> <weapon name="dagger" div="dagger"> </weapon> </class> <class title="incar magician" div="mage"> <weapon name="wand" div="wand"> </weapon> <weapon name="staff" div="staff"> </weapon> </class> <class title="azure knight" div="ak"> <weapon name="1h axe" div="1ha"> </weapon> <weapon name="2h axe" div="2ha"> </weapon> <weapon name="1h mace" div="1hm"> </weapon> <weapon name="2h mace" div="1hm"> </weapon> <weapon name="1h sword" div="1hs"> </weapon> <weapon name="2h sword" div="1hs"> </weapon> <weapon name="shield" div="shield"> </weapon> </class> <class title="vicious summoner" div="summy"> <weapon name="twin blades" div="tb"> </weapon> <weapon name="staff" div="staff"> </weapon> </class> <class title="segnale" div="seg"> <weapon name="whip" div="whip"> </weapon> </class> <class title="aloken" div="alo"> <weapon name="spear" div="spear"> </weapon> </class> <class title="seguriper" div="ripper"> <weapon name="scythe" div="scythe"> </weapon> </class> <class title="concerra summoner" div="concerra"> <weapon name="duel blades" div="db"> </weapon> <weapon name="staff" div="staff"> </weapon> </class> <class title="black wizard" div="wizard"> <weapon name="orb" div="orb"> </weapon> </class> <class title="half bagi" div="hb"> <weapon name="great falchion" div="gf"> </weapon> <weapon name="katar" div="katar"> </weapon> </class> </weapons> </market>

and php trying use:

<?php $xml = simplexml_load_file('market.xml'); ?> <ul> <?php foreach ($xml->weapons->class $classes) { $class = $classes["title"]; $div = $classes["div"]; ?> <li><a onclick="document.getelementbyid('<?=$div ?>').style.display=(document.getelementbyid('<?=$div ?>').style.display =='none')?'':'none'"><?=$class?></a></li> <div id="<?=$div ?>" class="tree" style="display:none"> <ul> <?php foreach ($xml->weapons->$classes->weapon $cl_weapon) { $weapon = $cl_weapon["name"]; $weap_div = $cl_weapon["div"]; ?> <li><a onclick="document.getelementbyid('<?=$weapon ?>').style.display=(document.getelementbyid('<?=$weapon ?>').style.display =='none')?'':'none'"><?=$weapon ?></a></li> <li> <div id="<?=$weap_div ?>" style="display:none"> <ul> <?php foreach ($xml->weapons->$classes->$cl_weapon->item $item) { $name = $item->name; $level = $item->level; echo "<li><a name='".$name."' level='".$level."'>".$name." (".$level.")</a></li>"; } ?> </ul> </div> </li> <? } ?> </ul> </div> <? } ?> </ul>

the expected outcome should be:

bagi warrior ->gauntlet --->item (not in xml yet) --->item segita hunter ->bow --->item (not in xml yet) --->item ->crossbow --->item --->item

so far, giving me main items (bagi, hunter, etc) when click show children of item (gauntlets, bow, crossbow, etc), gives me error: warning: invalid argument supplied foreach() in test.php on line 14.

i know has $classes in

foreach ($xml->weapons->$classes->weapon $cl_weapon) {`

i cant think of way children in section though (if makes sense).

i got it. needed start $classes , move on there. right coding:

<?php $xml = simplexml_load_file('market.xml'); ?> <ul> <?php foreach ($xml->weapons->class $classes) { $class = $classes["title"]; $div = $classes["div"]; ?> <li><a onclick="document.getelementbyid('<?=$div ?>').style.display=(document.getelementbyid('<?=$div ?>').style.display =='none')?'':'none'"><?=$class?></a></li> <div id="<?=$div ?>" class="tree" style="display:none"> <ul> <?php foreach ($classes->weapon $cl_weapon) { $weapon = $cl_weapon["name"]; $weap_div = $cl_weapon["div"]; ?> <li><a onclick="document.getelementbyid('<?=$weapon ?>').style.display=(document.getelementbyid('<?=$weapon ?>').style.display =='none')?'':'none'"><?=$weapon ?></a></li> <li> <div id="<?=$weap_div ?>" style="display:none"> <ul> <?php foreach ($cl_weapon->item $item) { $name = $item->name; $level = $item->level; echo "<li><a name='".$name."' level='".$level."'>".$name." (".$level.")</a></li>"; } ?> </ul> </div> </li> <? } ?> </ul> </div> <? } ?> </ul>

php xml

No comments:

Post a Comment