Page 1 of 1

Looking for workaround For PHP writing to XML

Posted: Tue May 27, 2008 9:28 pm
by BC_PHPnewb
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:

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);
?>
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

Re: Looking for workaround For PHP writing to XML

Posted: Tue May 27, 2008 9:50 pm
by s.dot
The workaround would just involve str_replace('</allentries>', '', $whatever); and then just adding it at the end of the post.

However you could do it much more logically using simplexml

Re: Looking for workaround For PHP writing to XML

Posted: Tue May 27, 2008 10:07 pm
by BC_PHPnewb
scottayy - thank you very much for the reply. I am very new to php so I am trying to do what is easiest at this point which I think is just to append the php script I am currently using. So to do as you suggested "str_replace('</allentries>', '', $whatever);" would look like this?

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";
    [b]$xml_doc .=  str_replace('</allentries>', '', $whatever);[/b]
    $default_dir = $_SERVER['DOCUMENT_ROOT'].'/bjcmedia/guestbook/';
    $default_dir .=  "guestbook.xml";   
// write user data
    $fp = fopen($default_dir,'a');
            $write = fwrite($fp,$xml_doc);
            
?>
thank you,
BC

Re: Looking for workaround For PHP writing to XML

Posted: Wed May 28, 2008 4:02 am
by hansford
In "theory" this is the way it should work, but no guarantees as str_replace() has random behavior problems. If it works the first time try it again and again and see if you're getting the results you want before putting it into production. Maybe some php guru will give us the scoop on how to do this properly.

$path = $_SERVER['DOCUMENT_ROOT'] . "/xml/guestbook.xml";
$xml_doc = file_get_contents($path);
$needle = "</allentries>";
$xml_doc = str_replace($needle, " ", $xml_doc);
$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";
$xml_doc .= "</allentries>". "\n\n\n";
$fp = fopen($path,'r+');
$write = fwrite($fp,$xml_doc);
fclose($fp);

Re: Looking for workaround For PHP writing to XML

Posted: Wed May 28, 2008 8:41 pm
by BC_PHPnewb
@hansford - thanks for the lines of code, it ended up working perfectly with no weird behavior! :D
-BC