简体   繁体   English

使用PHP和XML的树菜单

[英]Tree menu using PHP and XML

I am trying to make a tree menu by using PHP and XML. 我正在尝试通过使用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 the PHP I am trying to use: 和我尝试使用的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>

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, 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. 到目前为止,它给了我所有主要物品(Bagi,Hunter等),但是当我单击以显示该物品的子项(手套,弓,cross等)时,它给了我这个错误:警告:提供了无效的参数在第14行的test.php中for foreach()

I know it has something to do with the $classes in 我知道这与$classes有关

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

I just cant think of another way to get the children in only that section though (if that makes sense). 我只是想不出另一种方法来让孩子们只去那个部分(如果那是有道理的)。

I got it. 我知道了。 I needed to start with $classes and move on from there. 我需要从$classes开始并从那里继续前进。 Correct coding: 正确的编码:

<?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 ($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 ($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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM