我正在尝试使用 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>
还有我正在尝试使用的 PHP:
<?php $xml = simplexml_load_file('market.xml'); ?>
<ul>
<?php
foreach ($xml->weapons->class as $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 as $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 as $item) {
$name = $item->name;
$level = $item->level;
echo "<li><a name='".$name."' level='".$level."'>".$name." (".$level.")</a></li>";
}
?>
</ul>
</div>
</li>
<? } ?>
</ul>
</div>
<? } ?>
</ul>
预期结果应该是:
Bagi Warrior
->Gauntlet
--->Item (not in XML yet)
--->Item
Segita Hunter
->Bow
--->Item (not in XML yet)
--->Item
->Crossbow
--->Item
--->Item
到目前为止,它给了我所有主要物品(Bagi、Hunter 等),但是当我点击显示该物品的子项(手套、弓、弩等)时,它给了我这个错误:警告:提供的参数无效对于第 14 行 test.php 中的 foreach()。
我知道这与$classes
in有关
foreach ($xml->weapons->$classes->weapon as $cl_weapon) {`
我只是想不出另一种方法让孩子们只进入那个部分(如果这有意义的话)。