Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: unset problem

  1. #1
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default unset problem

    I tested the foreach with an echo so it works but now I'm getting strange results with 'unset'. One thing I tried shut down my Appache server another just adds "</myquotes>" to the end of the .xml which is better than nothing but not good enough.

    the php:
    $dcount = array();
    $dcount = $_POST["dcount"];
    $dfile = '../sidebar.xml';
    $dxml = simplexml_load_file($dfile);

    foreach($dxml->children() as $dkid)
    {
    if(in_array($dkid->id, $dcount))
    {
    unset($dxml->myquotes->$dcount);
    //usenet($dxml->myquotes->$dcount); - This one adds "</myquotes> to existing file
    //unset($dxml->$dkid->id); - This one shuts off my Appache server
    //$dxml = $dxml->asXML();
    $dxml = $dxml->asXML($dfile);
    }
    }

    the xml structure

    <sidebar>
    <myquotes>
    <id>10001</id>
    <thequote>It's elementry my dear Watson...</thequote>
    <reference>Sherlock Holmes</reference>
    </myquotes>
    </sidebar>

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Have you tried
    PHP Code:
    foreach($dxml->children() as $dkid){
       if(
    in_array($dkid->id$dcount)){
          unset(
    $dkid);
       }

    ?

  3. #3
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default

    What I now have doesn't give me any errors but doesn't modify my xml.

    // Deleting an Announcement)
    if ( $_POST['deletechecker'] == 1 )
    {
    $dcount = array();
    $dcount = $_POST["dcount"];
    $dfile = '../sidebar.xml';
    $dxml = simplexml_load_file($dfile);

    foreach($dxml->children() as $dkid)
    {
    if(in_array($dkid->id, $dcount))
    {
    unset($dkid);

    }
    }
    }

    Shouldn't I have a '$dxml->save("../sidebar.xml");' somewhere after the if statment?

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by I am Abby View Post
    What I now have doesn't give me any errors but doesn't modify my xml.

    // Deleting an Announcement)
    if ( $_POST['deletechecker'] == 1 )
    {
    $dcount = array();
    $dcount = $_POST["dcount"];
    $dfile = '../sidebar.xml';
    $dxml = simplexml_load_file($dfile);

    foreach($dxml->children() as $dkid)
    {
    if(in_array($dkid->id, $dcount))
    {
    unset($dkid);

    }
    }
    }

    Shouldn't I have a '$dxml->save("../sidebar.xml");' somewhere after the if statment?
    yeah, sorry. I meant for you to keep that line at the end, but I didn't copy it.

    PHP Code:
    $dxml $dxml->asXML($dfile); 

  5. The Following User Says Thank You to traq For This Useful Post:

    I am Abby (04-29-2010)

  6. #5
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default

    It seems to be writing the full original xml back to itself. I know this because I have the xml open in dreamweaver...when I try to delete an entry from the web page I get a message in dreamweaver telling me it has been updated. However it's still the same as before.

    And I know the foreach works correctly because I had it echoing back the id without a problem.


    if ( $_POST['deletechecker'] == 1 )
    {
    $dcount = array();
    $dcount = $_POST["dcount"];
    $dfile = '../sidebar.xml';
    $dxml = simplexml_load_file($dfile);

    foreach($dxml->children() as $dkid)
    {
    if(in_array($dkid->id, $dcount))
    {
    unset($dkid);
    }
    }
    $dxml = $dxml->asXML($dfile);


    I tried moving the last line up under the unset($dkid) but that didn't work at all.

  7. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    let me set this up and do some testing. I'll let you know.

  8. #7
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    OKAY...

    If you know the node structure of your xml, this works:
    xml:
    Code:
    <sidebar>
       <myquotes>
          <id>10001</id>
          <thequote>It's elementry my dear Watson...</thequote>
          <reference>Sherlock Holmes</reference>
       </myquotes>
    </sidebar>
    PHP Code:
    $file 'sidebar.xml';
    $xml simplexml_load_file($file);

    // $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); 
    this loops though the xml nodes and deletes the one who's <id> matches $dcount. Notice that we must already know the node structure (e.g., we already know that the $xml->children() are named <myquotes> ). This will be fine for your application, but it's not very dynamic.

    Edit:

    The break; is important for two reasons:

    1) You probably only want to delete one quote. If there's another that mistakenly has the same <id>, it will go too.

    2) The foreach() loop counts the elements it will loop through - and therefore, expects to loop x times. After you unset() the desired variable, there is one fewer element available, and so you'll get a warning for trying to loop over a non-existent element:
    Warning: main() [function.main]: Node no longer exists in /blah/blah/blah on line whatever
    It still works fine, but I want to get it right.

    Last edited by traq; 04-30-2010 at 05:13 AM.

  9. #8
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default

    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
    Last edited by djr33; 05-03-2010 at 05:12 PM.

  10. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Please use [code] tags around your code while posting, every time.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  11. #10
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default

    Will do, sorry.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •