I have an .xml with three elements that I use to create an announcement sidebar on my pages.
The following code should receive elements from a form, read the.xml, add the new elements on the top, and then write the .xml.Code:<sidebar> <myquote> <id>1111</id> <thequote>Hello World</thequote> <reference>Abby Lee</reference> </myquote> </sidebar>
What I get is a new .xml with just the new data. Funny, I would think it would just give me the opposite. I believe I’m close but I just don’t see the problem. Can anyone point me to the problem? Thanks.
Code://Adding New Announcement //Item comes from form if (isset($_POST['iquote'])) { //Save new Announcement to sidebar.xml $insertcount = $_POST["newcount"]; $insertquote = $_POST["iquote"]; $insertref = $_POST["iref"]; $idoc = array(); $idoc = new DOMDocument(); $idoc->load( '../sidebar.xml' ); $sidebar = array(); $sidebar[] = array( 'id' => $insertcount, 'thequote' => $insertquote, 'reference' => $insertref ); //Read sidebar.xml and add to array $isidebar = $idoc->getElementsByTagName( "myquote" ); foreach( $isidebar as $imyquote ) { $iids = $imyquote->getElementsByTagName( "id" ); $iid = $iids->iten(0) ->nodeValue; $ithequotes = $imyquote->getElementsByTagName( "thequote" ); $ithequote = $ithequotes->item(0) ->nodeValue; $ireferences = $imyquote->getElementsByTagName( "reference" ); $ireference = $ireferences->item(0) ->nodeValue; $sidebar[] = array( 'id' => $iid, 'thequote' => $ithequote, 'reference' => $ireference ); } //rewrite xml $wdoc = new DOMDocument(); $wdoc ->formatOutput = true; $w = $wdoc->createElement( 'sidebar' ); $wdoc->appendChild( $w ); foreach ( $sidebar as $myquote ) { $x = $wdoc->createElement( "myquote" ); $wid = $wdoc->createElement( "id" ); $wid->appendChild( $wdoc->createTextNode( $myquote[ 'id' ] ) ); $x->appendChild( $wid ); $wthequote = $wdoc->createElement( "thequote" ); $wthequote->appendChild( $wdoc->createTextNode( $myquote[ 'thequote' ] ) ); $x->appendChild( $wthequote ); $wreference = $wdoc->createElement( "reference" ); $wreference->appendChild( $wdoc->createTextNode( $myquote[ 'reference' ] ) ); $x->appendChild( $wreference ); $w->appendChild( $x ); } $wdoc->save("../sidebar.xml");



Reply With Quote




Bookmarks