Thursday, October 13, 2011

How to modify xml data using php - Part 2

Use the following code to update the submitted value from modify.php into xml:
<?php
function update_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)
        {
       
        $btitle = $_POST['btitle'];        
        $bdate = $_POST['bdate'];
        $bookrentdate = $_POST['bookrentdate'];
        $booksid = $_POST['booksid'];
       
        $data->resource->book[$i]->title = $btitle;
        $data->resource->book[$i]->date = $bdate;
        $data->resource->book[$i]->retdate = $bookrentdate;
        $data->resource->book[$i]->SID = $booksid;
        break;
        }
    }
    file_put_contents($filename, $data->saveXML());
    //echo $deletingbook." Deleted !!!";
}
//SAMPLE USAGE
$updatingbook = $_POST['bid'];
echo $updatingbook;
update_book_id ($updatingbook);
header("Location:index.php");  
//delete_book_id('Book 1');
?>

How to modify xml data using php - Part 1


In modify.php add the following code:
<?php
$modifybook = $_GET['bid'];
$doc = new DOMDocument();
$doc->load( 'loaned.xml' );
 
$booklist = $doc->getElementsByTagName( "book" );

foreach( $booklist as $book )
{
$bookids = $book->getElementsByTagName( "ID" );
$bookid = $bookids->item(0)->nodeValue;
if($bookid == $modifybook){
      $booktitles= $book->getElementsByTagName( "title" );
      $booktitle = $booktitles->item(0)->nodeValue;

      $dates= $book->getElementsByTagName( "date" );
      $date= $dates->item(0)->nodeValue;
     
      $retdates = $book->getElementsByTagName( "retdate" );
      $retdate = $retdates->item(0)->nodeValue;
     
      $sids = $book->getElementsByTagName( "SID" );
      $sid = $sids->item(0)->nodeValue;
      echo "<tr> 
     <td colspan=\"2\" class=\"labelcell\"><label for=\"btitle\">Book Title:</label></td>
     <td colspan=\"2\"class=\"fieldcell\"><input type=\"text\" value=\"$booktitle\" id=\"btitle\" name=\"btitle\"  tabindex=\"1\"/></td>
   </tr>
      <tr> 
     <td colspan=\"2\" class=\"labelcell\"><label for=\"bdate\">Book Issue date:</label></td>
     <td colspan=\"2\" class=\"fieldcell\"> <input type=\"text\" id=\"bdate\" name=\"bdate\" value=\"$date\"  tabindex=\"2\"/><br />
</td>
   </tr>
         <tr> 
     <td colspan=\"2\" class=\"labelcell\"><label for=\"bookrentdate\">Book Rent date:</label></td>
     <td colspan=\"2\" class=\"fieldcell\"> <input type=\"text\" id=\"bookrentdate\" value=\"$retdate\" name=\"bookrentdate\" tabindex=\"3\"/> <br />
</td>
   </tr>
   <tr>
 <td colspan=\"2\" class=\"labelcell\"><label for=\"booksid\">Book SID :</label></td>
    <td colspan=\"2\"><input type=\"text\" id=\"booksid\" name=\"booksid\" value=\"$sid\" tabindex=\"4\">
    <input type=\"hidden\" value=\"$bookid\" id=\"bid\" name=\"bid\" />
    </td></tr>
     </td>
   </tr>";   
  }
}
?>  
   <tr><td colspan="4"><input type="submit" name="upload" class="box" value="Update" tabindex="5" /></td></tr>
  </table> 
</form>

Then on submit of above form, it will call the update.php where we should use following code:
Click here  to read for How to modify xml data using php - Part 2

Monday, October 10, 2011

How to delete xml node using php


Create delete.php file and add the following code
<?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());
    echo $deletingbook." Deleted !!!";
}
//SAMPLE USAGE
$deletingbook = $_GET['bid'];

delete_book_id ($deletingbook);
 header("Location: {$_SERVER['HTTP_REFERER']}");

//delete_book_id('Book 1');
?>

How to add new content in XML using php


In index.php add the following form for inserting new data in xml using php


<form action="booklistaction.php" method="post">
 <table>
   <tr> 
     <td colspan="2"class="labelcell"><label for="btitle">Book Title:</label></td>
     <td colspan="2"class="fieldcell"><input type="text" id="btitle" name="btitle"  tabindex="1"/></td>
   </tr>
      <tr> 
     <td colspan="2"class="labelcell"><label for="bdate">Book Issue date:</label></td>
     <td colspan="2"class="fieldcell"> <input type="text" id="bdate" name="bdate"  tabindex="2"/><br />
</td>
   </tr>
         <tr> 
     <td colspan="2"class="labelcell"><label for="bookrentdate">Book Rent date:</label></td>
     <td colspan="2"class="fieldcell"> <input type="text" id="bookrentdate" name="bookrentdate" tabindex="3"/> <br />
</td>
   </tr>
   <tr>
 <td colspan="2"class="labelcell"><label for="booksid">Student Name ID :</label></td>
    <td colspan="2"><input type="text" id="booksid" name="booksid" tabindex="4"></td></tr>
     </td>
   </tr>
   <tr><td colspan="4"><input type="submit" name="upload" class="box" value="Submit" tabindex="5" /></td></tr>
  </table>

</form>


Then create booklistaction.php and add the following code in this file:


<?php
$book = array(
    'bid' => $_POST['bid'],
    'btitle' => $_POST['btitle'],
    'bdate' => $_POST['bdate'],
    'bookrentdate' => $_POST['bookrentdate'],
    'booksid' => $_POST['booksid'],   
);
 
echo $book["bid"];
$doc = new DOMDocument();
$doc->load( 'loaned.xml' );


$booklist = $doc->getElementsByTagName( "book" );
foreach( $booklist as $book1 )
{
  $bookids = $book1->getElementsByTagName( "ID" );
  $bookid = $bookids->item(0)->nodeValue;  
}
echo "<b>$bookid<br>";
$bookid=$bookid+1;
$book["bid"] = $bookid;
echo "<b>$bookid<br>";


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

$b = $doc->createElement("book");

$bid = $doc->createElement("ID");
$bid->appendChild(
    $doc->createTextNode( $book["bid"] )
);
$b->appendChild( $bid );

$btitle = $doc->createElement("title");
$btitle->appendChild(
    $doc->createTextNode( $book["btitle"] )
);
$b->appendChild( $btitle );


$bdate = $doc->createElement("date");
$bdate->appendChild(
    $doc->createTextNode( $book["bdate"] )
);

$b->appendChild( $bdate );

$bookrentdate = $doc->createElement("retdate");
$bookrentdate->appendChild(
    $doc->createTextNode( $book["bookrentdate"] )
);
$b->appendChild( $bookrentdate );

$booksid = $doc->createElement("SID");
$booksid->appendChild(
    $doc->createTextNode( $book["booksid"] )
);

$b->appendChild( $booksid );
$r->appendChild( $b );
   
$doc->save("loaned.xml");

header("Location: {$_SERVER['HTTP_REFERER']}");   
?>
</body>