Teaching myself PHP this week, and as a test project, I've been building a very simple microblog that uses XML data to store/retrieve short post information. I referenced this question and it managed to get me to the point of producing an XML document that resembled what I wanted.
However, I ran into one issue I couldn't figure out on my own. In the linked solution, the same object is updated over and over, without any new information being put into it:
Ex, the 'third test post':
<postslist>
<post>
<name>Third Post</name>
<date>2013-11-05</date>
<time>00:00</time>
<text>There is some more post text here.</text>
</post>
</postslist>
And the 'fourth test post':
<postslist>
<post>
<name>Fourth Post</name>
<date>2013-11-05</date>
<time>00:00</time>
<text>There is even more post text here.</text>
</post>
</postslist>
My PHP, thusfar, resembles this:
$postname = $_POST["name"];
$postdate = $_POST["date"];
$posttime = $_POST["time"];
$posttext = $_POST["posttext"];
$postname = htmlentities($postname, ENT_COMPAT, 'UTF-8', false);
$postdate = htmlentities($postdate, ENT_COMPAT, 'UTF-8', false);
$posttime = htmlentities($posttime, ENT_COMPAT, 'UTF-8', false);
$posttext = htmlentities($posttext, ENT_COMPAT, 'UTF-8', false);
$xml = simplexml_load_file("posts.xml");
$xml->post = "";
$xml->post->addChild('name', $postname);
$xml->post->addChild('date', $postdate);
$xml->post->addChild('time', $posttime);
$xml->post->addChild('text', $posttext);
$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
$doc->preserveWhiteSpace = true;
$doc->loadXML($xml->asXML(), LIBXML_NOBLANKS);
$doc->save('posts.xml');
What I'm hoping to do is create multiple "post" elements, and add the children only to the newest element.
Any help/tips would be appreciated.