Forum Moderators: open
I have it pretty much sorted, but I cant get it to write to the xml file in the exact syntax I require for the flash file to run.
Heres how my xml file needs to read.
<?xml version="1.0"?>
<mySlideShow>
<myImage>
<myTitle>Example 1</myTitle>
<myDescription><![CDATA[<a href="http://www.link1.php" target="_self">Text and link 1</a>]]></myDescription>
<myMedia>torah.jpg</myMedia>
</myImage>
<myImage>
<myTitle>Example 2</myTitle>
<myDescription><![CDATA[<a href="http://www.link2.php" target="_self">Text and link 2</a>]]></myDescription>
<myMedia>fastride.jpg</myMedia>
</myImage>
<myImage>
</mySlideShow>
This is part of my php file to write to the xml page.
$write_string = "<mySlideShow>";
foreach($mySlideShow_final as $myImage){
$write_string .= "<myImage>";
$write_string .= "<myTitle>$myImage[myTitle]</myTitle>";
$write_string .= "<myDescription>$myImage[myDescription]</myDescription>";
$write_string .= "<myMedia>$myImage[myMedia]</mymedia>";
$write_string .= "</myImage>";
}
$write_string .= "</mySlideShow>";
$fp = fopen("images.xml", "w+");
fwrite($fp, $write_string) or die("Error writing to file");
fclose($fp);
print "<em>Project inserted or deleted successfully :)</em><br />";
print "<a href=\"index.php\" title=\"return\">Return</a>";
?>
Now this doesnt seem to be quite right, as when run it fails to fill out data into the xml file, Im just left with an xml file with a pile of empty nodes.
Now I also need to the description to include the CDATA tag but I have no idea how to write these.
Could anyone help me out?
I would eschew the manual writes completely, and use something like DOMDocument [us2.php.net] or SimpleXML [us2.php.net] to gather and create the file streams.
I always create a schema [w3schools.com] for my XML, and use a tool like Synchro Software <oXygen/> to test the output. A schema will be ahuge help here. It will help you to pinpoint the problem.
before:
$myImage[myMedia]
after:
$myImage['myMedia']
you can write a CDATA tag the same way you wrote the other stuff:
$write_string .= "<![CDATA[" . $myImage['description'] . "]]>";
Here's a situation where cmarshall and I disagree - the DOMDocument library is excellent, but I'd only bother delving into it if the XML gets really complex. You're doing fine building the XML in a simple for-loop of 5 lines, so good onya.
Thank you both.
$write_string .= "<?xml version="1.0" encoding="iso-8859-1"?>\n";
$write_string .= "<myslideshow>\n";
foreach( $mySlideShow_final as $myImage)
{
$write_string .= "<myImage>\n";
$write_string .= "<mytitle>".$myImage[myTitle]."</mytitle>\n";
$write_string .= "<mydescription>".$myImage[myDescription]."</mydescription>\n";
$write_string .= "<myMedia>".$myImage[myMedia]."</myMedia>\n";
$write_string .= "</myImage>\n";
}
$write_string .= "</myslideshow>";
$fp = fopen("images.xml", "w+");
fwrite($fp, $write_string) or die("Error writing to file");
fclose($fp);
print "<em>Project inserted or deleted successfully :)</em><br />";
print "<a href=\"index.php\" title=\"return\">Return</a>";
?>
Now for some reason or another it will fill the last entry, but leave any entrys before empty.
So I end up with a xml file that looks like this (If just two entrys have been submitted)
<?xml version="1.0" encoding="iso-8859-1"?>
<mySlideShow>
<myImage>
<myTitle></myTitle>
<myDescription></myDescription>
<myMedia></myMedia>
</myImage>
<myImage>
<myTitle>test2</myTitle>
<myDescription>this is the second and last entry</myDescription>
<myMedia>test2</myMedia>
</myImage>
</mySlideShow>
I am going to tackle the CDATA issue once I have this working. Why is it only saving the last entry?
foreach($mySlideShow_final as $myImage){
implies that $mySlideShow_final is an array, thus $myImage will temporarily become $mySlideShow_final[0],$mySlideShow_final[1],$mySlideShow_final[2], etc.
then inside the loop you're reading the value of $myImage[myDescription]
that implies that the original array has a structure like
$mySlideShow_final[0]['myTitle']
$mySlideShow_final[0]['myDescription']
$mySlideShow_final[1]['myTitle']
$mySlideShow_final[1]['myDescription']
etc.