CRUD PHP dan XML 2.0

> View.php


<?php
require('simplexml.class.php');
//delete
if($_GET['act']=='hapus'){
$product = simplexml_load_file('product.xml');
$id = $_GET['id'];
$index = 0;
$i= 0;
foreach($product->product as $produk){
if($produk->id_produk == $id){
$index = $i;
break;
}
$i++;
}
unset($product->product[$index]);
file_put_contents('product.xml',$product->asXml());

}
//end of delete

$product = simplexml_load_file('product.xml');
$json = json_encode($product);
$array = json_decode($json,TRUE);
$jumlah = count($array);
$no=1;
echo"<a href='add.php'> <button>add</button> </a> <br><br>";
echo "<table border=0 width=100% style='padding:15px; border:1px solid #000;'> ";
echo "<tr bgcolor='#ccc'> <td>No</td> <td>Nama</td> <td>Harga</td> <td>Edit</td> <td>Hapus</td></tr> ";
foreach($product->product as $produk ){
if(($no%2)==0){
$warna="#ccc";
}else{
$warna="#fff";
}
echo "<tr bgcolor='$warna'><td>".$no."</td>";
echo "<td>".$produk->name."</td>";
echo "<td>".$produk->price."</td>";
echo "<td><a href='edit.php?id=".$produk->id_produk."'> <button>Edit</button></a></td>";
echo "<td><a href='index.php?act=hapus&id=".$produk->id_produk."'><button>Delete</button></a></td></tr>";
$no++;
}
echo" </table>";
?>

> add.php


<?php
if($_POST['save']){
require('simplexml.class.php');
$products = simplexml_load_file('product.xml');
$product = $products->addChild('product');
$product->addChild('id_produk',$_POST['id_produk']);
$product->addChild('name',$_POST['name']);
$product->addChild('price',$_POST['price']);
file_put_contents('product.xml',$products->asXml());
header('location:index.php');
}
?>
<form method="POST">
<p> ID <input type="text" name="id_produk"></p>
<p> Nama <input type="text" name="name"></p>
<p> Harga <input type="text" name="price"></p>
<input type="submit" value="Simpan" name="save"></p>
</form>

> Edit.php


<?php
require('simplexml.class.php');
$products = simplexml_load_file('product.xml');
if($_POST['save']){

foreach($products->product as $product){
if($product->id_produk == $_POST['id_produk']){
$product->name = $_POST['name'];
$product->price = $_POST['price'];
break;
}
}
file_put_contents('product.xml',$products->asXml());
header('location:index.php');
}
foreach($products->product as $product){
if($product->id_produk == $_GET['id']){
$id = $product->id_produk;
$name = $product->name;
$price = $product->price;
break;
}

}
?>
<form method="POST">
<p> ID <input type="text" name="id_produk" value="<?php echo $id; ?>" readonly="readonly"> </p>
<p> Nama <input type="text" name="name" value="<?php echo $name; ?>"></p>
<p> Harga <input type="text" name="price" value="<?php echo $price; ?>"></p>
<input type="submit" value="Simpan" name="save"></p>
</form>

>product.xml

<?xml version="2.0" encoding="utf-8"?>
<products>
 <product>
 <id_produk> 1 </id_produk>
 <name> Gula </name>
 <price>200000</price>
 </product>
 <product>
 <id_produk>2</id_produk>
 <name>minyak</name>
 <price>10000</price>
 </product>

<product><id_produk>3</id_produk><name>sabun</name><price>10000</price></product><product><id_produk>4</id_produk><name>mie instant</name><price>45789</price></product></products>

> Hasil

xml

Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: