Sensation Energy

Tutorials


Content


Advertising


Function 7

XML PHP




Total number: 0

Id Name Email Year Actiona

XML PHP Documentation

PHP XML is used to get the value of an element in the structure of a XML file.


Select (index.php)

Select allows us to list all the data in the table. Here, all columns from the table are selected unconditionally and printed


<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</a></th>
<th>Year</th>
<th>Actiona</th>
</tr>
</thead>
<tbody>
<?php
$roditelj = simplexml_load_file('osoba.xml');
file_put_contents('users.xml', $parent->asXML());
?>
<h2>Total number: <?php echo count($parent); ?> </h2>
<?php
foreach($parent as $child)
{
?>
<tr>
<td><?php echo $child->id; ?></td>
<td><?php echo $child->name; ?></td>
<td><?php echo $child->email; ?></td>
<td><?php echo $child->year; ?></td>
<td><a href="view.php?id=<?php echo $child->id; ?>">View></a>
<a href="edit.php?id=<?php echo $child->id; ?>">Edit</a>
<a href="delete.php?action=delete&id=<?php echo $child['id']; ?>">Delete</a>
</tr>
<?php } ?>
</tbody>
</table>

View (view.php)

The view allows us to see only the individual values from the table via id. From select.php, click View, and then go to the View.php page. It is important to emphasize that this ID is important if the value of id 1 shows all data related to 1


<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</a></th>
<th>Year</th>
</tr>
</thead>
<tbody>
<?php
$parent = simplexml_load_file('users.xml');
foreach($parent as $xml){
if($xml->id == $_GET['id']){
?>
<tr>
<td><?php echo  "$xml->id" ?></td>
<td><?php echo  "$xml->name" ?></td>
<td><?php echo  "$xml->email" ?></td>
<td><?php echo  "$xml->year" ?></td>
</tr>
<?php } }
?>
</tbody>
</table>

Insert (add.php)

Insert allows us to insert data into the database. It is important that the name = "add" long and ame = "name" input from the form match if (isset ($ _ POST ['add'])) {and $ name = $ _POST ['name'] ;, insert jus those values we set.


<form action="" method="post">
<div class="form-group">
<label>Id:</label>
<input type="text" name="id" class="form-input" />
</div>
<div class="form-group">
<label>Name:</label>
<input type="text" name="name"  class="form-input" />
</div>
<div class="form-group">
<label>Email:</label>
<input type="text" name="email" placeholder="email" class="form-input">
</div>
<div class="form-group">
<label>Year</label>
<input type="number" name="year" class="form-input">
</div>
<div class="btn-group">
<button type="submit" name="add" >Add</button>
</div>
</form>
<?php
if(isset($_POST['add'])) {
$parent = simplexml_load_file('users.xml');
$child = $parent->addChild('child ');
$child->addChild('id', $_POST['id']);
$child->addChild('name', $_POST['name']);
$child->addChild('email', $_POST['email']);
$child->addChild('year', $_POST['year']);
file_put_contents('users.xml', $parent->asXML());
header('location:index.php');
}
?>


Edit (edit.php)

Edit lets we edit data from the table using ID. Also from selekt.php we chose edit.php and in the value of the ID it shows all the data related to that ID.


<?php
$parent = simplexml_load_file('users.xml');
if(isset($_POST['edit'])) {
foreach($parent as $xml){
if($xml->id == $_POST['id']){
$xml->name = $_POST['name'];
$xml->email = $_POST['email'];
$xml->year = $_POST['year'];
}
}
file_put_contents('users.xml', $parent->asXML());
header('location:index.php');
}
foreach($parent as $xml){
if($xml->id == $_GET['id']){
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="hidden" name="i" value="<?php echo $_GET['i'] ?>" />
<div class="form-group">
<label><h2>Id</h2></label>
<input type="text" name="id" class="form-input" value="<?php echo $xml->id; ?>">
</div>
<div class="form-group">
<label><h2>Name</h2></label>
<input type="text" name="name" class="form-input" value="<?php echo $xml->name; ?>">
</div>
<div class="form-group">
<label"><h2>Email</h2></label>
<input type="text" name="email" class="form-input" value="<?php echo $xml->email; ?>">
</div>
<div class="form-group">
<label><h2>Year</h2></label>
<input type="text" class="form-input" name="year" value="<?php echo $xml->year; ?>">
</div>
<div class="btn-group">
<input type="submit" name="edit" value="Edit">
</div>
</form>

Delete (delete.php)

Delete allows us to delete data from the table. Of course via id. From select.php we delete and click delete and all values related to the ID we selected will be deleted.


<?php
if (isset($_GET['action']))
{
$parent = simplexml_load_file('users.xml');
$id = $_GET['id'];
$index = 0;
$i = 0;
foreach ($parent->child as $child)
{
if($child['id'] == $id)
{
$index = $i;
break;
}
$i++;
}
unset($parent ->child[$index]);
file_put_contents('users.xml', $parent->asXML());
header("Location: index.php");
}?>

Database (users.xml)


<?xml version="1.0"?>
<parent>
<child>
<id>1</id>
<name>zdravo</name>
<email>asdfasd@mail.com</email>
<year>20</year>
</child>
</parent>