Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Using PHP to update an XML document

  1. #1

    Using PHP to update an XML document

    Hello,

    I realise there is another thread for this, but it is slightly different to what I have here I thought it would be best to start fresh.

    I am attempting to create a xml document for a flash music player, which can be updated using a php form. I've been at it for hours and hours, and have got a half working piece of code, mashed together from many different sources and websites.

    My problem is that when I try add a new "song" using the form, it completely overwrites the entire xml document.

    This is what I have so far:
    XML Document: I have manually placed some data in there for testing purposes.
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <musiclist>
        <song>
            <artist>Gandhi Bo-Bandhi</artist>
            <title>Gandhi Dub</title>
            <url>songs/GandhiDub.mp3</url>
        </song>
        <song>
            <artist>L-Wiz</artist>
            <title>Girl From Codeine City</title>
            <url>songs/02 Girl From Codeine City.mp3</url>
        </song>
    </musiclist>
    Form & current xml contents: The data displays from the xml document correctly.

    PHP Code:
    <h1>Playlist</h1>
    <form action="playlistaction.php" method="post">
    <fieldset>
    <label for="artist">Artist:</label><input type="text" id="artist" name="artist" /><br />
    <label for="title">Title:</label> <input type="text" id="title" name="title"/><br />
    <label for="path">URL:</label> <input type="text" id="url" name="url" /> <br />
    <input type="submit" />
    </fieldset>
    </form>
    <h2>Current entries:</h2>
    <p>Artist - Title - URL</p>
    <?php
    $doc 
    = new DOMDocument();
    $doc->load'musiclist.xml' );
      
    $musiclist $doc->getElementsByTagName"song" );
    foreach( 
    $musiclist as $song )
    {
      
    $artists $song->getElementsByTagName"artist" );
      
    $artist $artists->item(0)->nodeValue;
      
      
    $titles$song->getElementsByTagName"title" );
      
    $title$titles->item(0)->nodeValue;
      
      
    $urls $song->getElementsByTagName"url" );
      
    $url $urls->item(0)->nodeValue;
      
      echo 
    "<b>$artist - $title - $url\n</b><br>";
      }
    ?>
    The form action: - This is the root of the problem & I would like to know if I've approached it completely wrong, and if so, what needs to be done to fix it.

    PHP Code:
    <?php

      $musiclist 
    = array();
      
    $musiclist [] = array(
                      
    "artist" => $_POST['artist'],
                    
    "title" => $_POST['title'],
                    
    "url" => $_POST['url']
      );
      
    $doc = new DOMDocument();
    $doc->formatOutput true;
    $r $doc->createElement"musiclist" );
    $doc->appendChild$r );

    foreach( 
    $musiclist as $song )
    {
    $b $doc->createElement("song");

    $title $doc->createElement("title");
    $title->appendChild(
    $doc->createTextNode$song["title"] )
    );
    $b->appendChild$title );

    $artist $doc->createElement("artist");
    $artist->appendChild(
    $doc->createTextNode$song["artist"] )
    );
    $b->appendChild$artist );
    $url $doc->createElement("url");
    $url->appendChild(
    $doc->createTextNode$song["url"] )
    );

    $b->appendChild$url );
    $r->appendChild$b );
    }
    echo 
    $doc->saveXML();
    $doc->save("musiclist.xml")
    ?>
    So, instead of adding a new song at the end of the xml, the xml is completely wiped and replaced with the new data... Any ideas as to how I can fix this will be greatly apprecaited.

  2. #2
    icio's Avatar
    3,811
    posts
    looks better in lowercase
    I'm not sure what you're trying to loop across there with `foreach ($musiclist as $song)` -- that's not necessary to add one song. It's not necessary to rebuild the document from scratch, either.

    All you need to do is load in the document, get the musiclist element and then add one new song element.

    PHP Code:
    $song = array(
        
    'title' => $_POST['title'],
        
    'artist' => $_POST['artist'],
        
    'url' => $_POST['url'],
    );
        
    $doc = new DOMDocument();
    $doc->load'musiclist.xml' );

    $doc->formatOutput true;
    $r $doc->getElementsByTagName("musiclist")->item(0);

    $b $doc->createElement("song");

    $title $doc->createElement("title");
    $title->appendChild(
        
    $doc->createTextNode$song["title"] )
    );
    $b->appendChild$title );

    $artist $doc->createElement("artist");
    $artist->appendChild(
        
    $doc->createTextNode$song["artist"] )
    );
    $b->appendChild$artist );

    $url $doc->createElement("url");
    $url->appendChild(
        
    $doc->createTextNode$song["url"] )
    );

    $b->appendChild$url );
    $r->appendChild$b );
        
    $doc->save("musiclist.xml"); 
    Hope that helps
    "60% of the time it works... every time." -- Paul Rudd as Brian Fantana.

  3. #3

    Thank you!

    The code worked perfectly

    I've modified it to support file uploads now, so the song is uploaded through the form, and the url is automatically added to the xml document.

    I'll post the final code incase anyone needs it. Its not the tightest, or most secure code in the world, but its fine for what I need it for.


    XML Document (This time with values inputed through the form)

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <musiclist>
    
    <song>
    <title>Gandhi Dub</title>
    <artist>Gandhi Bo-Bandhi</artist>
    <url>songs/GandhiDub.mp3</url>
    </song>
    <song>
    <title>Lost</title>
    <artist>Gandhi Bo-Bandhi</artist>
    <url>songs/lost.mp3</url></song>
    <song>
    <title>Hellhorn</title>
    <artist>Gandhi Bo-Bandhi</artist>
    <url>songs/HELLHORN.mp3</url>
    </song>
    
    </musiclist>
    Form (now with upload) & code to show entries already in the playlist.

    PHP Code:

        <h1>Jukebox Playlist</h1>
        
        <p>Enter the Artist and Title names as you want them to appear on the jukebox. It'll take a few minutes to upload a song after you click submit, so leave it and don't close the page until you see it added to the Current Entries list.</p>

                <form action="playlistaction.php" method="post" enctype="multipart/form-data">
     <table>
       <tr>  
         <td colspan="2"class="labelcell"><label for="artist">Artist:</label></td>
         <td colspan="2"class="fieldcell"><input type="text" id="artist" name="artist"  tabindex="1"/></td>
       </tr>
          <tr>  
         <td colspan="2"class="labelcell"><label for="title">Title:</label></td>
         <td colspan="2"class="fieldcell"> <input type="text" id="title" name="title"  tabindex="2"/><br />
    </td>
       </tr>
            <!-- <tr>  
         <td colspan="2"class="labelcell"><label for="path">URL:</label></td>
         <td colspan="2"class="fieldcell"> <input type="text" id="url" name="url"  tabindex="3"/> <br />
    </td>
       </tr>-->
       <tr>
     <td colspan="2"class="labelcell"><label for="userfile">Song Upload</label></td>
        <td colspan="2"><input name="userfile" type="file" id="userfile"  tabindex="4"></td></tr>
         </td>
       </tr>
       <td colspan="4"><input type="submit" name="upload" class="box" value="Submit" tabindex="5" /></td>
      </table>
    </fieldset>
    </form>
    <h2>Current entries:</h2>
    <p>Artist - Title - URL</p>
    <?php
    $doc 
    = new DOMDocument();
    $doc->load'musiclist.xml' );
      
    $musiclist $doc->getElementsByTagName"song" );
    foreach( 
    $musiclist as $song )
    {
      
    $artists $song->getElementsByTagName"artist" );
      
    $artist $artists->item(0)->nodeValue;
      
      
    $titles$song->getElementsByTagName"title" );
      
    $title$titles->item(0)->nodeValue;
      
      
    $urls $song->getElementsByTagName"url" );
      
    $url $urls->item(0)->nodeValue;
      
      echo 
    "<b>$artist - $title - $url\n</b><br>";
      }
    ?>
    playlistaction.php (also stuck a header at the bottom so it takes you back to the previous page when its done)

    PHP Code:
    <?php
    $uploadDir 
    'songs/';

    if(isset(
    $_POST['upload']))
    {
    $fileName $_FILES['userfile']['name'];
    $tmpName $_FILES['userfile']['tmp_name'];
    $fileSize $_FILES['userfile']['size'];
    $fileType $_FILES['userfile']['type'];
    $filePath $uploadDir $fileName;

    $result move_uploaded_file($tmpName$filePath);
    if (!
    $result) {
                    echo 
    "Error uploading file";
                    exit;
                  }

    }

    $song = array(
        
    'title' => $_POST['title'],
        
    'artist' => $_POST['artist'],
        
    'url' => $filePath,
    );
        
    $doc = new DOMDocument();
    $doc->load'musiclist.xml' );

    $doc->formatOutput true;
    $r $doc->getElementsByTagName("musiclist")->item(0);

    $b $doc->createElement("song");

    $title $doc->createElement("title");
    $title->appendChild(
        
    $doc->createTextNode$song["title"] )
    );
    $b->appendChild$title );

    $artist $doc->createElement("artist");
    $artist->appendChild(
        
    $doc->createTextNode$song["artist"] )
    );
    $b->appendChild$artist );

    $url $doc->createElement("url");
    $url->appendChild(
        
    $doc->createTextNode$song["url"] )
    );

    $b->appendChild$url );
    $r->appendChild$b );
        
    $doc->save("musiclist.xml");

        
    header("Location: {$_SERVER['HTTP_REFERER']}");    
    ?>
    Thanks again for the help, and I hope this code saves people the trouble I had!

  4. #4
    icio's Avatar
    3,811
    posts
    looks better in lowercase
    That's very sporting of you. Glad you go it working
    "60% of the time it works... every time." -- Paul Rudd as Brian Fantana.

  5. #5
    Quote Originally Posted by scazy_lumbag View Post
    The code worked perfectly

    I've modified it to support file uploads now, so the song is uploaded through the form, and the url is automatically added to the xml document.

    I'll post the final code incase anyone needs it. Its not the tightest, or most secure code in the world, but its fine for what I need it for.


    XML Document (This time with values inputed through the form)

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <musiclist>
     
    <song>
    <title>Gandhi Dub</title>
    <artist>Gandhi Bo-Bandhi</artist>
    <url>songs/GandhiDub.mp3</url>
    </song>
    <song>
    <title>Lost</title>
    <artist>Gandhi Bo-Bandhi</artist>
    <url>songs/lost.mp3</url></song>
    <song>
    <title>Hellhorn</title>
    <artist>Gandhi Bo-Bandhi</artist>
    <url>songs/HELLHORN.mp3</url>
    </song>
     
    </musiclist>
    Form (now with upload) & code to show entries already in the playlist.

    PHP Code:
     
        <h1>Jukebox Playlist</h1>
     
        <p>Enter the Artist and Title names as you want them to appear on the jukebox. It'll take a few minutes to upload a song after you click submit, so leave it and don't close the page until you see it added to the Current Entries list.</p>
     
                <form action="playlistaction.php" method="post" enctype="multipart/form-data">
     <table>
       <tr>  
         <td colspan="2"class="labelcell"><label for="artist">Artist:</label></td>
         <td colspan="2"class="fieldcell"><input type="text" id="artist" name="artist"  tabindex="1"/></td>
       </tr>
          <tr>  
         <td colspan="2"class="labelcell"><label for="title">Title:</label></td>
         <td colspan="2"class="fieldcell"> <input type="text" id="title" name="title"  tabindex="2"/><br />
    </td>
       </tr>
            <!-- <tr>  
         <td colspan="2"class="labelcell"><label for="path">URL:</label></td>
         <td colspan="2"class="fieldcell"> <input type="text" id="url" name="url"  tabindex="3"/> <br />
    </td>
       </tr>-->
       <tr>
     <td colspan="2"class="labelcell"><label for="userfile">Song Upload</label></td>
        <td colspan="2"><input name="userfile" type="file" id="userfile"  tabindex="4"></td></tr>
         </td>
       </tr>
       <td colspan="4"><input type="submit" name="upload" class="box" value="Submit" tabindex="5" /></td>
      </table>
    </fieldset>
    </form>
    <h2>Current entries:</h2>
    <p>Artist - Title - URL</p>
    <?php
    $doc 
    = new DOMDocument();
    $doc->load'musiclist.xml' );
     
    $musiclist $doc->getElementsByTagName"song" );
    foreach( 
    $musiclist as $song )
    {
      
    $artists $song->getElementsByTagName"artist" );
      
    $artist $artists->item(0)->nodeValue;
     
      
    $titles$song->getElementsByTagName"title" );
      
    $title$titles->item(0)->nodeValue;
     
      
    $urls $song->getElementsByTagName"url" );
      
    $url $urls->item(0)->nodeValue;
     
      echo 
    "<b>$artist - $title - $url\n</b><br>";
      }
    ?>
    playlistaction.php (also stuck a header at the bottom so it takes you back to the previous page when its done)

    PHP Code:
    <?php
    $uploadDir 
    'songs/';
     
    if(isset(
    $_POST['upload']))
    {
    $fileName $_FILES['userfile']['name'];
    $tmpName $_FILES['userfile']['tmp_name'];
    $fileSize $_FILES['userfile']['size'];
    $fileType $_FILES['userfile']['type'];
    $filePath $uploadDir $fileName;
     
    $result move_uploaded_file($tmpName$filePath);
    if (!
    $result) {
                    echo 
    "Error uploading file";
                    exit;
                  }
     
    }
     
    $song = array(
        
    'title' => $_POST['title'],
        
    'artist' => $_POST['artist'],
        
    'url' => $filePath,
    );
     
    $doc = new DOMDocument();
    $doc->load'musiclist.xml' );
     
    $doc->formatOutput true;
    $r $doc->getElementsByTagName("musiclist")->item(0);
     
    $b $doc->createElement("song");
     
    $title $doc->createElement("title");
    $title->appendChild(
        
    $doc->createTextNode$song["title"] )
    );
    $b->appendChild$title );
     
    $artist $doc->createElement("artist");
    $artist->appendChild(
        
    $doc->createTextNode$song["artist"] )
    );
    $b->appendChild$artist );
     
    $url $doc->createElement("url");
    $url->appendChild(
        
    $doc->createTextNode$song["url"] )
    );
     
    $b->appendChild$url );
    $r->appendChild$b );
     
    $doc->save("musiclist.xml");
     
        
    header("Location: {$_SERVER['HTTP_REFERER']}");    
    ?>
    Thanks again for the help, and I hope this code saves people the trouble I had!
    done a pukka job with that i can't get my head round t to work on my set up ill show you the codes and the warning message but im doing something wrong but one good thng s that it s sort of working just not as good lol......bit of advice from someone would be nice ty

    playlist.xml


    <?xml version="1.0" encoding="utf-8"?>
    <musiclist>
    <track>
    <path>samples/1.mp3</path>
    <title>sample-1</title>
    </track>
    <track>
    <path>samples/2.mp3</path>
    <title>sample-2</title>
    </track><track><path>samples/bomb-01.mp3</path><title>dddd</title></track><track><path>samples/bomb-01.mp3</path><title>dddd</title></track><track><path>samples/bomb-01.mp3</path><title>dddd</title></track><track><path>samples/air.rns</path><title>dddd</title></track><track><path>samples/bomb-01.mp3</path><title>tile</title></track><track><path>samples/evil jaide.gif</path><title>tile</title></track></musiclist>

    (don't watch the uploads was testing it lol)




    playlistaction.php

    <?php
    $uploadDir = 'samples/';
    if(isset($_POST['upload']))
    {
    $fileName = $_FILES['userfile']['name'];
    $tmpName = $_FILES['userfile']['tmp_name'];
    $fileSize = $_FILES['userfile']['size'];
    $fileType = $_FILES['userfile']['type'];
    $filePath = $uploadDir . $fileName;
    $result = move_uploaded_file($tmpName, $filePath);
    if (!$result) {
    echo "Error uploading file";
    exit;
    }
    }
    $track = array(
    'title' => $_POST['path'],
    'path' => $filePath,
    );

    $doc = new DOMDocument();
    $doc->load( 'playlist.xml' );
    $doc->formatOutput = true;
    $r = $doc->getElementsByTagName("musiclist")->item(0);
    $b = $doc->createElement("track");
    $path = $doc->createElement("path");
    $path->appendChild(
    $doc->createTextNode( $track["path"] )
    );
    $b->appendChild( $path );
    $title = $doc->createElement("title");
    $title->appendChild(
    $doc->createTextNode( $track["title"] )
    );
    $b->appendChild( $title );
    $r->appendChild( $b );

    $doc->save("playlist.xml");
    header("Location: {$_SERVER['HTTP_REFERER']}");

    ?>
    <FORM><INPUT TYPE='button' VALUE='Back' onClick='history.go(-1);return true;'> </FORM>(lol had to put a back button)


    uploadpage.php


    <h3>upload music</h3>
    <form action="playlistaction.php" method="post" enctype="multipart/form-data">
    <table>
    <tr>
    <td colspan="2"class="labelcell"><label for="path">title:</label></td>
    <td colspan="2"class="fieldcell"> <input type="text" id="path" name="path" tabindex="2"/><br />
    </td>
    </tr>
    <!-- <tr>
    <td colspan="2"class="labelcell"><label for="path">title:</label></td>
    <td colspan="2"class="fieldcell"> <input type="text" id="title" name="title" tabindex="3"/> <br />
    </td>
    </tr>-->
    <tr>
    <td colspan="2"class="labelcell"><label for="userfile">track Upload</label></td>
    <td colspan="2"><input name="userfile" type="file" id="userfile" tabindex="4"></td></tr>
    </td>
    </tr>
    <td colspan="4"><input type="submit" name="upload" class="box" value="Submit" tabindex="5" /></td>
    </table>
    </form>
    <h3>uploads:</h3>
    <p>title ------------- path</p>
    <?php
    $doc = new DOMDocument();
    $doc->load( 'playlist.xml' );

    $musiclist = $doc->getElementsByTagName( "track" );
    foreach( $musiclist as $track )
    {
    $artists = $track->getElementsByTagName( "artist" );
    $artist = $artists->item(0)->nodeValue;

    $paths= $track->getElementsByTagName( "title" );
    $path= $paths->item(0)->nodeValue;

    $titles = $track->getElementsByTagName( "path" );
    $title = $titles->item(0)->nodeValue;

    echo "<b>$path - $title\n</b><br>";
    }
    ?>


    now i seem to get a warning message "Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\playlistaction.php:5) in C:\wamp\www\playlistaction.php on line 54"

    can anyone help fix my bug thank you ...
    Last edited by darren1; July 28th, 2009 at 07:18 AM.

  6. #6
    Let us live so that when we come to die even the undertaker will be sorry. - Mark Twain
    Don't PM me your CSS, xHTML, JS or PHP questions. I will not reply to ANY IE6 questions.

  7. #7
    Quote Originally Posted by icio View Post
    I'm not sure what you're trying to loop across there with `foreach ($musiclist as $song)` -- that's not necessary to add one song. It's not necessary to rebuild the document from scratch, either.

    All you need to do is load in the document, get the musiclist element and then add one new song element.

    PHP Code:
    $song = array(
        
    'title' => $_POST['title'],
        
    'artist' => $_POST['artist'],
        
    'url' => $_POST['url'],
    );
        
    $doc = new DOMDocument();
    $doc->load'musiclist.xml' );

    $doc->formatOutput true;
    $r $doc->getElementsByTagName("musiclist")->item(0);

    $b $doc->createElement("song");

    $title $doc->createElement("title");
    $title->appendChild(
        
    $doc->createTextNode$song["title"] )
    );
    $b->appendChild$title );

    $artist $doc->createElement("artist");
    $artist->appendChild(
        
    $doc->createTextNode$song["artist"] )
    );
    $b->appendChild$artist );

    $url $doc->createElement("url");
    $url->appendChild(
        
    $doc->createTextNode$song["url"] )
    );

    $b->appendChild$url );
    $r->appendChild$b );
        
    $doc->save("musiclist.xml"); 
    Hope that helps
    Thanks a lot for sharing your hardwork.

    This code is working perfectly. i want to know the delete code for the same.
    if i want to delete some songs entries from the xml file then for that delete code is required.

    and one more thing in this code when i add song it goes to the last. can we set the last added
    entry at the first page?

    Thanks a lot in advance.

    Very nice code implemented.

    Thanks

  8. #8
    icio's Avatar
    3,811
    posts
    looks better in lowercase
    Hi, pwsol. To insert to the top of a node list you'll need to use a combination of `insertBefore` and `$firstChild` in something like the following manner (I've not tested it):

    PHP Code:
    if ($r->firstChild) {
        
    $r->insertBefore($b$r->firstChild);
    } else {
        
    $r->appendChild($b);

    And if you're wanting to delete elements, you'll need get all of the <song> elements and find the one that matches the credentials of what you want to delete. Get those elements using `$r->getElementsByTagName('song')` and loop through it in a `for(each)` loop, checking for the title/file within that element. If they're a match, remove the node using `$r->removeChild($song);`, `break;` and then save as before.

    You can find all of the information that you need here: http://uk3.php.net/manual/en/book.dom.php

    Hope that helps
    "60% of the time it works... every time." -- Paul Rudd as Brian Fantana.

  9. #9

    if u kindly upload the code. whould be helpfull for me.

    Hi thanks for quick reply...

    well i have got this code for delete the xml entry but it has some error. kindly correct the error please.

    <html>
    <head></head>
    <body>
    <?php
    $xmldoc = new DOMDocument();
    $xmldoc->load('sample.xml', LIBXML_NOBLANKS);

    $count = 0;

    $activities = $xmldoc->firstChild->firstChild;
    //prints the list of activities, with checkboxes on the left for each item
    //the $count variable is the id to each entry
    if($activities!=null){
    echo '<form name=\'erase\' action=\'delete.php\' method=\'post\'>' . "\n";
    while($activities!=null){
    $count++;
    echo " <input type=\"checkbox\" name=\"activity[]\" value=\"$count\"/>";
    echo ' '.$activities->textContent.'<br/>'."\n";
    $activities = $activities->nextSibling;
    }
    echo ' <input type=\'submit\' value=\'erase selected\'>';
    echo '</form>';
    }
    ?>
    //section used for inserting new entries. this feature is working as expected.
    <form name='input' action='insert.php' method='post'>
    insert activity:
    <input type='text name='activity'/>
    <input type='submit' value='send'/>
    <br/>
    </form>
    </body>
    </html>

  10. #10
    Quote Originally Posted by pwsol View Post
    Hi thanks for quick reply...

    well i have got this code for delete the xml entry but it has some error. kindly correct the error please.

    <html>
    <head></head>
    <body>
    <?php
    $xmldoc = new DOMDocument();
    $xmldoc->load('sample.xml', LIBXML_NOBLANKS);

    $count = 0;

    $activities = $xmldoc->firstChild->firstChild;
    //prints the list of activities, with checkboxes on the left for each item
    //the $count variable is the id to each entry
    if($activities!=null){
    echo '<form name=\'erase\' action=\'delete.php\' method=\'post\'>' . "\n";
    while($activities!=null){
    $count++;
    echo " <input type=\"checkbox\" name=\"activity[]\" value=\"$count\"/>";
    echo ' '.$activities->textContent.'<br/>'."\n";
    $activities = $activities->nextSibling;
    }
    echo ' <input type=\'submit\' value=\'erase selected\'>';
    echo '</form>';
    }
    ?>
    //section used for inserting new entries. this feature is working as expected.
    <form name='input' action='insert.php' method='post'>
    insert activity:
    <input type='text name='activity'/>
    <input type='submit' value='send'/>
    <br/>
    </form>
    </body>
    </html>
    This code runs well at server but does not delete the entry. this xml file is different than the file u have posted.

    Thanks a lot in advance.

  11. #11
    icio's Avatar
    3,811
    posts
    looks better in lowercase
    Can you paste the contents of the script that actually tries to delete the XML node? Namely delete.php. Also, please surround your script in [php][/ php] tags (minus the space) so that it is formatted in a readable manner (as I have done with the code I pasted.)
    "60% of the time it works... every time." -- Paul Rudd as Brian Fantana.

  12. #12
    Quote Originally Posted by icio View Post
    Can you paste the contents of the script that actually tries to delete the XML node? Namely delete.php. Also, please surround your script in [php][/ php] tags (minus the space) so that it is formatted in a readable manner (as I have done with the code I pasted.)
    Well Thanks again but sorry to say that i do not have the other code
    the file which i had pasted repersent to delete.php.

    Rest if you can build ur own could be more helpful.

    Thanks a lot

  13. #13

  14. #14
    icio's Avatar
    3,811
    posts
    looks better in lowercase
    Yes, I'm sure it would be more helpful if I were to build it on my own.
    "60% of the time it works... every time." -- Paul Rudd as Brian Fantana.

  15. #15
    Quote Originally Posted by icio View Post
    Yes, I'm sure it would be more helpful if I were to build it on my own.
    I will be thankful to you if u will make for me. and will be helpful to me like needy ppl

    well from net i got the following delete code which works ok but
    i want to delete the chide nodes but it deletes the main parent node.

    <?php
    function delete_book_id($id, $filename = 'loaned.xml'){
    $data = simplexml_load_file($filename);
    for($i = 0, $length = count($data->resource->book); $i < $length; $i++){
    if($data->resource->book[$i]->ID == $id){
    unset($data->resource->book[$i]);
    break;
    }
    }
    file_put_contents($filename, $data->saveXML());
    }
    //SAMPLE USAGE
    delete_book_id('Book 1');
    ?>


    xml file
    ----------

    <catalogue>
    <resource>
    <book>
    <ID>Book 1</ID>
    <date>today</date>
    <retdate>tomorrow</retdate>
    <SID>Student 1</SID>
    </book>
    <book>
    <ID>Book 2</ID>
    <date>today</date>
    <retdate>tomorrow</retdate>
    <SID>Student 4</SID>
    </book>
    </resource>
    </catalogue>

    This delete php code works fine
    but my requirement is to delete the chide node <ID>
    but it deletes the all parent node <book>


    my xml file is like this

    slides>
    <slide>
    <url>images/simple_img_4.jpg</url>
    </slide>
    <slide>
    <url>images/simple_img_4.jpg</url>
    </slide>

    I want the php code to delete the url node only

    I will be thankful if anyone make the file

    cheers.

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012