【问题标题】:Using foreach to avoid redundant XPath form processing code使用 foreach 避免多余的 XPath 表单处理代码
【发布时间】:2019-10-26 09:35:45
【问题描述】:

已回复@setAttribute not saving back to XML page

我的 XML 如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<facilities>
    <areas>
        <area name="Rocket">
            <trails>
                <trail name="Trail 1" status="CLOSED" />
                <trail name="Trail 2" status="CLOSED" />
                <trail name="Trail 2" status="CLOSED" />
            </trails>
        </area>
    </areas>
</facilities>

我可以使用以下代码成功地编辑属性:

<script src="http://code.jquery.com/jquery-latest.min.js"></script>
 <?php
 $xml = new DOMDocument('1.0', 'utf-8');
 $xml->formatOutput = true; 
 $xml->preserveWhiteSpace = false;
 $xml->load('example.xml');

 $xpath = new DOMXpath($xml);

 $asstatus = $xpath->query("//facilities/areas/area[@name='Rocket']/trails/trail[@name='Trail 1']/@status")->item(0);

$childForm = '';
foreach ( $xpath->query("//trails/trail") as $trail )   {
  $childForm .= "<form action=''>
  <span class=\"title\">".$trail->getAttribute("name")."</span> <span class=\"title\">Status</span>
  <select name=\"asstatusform\" >
  <option selected value=".$trail->getAttribute("status").">".$trail->getAttribute("status")."</option>
  <option value=\"OPEN\">OPEN</option>
  <option value=\"CLOSED\">CLOSED</option>
  <option value=\"RACING CLOSURE\">RACING CLOSURE</option>
  </select></span>
  <br>
  </form>";
}

 ?>

 <form action="" method="POST">
    <?php echo $childForm; ?>
    <input name="submit" type="submit" value="Save" />
</form>

 <?php
 if (isset($_POST['submit']))
 {
$trail->setAttribute('status', $_POST['asstatusform']);
htmlentities($xml->save('example.xml'));
 }

 ?>

我想知道是否有一种简单的方法可以将其更改为使用 foreach,这样我就不必为每个单独的跟踪名称创建新的查询和表单。到目前为止,我已经尝试了一些变体,但都没有取得很大的成功。

【问题讨论】:

    标签: php xml xpath foreach attributes


    【解决方案1】:

    使用 XPath 获取轨迹列表,然后为每个轨迹获取显示属性的简单组合。

    foreach ( $xpath->query("//trails/trail") as $trail )   {
        echo $trail->getAttribute("name")."/".
              $trail->getAttribute("status").PHP_EOL;
    }
    

    您可以决定如何显示这些数据 - 单独的表格/表格等。

    代码中的输出从使用嵌入的 PHP 将数据回显到 HTML。您需要保持一致并确保您也正确嵌套了引号。以下应该在循环中工作......

        echo "<form method=\"POST\" action=''>
      <span class=\"title\">".$trail->getAttribute("name")."</span> <span class=\"title\">Status</span>
      <select name=\"asstatusform\" >
      <option selected value=".$trail->getAttribute("status").">".$trail->getAttribute("status")."</option>
      <option value=\"OPEN\">OPEN</option>
      <option value=\"CLOSED\">CLOSED</option>
      <option value=\"RACING CLOSURE\">RACING CLOSURE</option>
      </select></span>
      <br>
      <input name=\"submit\" type=\"submit\" value=\"Save\" />
      </form>";
    

    【讨论】:

    • 嗨奈杰尔,感谢您的回复。我试图通过在 foreach 语句中回显整个表单并将 nodeValue ?> 替换为 getAttribute("name") ?> 来显示这一点,但它只是弹出出现内部服务器错误。我仍然没有弄清楚我将如何将属性发布回来,但我猜是一次一步。 :)
    • 好的——你能把代码添加到问题中吗——还可以添加错误的内容和位置。
    • 编辑反映,错误只是“内部服务器错误500”
    • 您的代码在未转义引号和混合 echo/php 方面存在各种问题,我已对其进行了更改并将其发布在答案中。
    • 奈杰尔,谢谢。这很有魅力。我已对其进行了更改,以便将值反映在单个提交按钮中。我只有一个问题,那就是帖子。由于它不再由 nodeValue 拉取,因此我无法使用它来保存值。我已将 POST 更改为使用 setAttribute,但现在显示服务器错误。 $trail->setAttribute("status") = $_POST['asstatusform'];
    猜你喜欢
    • 2020-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-28
    相关资源
    最近更新 更多