Looking for workaround For PHP writing to XML
Posted: Tue May 27, 2008 9:28 pm
Hi All,
I am currently using this code to have php write the variables into an xml file however I am trying to figure out a way for the php to add a closing bracket at the end of the script. Here is my code:
However once say two guests write to the file it looks something like this:
<?xml version="1.0" encoding="UTF-8"?>
<allentries>
<guestbook>
<dateandtime>Tue, May 27, 2008 5:26pm</dateandtime>
<ipaddress>Sender's IP Address: xx.xx.xx.xx</ipaddress>
<name>Brian</name>
<emailaddress>email1@email.org</emailaddress>
<message>testing script</message>
</guestbook>
<guestbook>
<dateandtime>Tue, May 27, 2008 8:54pm</dateandtime>
<ipaddress>Sender's IP Address: xx.xx.xx.xx</ipaddress>
<name>Brad</name>
<emailaddress>email2@mit.edu</emailaddress>
<message>does this work?</message>
</guestbook>
see the problem is: how do I make it add </allentries> to the end of the xml file after a person posts? Since If i were to add it to the end of the script the way I have it now after every person posts it would write </allentries> and I only want it once at the very end. Any help is greatly appreciated. Thanks in advance,
BC
I am currently using this code to have php write the variables into an xml file however I am trying to figure out a way for the php to add a closing bracket at the end of the script. Here is my code:
Code: Select all
<?PHP
// read the variables from flash
if(isset($_POST['yourname']) and isset($_POST['youremail']) and isset($_POST['yourmessage'])){
echo "Guestbook Data Posted";
$name = $_POST['yourname'];
$email = $_POST['youremail'];
$message = $_POST['yourmessage'];
$ipaddress = "Sender's IP Address: " . $_SERVER['REMOTE_ADDR'];
$date = date("D, M d, Y g:ia", time()+10800);
}
// hold all data
$xml_doc .= "<guestbook>" . "\n";
$xml_doc .= "<dateandtime>";
$xml_doc .= $date;
$xml_doc .= "</dateandtime>" . "\n";
$xml_doc .= "<ipaddress>";
$xml_doc .= $ipaddress;
$xml_doc .= "</ipaddress>" . "\n";
$xml_doc .= "<name>";
$xml_doc .= $name;
$xml_doc .= "</name>" . "\n";
$xml_doc .= "<emailaddress>";
$xml_doc .= $email;
$xml_doc .= "</emailaddress>" . "\n";
$xml_doc .= "<message>";
$xml_doc .= $message;
$xml_doc .= "</message>" . "\n";
$xml_doc .= "</guestbook>". "\n\n\n";
$default_dir = $_SERVER['DOCUMENT_ROOT'].'/bjcmedia/guestbook/';
$default_dir .= "guestbook.xml";
// write user data
$fp = fopen($default_dir,'a');
$write = fwrite($fp,$xml_doc);
?><?xml version="1.0" encoding="UTF-8"?>
<allentries>
<guestbook>
<dateandtime>Tue, May 27, 2008 5:26pm</dateandtime>
<ipaddress>Sender's IP Address: xx.xx.xx.xx</ipaddress>
<name>Brian</name>
<emailaddress>email1@email.org</emailaddress>
<message>testing script</message>
</guestbook>
<guestbook>
<dateandtime>Tue, May 27, 2008 8:54pm</dateandtime>
<ipaddress>Sender's IP Address: xx.xx.xx.xx</ipaddress>
<name>Brad</name>
<emailaddress>email2@mit.edu</emailaddress>
<message>does this work?</message>
</guestbook>
see the problem is: how do I make it add </allentries> to the end of the xml file after a person posts? Since If i were to add it to the end of the script the way I have it now after every person posts it would write </allentries> and I only want it once at the very end. Any help is greatly appreciated. Thanks in advance,
BC