[trick] Crud dengan menggunakan angular-js

berikut ini adalah tricky dari membuat crud dengan menggunakan php dan angular.js

Insert.html


<!DOCTYPE html>
<html data-ng-app="latihan">
<head>
<meta charset="utf-8" />
<title>Insert Multiple Data</title>
<script src="vendor/angular/angular.js"></script>
</head>
<body ng-controller="MainController" >
{{ data }}
<div data-ng-repeat="key in data">
Nama <input type="text" data-ng-model="data[$index].nama"> <br>
Kelas <input type="text" data-ng-model="data[$index].kelas"> <br>
</div>

<button data-ng-click="tambah()">Tambah Data</button>
<button data-ng-click="cancel()" data-ng-show="data.length > 1">Cancel</button>
<button data-ng-click="simpan()">Simpan</button>
<p>{{ pesan }}</p>
</body>
</html>
<script>
angular.module('latihan', [])

.controller('MainController',function($scope,$http){
//set type data 'data' dengan object
$scope.data = [{nama : '',kelas : ''} ];
//fungsi tambah inputan
$scope.tambah = function(){
$scope.data.push({nama : '', kelas : ''});
};
//fungsi kurangi inputan
$scope.cancel = function(){
$scope.data.splice($scope.data.length-1);
};
//fungsi simpan
$scope.simpan = function(){
$http.post('http://localhost/latihan/server.php?action=insert',$scope.data).success(function(response){
if(response > 0){
$scope.data = [{nama : '',kelas : ''} ];
$scope.pesan = response +' Data telah berhasil di simpan';
$scope.$apply();
}
});
}
})
</script>

Edit.html


<!DOCTYPE html>
<html data-ng-app="latihan">
<head>
<meta charset="utf-8" />
<title>Edit Multiple Data</title>
<script src="vendor/angular/angular.js"></script>
</head>
<body ng-controller="MainController" >

<table>
<tr>
<td>No</td>
<td>Nama</td>
<td>Kelas</td>
<td>Action</td>
</tr>
<tr data-ng-repeat="(k,v) in data">
<td>{{ $index +1 }}</td>
<td> <input type="text" data-ng-model="v.nama" data-ng-if="v.isedit"><p data-ng-if="!v.isedit">{{v.nama}}</p></td>
<td><input type="text" data-ng-model="v.kelas" data-ng-if="v.isedit"><p data-ng-if="!v.isedit">{{v.kelas}}</p></td>
<td><button data-ng-click="edit($index)">Edit</button></td>
</tr>
</table>
<button data-ng-click="update()">Update</button>
{{ pesan }}
</body>
</html>
<script>
angular.module('latihan', [])

.controller('MainController',function($scope,$http){
// set type data dataedit dengan array kosong
$scope.dataedit = [];
// fugsin menggambil data ke server
$scope.getdata = function(){
$http.get('http://localhost/latihan/server.php?action=get').success(function(response){
$scope.data = response;
$scope.$apply();
});
};
// init data
$scope.getdata();
//ketika user klik button edit
$scope.edit = function(index){
//set agar ketika user input
//munculkan input type
$scope.data[index]['isedit'] = true;
// masukan data yang di pilih ke array dataedit
$scope.dataedit.push($scope.data[index]);
};
$scope.update = function(){
//kirim data yang di edit saja ke server
$http.post('http://localhost/latihan/server.php?action=update',$scope.dataedit).success(function(response){
if(response > 0){
//munculkan list baru yang sudah berhasil di edit
$scope.getdata();
//tampilkan pesna sukses
$scope.pesan = response +' Data telah berhasil di update';
$scope.$apply();
}
});
}
});
</script>

delete .html


<!DOCTYPE html>
<html data-ng-app="latihan">
<head>
<meta charset="utf-8" />
<title>Delete Multiple Data</title>
<script src="vendor/angular/angular.js"></script>
</head>
<body ng-controller="MainController" >

<table>
<tr>
<td></td>
<td>Nama</td>
<td>Kelas</td>
</tr>
<tr data-ng-repeat="(k,v) in data">
<td><input type="checkbox" data-ng-click="check(v.siswaid)"></td>
<td> <input type="text" data-ng-model="v.nama" data-ng-if="v.isedit"><p data-ng-if="!v.isedit">{{v.nama}}</p></td>
<td><input type="text" data-ng-model="v.kelas" data-ng-if="v.isedit"><p data-ng-if="!v.isedit">{{v.kelas}}</p></td>
</tr>
</table>
<button data-ng-click="delete()">Delete</button>
{{ pesan }}
</body>
</html>
<script>
angular.module('latihan', [])

.controller('MainController',function($scope,$http){
// set type data datadelete dengan array kosong
$scope.datadelete = [];
// fugsin menggambil data ke server
$scope.getdata = function(){
$http.get('http://localhost/latihan/server.php?action=get').success(function(response){
$scope.data = response;
$scope.$apply();
});
};
// init data
$scope.getdata();
//ketika user klik button edit
$scope.check = function(siswaid){
//buat variabel tmp bernilai true
//ini digunakan untuk chek apakah sudah ada
//siswa id di dalah array data delete
//jika sudah ada delete array dengan splce dan set
// tmp jadi false agar tidak di push(dimasukan ke dalam array) kembali di bawah
var tmp = true;
for(var i = 0; i < $scope.datadelete.length;i++){
if($scope.datadelete[i] == siswaid){
$scope.datadelete.splice(i);
tmp = false;
break;
}
}
if(tmp)$scope.datadelete.push(siswaid);
};
$scope.delete = function(){
//kirim data yang di edit saja ke server
$http.post('http://localhost/latihan/server.php?action=delete',$scope.datadelete).success(function(response){
if(response > 0){
//munculkan list baru yang sudah berhasil di delete
$scope.getdata();
//tampilkan pesna sukses
$scope.pesan = response +' Data telah berhasil di delete';
$scope.$apply();
}
});
}
});
</script>

server.php


<?php

$hostname = "localhost";
$user = "root";
$pass = "";
$database = "latihan";

$conn = mysql_connect($hostname,$user,$pass);
if(!$conn) die ("gagal melakukan koneksi");
mysql_select_db($database,$conn) or die("database tidak ditemukan");

switch($_GET['action']){
case 'insert' :
$data = json_decode(file_get_contents("php://input"));
$i = 0;
foreach($data as $k => $v){
$query = "INSERT INTO siswa(nama,kelas) VALUES ('".$v->nama."','".$v->kelas."')";
mysql_query($query);
$i++;
};
echo json_encode($i);
break;
case 'get' :
$query = "SELECT * FROM siswa";
$sql = mysql_query($query);
$data = array();
while($row=mysql_fetch_array($sql)) {
$data[] = $row;
}

echo json_encode($data);
break;
case'update' :
$data = json_decode(file_get_contents("php://input"));
$i = 0;
foreach($data as $k => $v){
$query = "UPDATE siswa set nama = '".$v->nama."' , kelas = '".$v->kelas."' where siswaid = '".$v->siswaid."'";
mysql_query($query);
$i++;
};
echo json_encode($i);
break;
case'delete' :
$data = json_decode(file_get_contents("php://input"));
$i = 0;
foreach($data as $k => $v){
$query = "DELETE FROM siswa where siswaid = '".$v."'";
mysql_query($query);
$i++;
};
echo json_encode($i);
break;
}

[trick] Algoritma binary search


<?php
function BinarySearch( $looping, $array, $key, $low, $high )
{
if( $low > $high )
{
return -1;
}

$looping++;


$middle = intval( ( $low+$high )/2 );
echo "
Looping ke-$looping <br>
middle = ( $low+$high )/2 = $middle <br>
";



if ( $array[$middle] == $key )
{
echo "
Jika ".$array[$middle]." == $key, maka $middle <br>
";

return $middle;
}
elseif ( $key < $array[$middle] )
{
echo "
Jika $key < ".$array[$middle].", maka kembali ke fungsi BinarySearch() <br><br>
";

return BinarySearch( $looping, $array, $key, $low, $middle-1 );
}
elseif ( $key > $array[$middle] )
{
echo "
Jika $key > ".$array[$middle].", maka kembali ke fungsi BinarySearch() <br><br>
";

return BinarySearch( $looping, $array, $key, $high, $middle+1 );
}
}
?>

Biasanya digunakan untuk mencari keakuratan untuk pencarian data SQL dan Kecepatan Pencarian data dalam Microsite. contoh penerapannya bisa kita lihat google.

[trick] beberapa plugin php yang dipakai untuk edit photo


// IMG_FILTER_EDGEDETECT
// IMG_FILTER_BRIGHTNESS
// IMG_FILTER_COLORIZE
// IMG_FILTER_EMBOSS
// IMG_FILTER_GAUSSIAN_BLUR
// IMG_FILTER_GRAYSCALE
// IMG_FILTER_NEGATE
// IMG_FILTER_MEAN_REMOVAL
// IMG_FILTER_SMOOTH

header("Content-type:image/jpeg");
$image = imagecreatefromjpeg('koala.jpg');
imagefilter($image,IMG_FILTER_GRAYSCALE,70);
imagejpeg($image);

kali ini saya akan menggunakan gray scale, yang hasilnyan seperti ini.

koala

 

fungsi lainnya :

https://topidesta.wordpress.com/2015/04/08/memaksimalkan-penggunaan-input-typerange-html-5/

Share Custom Share Social Media

like and share

kita pasti mikir gimana ya caranya button share atau like itu kita buat sendiri , bukan memakai langsung dari facebook atau sosmed lainnya.

nah saya share share dikit aja disini, ya meskipun sedikit mudah2an membantu lah.. hahaha

untuk facebook:

</pre>
<a href="https://www.facebook.com/sharer/sharer.php?u=<? echo $url_webnya ;?>">share fb</a>

untuk twitter:

</pre>
<a href="https://twitter.com/intent/tweet?text=<?php echo $title_web; ?> <?=$url_webnya;?>">share twiiter</a>

untuk google_plus:

</pre>
<a href="https://plus.google.com/share?url=<?php echo $url; ">share g+</a>

Nah demikian lah scriptnya, mudah bukan. heheh, selamat mencoba

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

Contoh Array Dua Dimensi Php dengan Data Static dan Dinamic

sudah lama ng ngepos lagi disini, dan kali ini ada kesempatan buat ngepos mskipun sedikit waktunya.

kali ini saya ngebahas array dua dimensi yang static dan dinamic

> static


<?php
$fruits = array ( "fruits" => array ( 1 => "orange",
2 => "banana",
3 => "apple"
),
"numbers" => array ( 1 => "orange",
2 => "banana",
3 => "apple"
),
"holes" => array ( 1 => "orange",
2 => "banana",
3 => "apple" )
);

echo"<ul>";
foreach($fruits as $index => $buah){
echo "<li>".$index."<ul>" ;
for($i=1;$i<=count($buah);$i++){
echo "<li>".$buah[$i]."</li>";
}
echo"</ul></li>";
}
echo"</ul>";
?>

///////////////////////////////////////////////////////////////////////////////////////////////////////////

> dinamic

teset


<?php
mysql_connect("localhost","root","12345");
mysql_select_db("belajar");
echo"<ul>";
$sql= mysql_query("SELECT DISTINCT parent_id, GROUP_CONCAT( DISTINCT child_id
ORDER BY child_id ) AS child_id_list
FROM table_anda
GROUP BY parent_id");

while($data=mysql_fetch_array($sql)){

echo "<li>".$data['parent_id']."<ul>" ;
for($i=1;$i<=count($data['child_id_list']);$i++){
echo "<li>".$data['parent_id'][$i].$data['child_id_list']."</li>";
}
echo"</ul></li>";
}
echo"</ul>";
?>

Hasilnya :

teset

jangan salah lo , diluar sana byk prgrammer yang masih lemah dalam array lo, hehehe