Looking for workaround For PHP writing to XML

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
BC_PHPnewb
Forum Newbie
Posts: 4
Joined: Sun May 25, 2008 4:33 pm

Looking for workaround For PHP writing to XML

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Looking for workaround For PHP writing to XML

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
BC_PHPnewb
Forum Newbie
Posts: 4
Joined: Sun May 25, 2008 4:33 pm

Re: Looking for workaround For PHP writing to XML

Post 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
hansford
Forum Commoner
Posts: 91
Joined: Mon May 26, 2008 12:38 am

Re: Looking for workaround For PHP writing to XML

Post 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);
BC_PHPnewb
Forum Newbie
Posts: 4
Joined: Sun May 25, 2008 4:33 pm

Re: Looking for workaround For PHP writing to XML

Post by BC_PHPnewb »

@hansford - thanks for the lines of code, it ended up working perfectly with no weird behavior! :D
-BC
Post Reply