【问题标题】:Selected node not updating所选节点未更新
【发布时间】:2013-07-19 05:58:58
【问题描述】:

我正在尝试修改拖放上传器。上传文件后,它会显示在右侧。我想在右侧为每个文件添加一个描述字段。我的 XML 正在通过检查目录来编写。我能够将每个文件的描述保存到 XML。但如果我要删除或上传文件到目录,它会用“-”覆盖我的所有描述。我正在尝试做的是上传或删除新项目而不覆盖我的旧项目描述。

PHP- 用于删除和上传。 此功能检查目录并将节点添加到每个已上传文件的 XML。

if ( $handle = opendir( $path_to_image_dir ) )
{
    while (false !== ($file = readdir($handle)))
    {
        if ( is_file($path_to_image_dir.'/'.$file) && $file != "." && $file != ".." && $file != "Thumb.db" && $file != "Thumbs.db" && $file != ".DS_Store" )
        {
            list( $width, $height ) = getimagesize($path_to_image_dir.'/'.$file);
            $image = $xml_generator->addChild('image');
            $image->addChild('id', 'file'.$i++);
            $image->addChild('name', $file);
            $image->addChild('width', $width);
            $image->addChild('height', $height);
            $image->addChild('description', '-');
        }
    }
    closedir($handle);
}

// Write the XML File to a file.
$xml_generator->asXML('../file.xml');

Description.Php 为每个项目添加说明。

$filename = "../file.xml";

//value form the pop up description form.
$description = $_POST['description'];

// id of input from the write rightside list. 
$hid = $_POST['fileID'];

$xml->image ->description = $description;

$xml = simplexml_load_file($filename);

foreach($xml->image as $image)
{
    // matching the xml id to input id, and then updating the description. 
    if ($image->id == $fileID)
    {
        $image->description = $description;
    }
}

$xml->asXML($filename);

XML

<images>
  <image>
    <id>file0</id>
    <name>image1.jpg</name>
    <height>1435</height>
    <width>2551</width>
    <description>-</description>
  </image>
  <image>
    <id>file1</id>
    <name>Image2.jpg</name>
    <height>1435</height>
    <width>2551</width>
    <description>-</description>
  </image>
</images>

【问题讨论】:

  • echo $image-&gt;id;echo $description;Description.Php 中为您提供了什么?它们会显示吗? ---我只是问,因为您问题中的代码看起来并没有那么错。如果您可以压缩示例,那么它可以在没有文件交互的情况下简单地执行,这将有助于改善问题。最后是关于 XML 而不是文件上传,所以标题也可能会吸引一些用户。
  • 回声 $image->id;并回显$描述;对我来说,将 ajax 请求的值获取到控制台日志。你可以忽略那些。它并没有真正做任何事情。对不起,这一定让你感到困惑。你可能对标题和问题是正确的。谢谢!
  • @Prix file0 是我的 html 输入的 id。然后我将其与 xml id->file0 匹配。所以它更新了 xml 中唯一的 file0 部分。这部分已经对我有用了。

标签: php xml simplexml


【解决方案1】:

我可以看到您的 Description.Php 存在 3 个问题,首先您有应该称为 $fileID 的变量 $hid,并且您还在 simplexml_load_file 之前调用了 $xml-&gt;image -&gt;description = $description;,最后您没有验证是否fileID 和 description 是否设置。

所以你的文件应该是这样的:

$filename = "../file.xml";

//value form the pop up description form.
$description = $_POST['description'];
if (!isset($description))
    die("The field description is not set.");

// id of input from the write rightside list. 
$fileID = $_POST['fileID'];
if (!isset($fileID))
    die("The field fileID is not set.");

$xml = simplexml_load_file($filename);

foreach($xml->image as $image)
{
    // matching the xml id to input id, and then updating the description. 
    if ($image->id == $fileID)
    {
        $image->description = $description;
    }
}

$xml->asXML($filename);

您还应该使用isset 或类似名称来确保 POST 存在。

关于您的第一个代码,发生的情况是,在不读取 XML 的情况下,您已经为每个文件一遍又一遍地创建新节点并保存为新的 XML 覆盖旧的,以保留您应该做的旧描述像这样:

if ( $handle = opendir( $path_to_image_dir ) )
{
    while (false !== ($file = readdir($handle)))
    {
        if ( is_file($path_to_image_dir.'/'.$file) && $file != "." && $file != ".." && $file != "Thumb.db" && $file != "Thumbs.db" && $file != ".DS_Store" )
        {
            $fileID = 'file'.$i++;
            list( $width, $height ) = getimagesize($path_to_image_dir.'/'.$file);
            $oldImage = $xml_generator->xpath('//images/image[id="'.$fileID.'"]');
            if (count($oldImage) == 0)
            {
                $image = $xml_generator->addChild('image');
                $image->addChild('id', $fileID);
                $image->addChild('name', $file);
                $image->addChild('width', $width);
                $image->addChild('height', $height);
                $image->addChild('description', '-');
            }
            else
            {
                $oldImage = $oldImage[0];
                $oldImage->name = $file;
                $oldImage->width = $width;
                $oldImage->height = $height;
            }
        }
    }
    closedir($handle);
}

// Write the XML File to a file.
//$xml_generator->asXML('../file.xml');
//In case it saves all in one line and u want 
//to properly format the XML use:
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml_generator->asXML());
echo $dom->save('../file.xml');

【讨论】:

  • 哦,这是我的语法问题。我在发布示例时更改了一些变量,我想更改为$fileID,但它在实际代码中的两个位置都有相同的变量。我试过你的代码。它就像我的代码一样工作,但我看到你的方式更干净。仅更改描述就可以正常工作。但是它并不能解决我最初的问题。当我上传或修改(删除)文件时,它仍然被覆盖。然后所有描述都转向&lt;description&gt;-&lt;/description&gt;。非常感谢您的帮助
  • @Tierendu 查看更新,假设 $xml_generator 正在读取../file.xml,那么我们可以使用它来匹配现有图像并简单地更新它,而不是使用空描述重新创建整个文件。
  • 我有一种感觉,那就是问题所在,无法真正想出解决问题的游戏计划。谢谢!
  • 别担心,我不会让你挂的。如果是正确的我会标记它,否则我会告诉你。我现在正在测试代码。非常感谢。
  • 非常感谢。现在效果很好。我试图弄清楚这部分好几天了。
猜你喜欢
  • 2023-03-23
  • 1970-01-01
  • 2021-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-12
  • 2011-11-14
  • 1970-01-01
相关资源
最近更新 更多