duancuo1234 2010-11-23 11:02
浏览 60
已采纳

将发布的值保存到php中的xml

<root>
  <gallery name="First"/>
  <gallery name="Second"/>
  <gallery name="Third"/>
</root>

I think I have solved part of a previous problem and now have the foreach loop correct? Still I can't get it to save the xml.

$objXML = new SimpleXMLElement(XML_FILE_NAME, null, true);  
foreach($objXML->xpath('/root/gallery/@name') as $key => $old){
    $new = $_POST['name'][$key];
    echo "$key : $old : $new<br />
";
    $old = $new; // this does nothing??
    }
$objXML->asXML(XML_FILE_NAME);

echo returns:
0 : First : First New
1 : Second : Second New
2 : Third : Third New

Why does this not save the new posted values for 'name' back to my XML doc, what am I doing wrong?

  • 写回答

1条回答 默认 最新

  • doufu2496 2010-11-23 11:09
    关注

    You're in a loop.

    This mean each time the loop comes back to another point $key and $old get their respective new values.

    I guess that's what you're trying to achieve (but I'm not you're sure I got your goal here)

    foreach($objXML->xpath('/root/gallery/@name') as $key => $value){
        $new = $_POST['name'][$key];
        echo "$key : $old : $new<br />
    ";
        $old = $value;
    }
    

    From your comment :

    You can test it here.

    $objXML = new SimpleXMLElement(XML_FILE_NAME, null, true);  
    
    $galleryLore = $objXML->gallery; // finding gallery
    $i = 0;
    foreach($galleryLore as $gallery){
      unset($gallery['name']); // We delete the old name
      $new = $_POST['name'][$i]; // We find the new name value
      $gallery->addAttribute('name', $new); // We add the new attribute we deleted before
      $i++;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信