PHP - Simple Delete Data In XML File

In this tutorial we will create a Simple Delete Data In XML File using PHP. This code will launch a modal dialog to warning the user about deleting the data in the xml file, when the delete is click the data is automatically deleted in the xml file. The code use unset() function to delete a specific data in the xml file base on the index position after clicking the button. This is a user-friendly kind of program feel free to modify it. We will be using XML as a markup language that defines a set of rules for encoding documents in a format that is both readable in a certain way. It is designed to store and transport data that can be manipulated within the local server.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" name="viewport" content="width=device-width, intial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Simple Delete Data In XML File</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <table class="table table-bordered table-striped" style="margin-top:20px;">
  18. <thead class="alert-info">
  19. <th>Student ID</th>
  20. <th>Firstname</th>
  21. <th>Lastname</th>
  22. <th>Address</th>
  23. <th>Action</th>
  24. </thead>
  25. <tbody>
  26. <?php
  27. $file = simplexml_load_file('students.xml');
  28.  
  29. foreach($file->student as $row){
  30. ?>
  31. <tr>
  32. <td><?php echo $row->stud_id; ?></td>
  33. <td><?php echo $row->firstname; ?></td>
  34. <td><?php echo $row->lastname; ?></td>
  35. <td><?php echo $row->address; ?></td>
  36. <td><a href="#delete_<?php echo $row->stud_id; ?>" data-toggle="modal" class="btn btn-danger"><span class="glyphicon glyphicon-remove"></span> Delete</a></td>
  37. </tr>
  38. <div class="modal fade" id="delete_<?php echo $row->stud_id; ?>" role="dialog" aria-hidden="true">
  39. <div class="modal-dialog">
  40. <div class="modal-content">
  41. <div class="modal-header">
  42. <h4 class="modal-title">System Information</h4>
  43. </div>
  44. <div class="modal-body">
  45. <center><h3 class="text-danger">Are you sure you want to Delete this record?</h3></center>
  46. </div>
  47. <div class="modal-footer">
  48. <button type="button" class="btn btn-default" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  49. <a href="delete.php?id=<?php echo $row->stud_id; ?>" class="btn btn-danger"><span class="glyphicon glyphicon-trash"></span> Yes</a>
  50. </div>
  51.  
  52. </div>
  53. </div>
  54. </div>
  55. <?php
  56. }
  57. ?>
  58. </tbody>
  59. </table>
  60. </div>
  61. <script src="js/jquery-3.2.1.min.js"></script>
  62. <script src="js/bootstrap.js"></script>
  63. </body>
  64. </html>

Creating the Main Function

This code contains the main function of the application. This code will remove the data in the xml file when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as delete.php
  1. <?php
  2. $id = $_GET['id'];
  3.  
  4. $students = simplexml_load_file('students.xml');
  5.  
  6. $position = 0;
  7. $i = 0;
  8.  
  9. foreach($students->student as $student){
  10. if($student->id == $id){
  11. $position = $i;
  12. break;
  13. }
  14. $i++;
  15. }
  16.  
  17. unset($students->student[$position]);
  18. file_put_contents('students.xml', $students->asXML());
  19.  
  20. header('location: index.php');
  21.  
  22. ?>
There you have it we successfully created Simple Delete Data In XML File using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment