<?php
	//Import File Koneksi Database
	//require_once('koneksi.php');
	

include 'dbconfig.php';
 
// Create connection
//$nama=$_POST['nama'];
//$kodecab=$_POST['kodecab'];
 
$conn = mysqli_connect($servername, $username, $password, $dbname);

if (!$conn) {
 
    die("Connection failed: " . mysqli_connect_error());
 
}
	
$tipe=$_POST['tipe'];

if ($tipe=='view')
{

	//Membuat SQL Query
	$sql = "SELECT * FROM toko_profile";
	
	//Mendapatkan Hasil
//	$r = mysqli_query($con,$sql);
	
	//Membuat Array Kosong 
/*	$result = array();
	
	while($row = mysqli_fetch_array($r)){
		
		//Memasukkan Nama dan ID kedalam Array Kosong yang telah dibuat 
		array_push($result,array(
			"idno"=>$row['IDNO'],
			"nama_toko"=>$row['nama_toko'],
			"alamat_toko"=>$row['alamat_toko'],
			"no_hp_toko"=>$row['no_hp_toko']
		));
	}
	
	//Menampilkan Array dalam Format JSON
	echo json_encode(array('data'=>$result));
	
	mysqli_close($con);
	
*/


$res = mysqli_query($conn, $sql);

$items = array();
 
if (mysqli_num_rows($res) > 0) {
 
    while($row = mysqli_fetch_object($res)){
 
        array_push($items, $row);
 
//$result["errorcode"] = "1";
 
    $result["data"] = $items;
 
}}
else
{
 
       $result["data"] = "failed";
 
}
 
echo json_encode($result);
 
mysqli_close($conn);
}

if ($tipe=='save')
{
    $nama=$_POST['nama'];
    $nohp=$_POST['nohp'];
    $alamat=$_POST['alamat'];

$sql="update toko_profile set nama_toko='$nama',alamat_toko='$alamat',no_hp_toko='$nohp'";
$response=mysqli_query($conn,$sql) or die('error query :'.$sql);

if ($response==1)
{
    $result['errormsg']='ok';
}else{
    $result['errormsg']='failed';
}

echo json_encode($result);


}
	
?>