Append to XML sheet with PHP

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
boozker
Forum Newbie
Posts: 4
Joined: Wed Apr 16, 2008 5:44 pm

Append to XML sheet with PHP

Post by boozker »

I have posted on multiple forums and also researched this online. I am having no luck. People just don't know it seems. Someone suggested I go here because you guys are the best, let's see if you are :D because I have been working on this now for about a month, not the project, just this ONE thing.

This is for a CMS tool that does NOT use a DB. I decided to go with XML then. At first it was awesome. I am adding to the XML and it adds the content to the page with the corresponding pageid variable I have.

What if they need to add a page though? That's why I made this form.

I have this form

Code: Select all

    <form method="post" action="newpage_submit.php">
        <input type="text" value="Title" name="title"/><br />
        <input type="text" value="Sub Header" name="subheader" /><br />
        <input type="text" value="Content Here" name="content"/><br />
        <input type="submit" name="save" />
    </form>
 
When they hit submit it needs to OPEN the EXISTING xml page (cms.xml) and APPEND the new content.

Here is my barebones xml sheet.

Code: Select all

 
<website>
    <webpage>
        <title></title>
        <subhead></subhead>
        <content>
            <p></p>
        </content>
    </webpage>
</website>
 
So when they hit submit the form passes the content to a newpage_submit.php and that makes variables from the form and then writes the XML. Right now I have this which WRITES the XML, but I can't make it APPEND the xml.

Code: Select all

 
 
    <?php
 
    $newpage_title = $_POST['title'];
    $newpage_subheader = $_POST['subheader'];
    $newpage_content = $_POST['content'];
    $newpage_content = str_replace("\r", "<br />", $newpage_content);
 
    $xw = new xmlWriter();
    $xw->openMemory();
    //$xw->setIndent(true);
    $xw->startElement('webpage');
        $xw->writeElement ('title', $newpage_title);
        $xw->writeElement('subhead', $newpage_subheader);
        $xw->writeElement('content', $newpage_content);
    $xw->endElement(); // webpage
    $xw = $xw->outputMemory(true);
    $xw = str_replace('&#13;', '', $xw);
    $xw = str_replace('<', '<', $xw);
    $xw = str_replace('>', '>', $xw);
    ?>
 
The str_replaces are because XML writer has an html entities function built in, so it converts all the HTML tags to HTML Unicode. Never mind that. Just how in the hell do I append a existing XML sheet! :banghead:

Also, after it appends the XML sheet can it create a new new HTML document with ++pageid so that it makes a new page with a new page id and so it pulls the XML data in i without manually having to go and create a new HTML document copy and pasting the template and adding the the pageid up one?

If anyone can just get me how to append data that would be awesome!
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Append to XML sheet with PHP

Post by s.dot »

fopen() with the a+ mode as the second parameter. ;)
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.
boozker
Forum Newbie
Posts: 4
Joined: Wed Apr 16, 2008 5:44 pm

Re: Append to XML sheet with PHP

Post by boozker »

AH yes, duh I should have thought about that open it, add to it, and resave. Nice.

Thanks, no one else knows about the other stuff though?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Append to XML sheet with PHP

Post by s.dot »

You can just create the settings in your XML.

Code: Select all

<settings>
    <pageid>3</pageid>
</settings>
(unless I understand the problem wrong)
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.
boozker
Forum Newbie
Posts: 4
Joined: Wed Apr 16, 2008 5:44 pm

Re: Append to XML sheet with PHP

Post by boozker »

scottayy wrote:You can just create the settings in your XML.

Code: Select all

<settings>
    <pageid>3</pageid>
</settings>
(unless I understand the problem wrong)
That actually might work. Right now I have it set manually and it pulls the XML data from the Array and looks for that <webpage> number. So if pageid is 2 it goes to the second <webpage>. I think I can get it to work your way to make it more dynamic. Thanks!
boozker
Forum Newbie
Posts: 4
Joined: Wed Apr 16, 2008 5:44 pm

Re: Append to XML sheet with PHP

Post by boozker »

:cry: I have looked everywhere, and no one can tell me how to append XML data with PHP5. I don't know where to even look! Can someone at least point me in the right direction as to where I can find the answer?
Post Reply