The code is still printing the original .xml back to itself. Is it possible that other parts of my page could be messing with the code to delete objects? I use a DOMDocuments object to update my counter…I did this before I learned that Simplexml was better…could that be causing the problem? But then this code does not activate unless you are adding a new quote. I'm starting to pull my hair out in frustration...me bald is not pretty.
Below is my entire page…could there be something the code that interferes with the deleting of objects?
Code:
<?php
//Adding New Announcement
if ( isset($_POST['iquote']) && !empty($_POST['iquote']) )
{
//Save new Announcement to sidebar.xml
$insertcount = $_POST["newcount"];
$insertquote = $_POST["iquote"];
$insertref = $_POST["iref"];
$file = '../sidebar.xml';
$xml = simplexml_load_file($file);
// add new <myquote>
$newItem = $xml->addChild('myquotes');
// add new <id> to <myquote>
$newItem->addChild('id', $insertcount);
// add new <thequote> to <myquote>
$newItem->addChild('thequote', $insertquote);
// add new <reference> to <myquote>
$newItem->addChild('reference', $insertref);
// convert $xml object to string
// and save to sidebar.xml file
$xml = $xml->asXML($file);
//Set and Update Counter to sidebarcounter.xml
$updatecount = $_POST["newcount"] + 1;
$counters = array();
$counters [] = array(
'numcount' => $updatecount
);
$doc = new DOMDocument();
$doc->formatOutput = true;
$r = $doc->createElement( "counters" );
$doc->appendChild( $r );
foreach ( $counters as $mycount )
{
$b = $doc->createElement ( "mycount" );
$numcount = $doc->createElement( "numcount" );
$numcount->appendChild(
$doc->createTextNode( $mycount['numcount'])
);
$b->appendChild( $numcount );
$r->appendChild( $b );
}
$doc->save("../sidebarcounter.xml");
header('Location: '.$_SERVER['PHP_SELF'].'?iquote='.$iquote);
}
// Deleting an Announcement)
if ( $_POST['deletechecker'] == 1 )
{
// $dcount = array();
$dcount = $_POST["dcount"];
$file = '../sidebar.xml';
$xml = simplexml_load_file($file);
echo $dcount;
// $dcount is the <id> of the item you wish to delete
$i = 0;
foreach($xml as $kid){
if($kid->id == $dcount){
unset($xml->myquotes[$i]); break;
}
$i ++;
}
$xml = $xml->asXML($file);
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Announcements</title>
<script language="javascript">
function addhidden(){
document.insertsidebar.deletechecker.value = 1;
}
function subhidden(){
document.insertsidebar.deletechecker.value = 0;
}
</script>
</head>
<body onLoad="subhidden()">
<form action="<?php echo $PHP_SELF;?>" method="post" enctype="multipart/form-data" name="insertsidebar" id="insertsidebar">
<input type="hidden" name="deletechecker" id="deletechecker">
<?php
//get count
$doc = new DOMDocument();
$doc->load('../sidebarcounter.xml');
$getcount = $doc->getElementsByTagName( "mycount" );
$newnumcount = $getcount->item(0)->nodeValue;
echo"<input type='hidden' name='newcount' id='newcount' value=$newnumcount>"
?>
<h3>Add New Sidebar Anouncement</h3>
<p>
<label>Quote
<input name="iquote" type="text" id="iquote" size="140" />
</label>
</p>
<p>
<label>Reference
<input type="text" name="iref" id="iref" />
</label>
<label>
<input type="submit" name="submit" id="submit" value="Submit" />
</label>
</p>
<?php
//list all entries to delete
$file = '../sidebar.xml';
$xml = simplexml_load_file($file);
foreach($xml->children() as $myquote)
{
echo"
<hr>
Quote: ".$myquote->thequote."<br />
Reference: ".$myquote->reference."<br />
<input type=\"checkbox\" name=\"dcount[]\" id=\"dcount[]\" value=".$myquote->id." onClick=\"addhidden()\" /> Delete Item";
}
?>
</form>
</body>
</html>
The other two files are:
sidebarcounter.xml
Code:
<counters>
<mycount>10009</mycount>
</counters>
sidebar.xml
Code:
<sidebar>
<myquotes>
<id>10008</id>
<thequote>the quote goes here</thequote>
<reference>who said it</reference>
</myquotes>
</sidebar>
thanks
Bookmarks