首页 > 解决方案 > 无法添加值 xml

问题描述

每次访问者访问该页面时,我都希望将 XML 节点增加 1。

这是我目前拥有的,但它一直返回值 1...

<?php

$xPostName =   $xml->up;

//load xml file to edit

$xml = simplexml_load_file($_GET['id'].'/info.xml');

$xml->up = $xPostName +1;

// save the updated document

$xml->asXML($_GET['id'].'/info.xml');

echo "done";

?>

标签: phpxmlsimplexml

解决方案


问题是你$xPostName在加载文件之前设置的,所以此时没有值,然后给这个加1更新值...

$xPostName =   $xml->up;
//load xml file to edit
$xml = simplexml_load_file($_GET['id'].'/info.xml');
$xml->up = $xPostName +1;

因此,在加载文件后将其移至...

//load xml file to edit
$xml = simplexml_load_file($_GET['id'].'/info.xml');
$xPostName =   $xml->up;
$xml->up = $xPostName +1;

或者直接增加值...

$xml = simplexml_load_file('out.xml');

$xml->up +=1;

推荐阅读