Kita Pasti Pernah Mendengar Istilah MVC , yak, benar sekali Model,View,Controller .

Itu sebutan bagi framework PHP seperti CI, YII, Cake dll. pernahkan kita berfikir MVC itu bisa dibuat sendiri sesuai keinginan kita. ok Ng usah bertele-tele lagi, langsung saja siapkan database :

data-300x100 

selanjutnya kita buat MVC nya :

1.index.php

<?php
include_once”controller.php”;
$main_ctrl = new controller();
$main_ctrl->invoke();

?>

2. model.php

<?php
include_once”book.php”;
$db=new database();
$db->konek();

class model{
public function getData(){
$sql=mysql_query(“select * from table_oop”);
while($rows=mysql_fetch_array($sql))
$datas[]=$rows;
return $datas;
}
}
?>

3.view.php

<html>
<head><title>MVC OOP</title>
</head>
<body>
<center><?php echo $data ; ?></center>
</body>
</html>

4.Controller.php

<?php
include_once”model.php”;
class controller{
function invoke(){
$model_data = new model();
$rows_data = $model_data->getData();
$data=”<table border=1><tr><td>ID</td><td>Nama</td><td>Email</td><td>Pesan</td></tr>”;
foreach($rows_data as $datas ){
$data.=”<tr><td>”.$datas[‘id’].”</td><td>”.$datas[‘nama’].”</td><td>”.$datas[’email’].”</td><td>”.$datas[‘pesan’].”</td></tr>”;
}
$data.=”</table>”;
include”view.php”;
}
}
?>

5.book.php / sample.php

<?php

class database{

private $dbHOst =”localhost”;
private $dbUser =”root”;
private $dbPass =””;
private $dbDatabase =”oop”;
function konek(){

mysql_connect($this->dbHost,$this->dbUser,$this->dbPass);
mysql_select_db($this->dbDatabase);

}
}

class book{
public $id;
public $nama;
public $email;
public $pesan;

public function __construct($id,$nama,$email,$pesan){
$this->id = $id;
$this->nama = $nama;
$this->email = $email;
$this->pesan = $pesan;

}
}
?>

untuk hasil bisa dilihat :

hasil1-300x225

untuk download sourecode bisa disini

Advertisements

Bismillah..

Dalam Tutorial Kali Ini Saya Coba Mencoba MengeShare cara CRUD secara simultan atau dengan kata lain menggunakan ala PHPMYADMIN, disini kita menggunakan checkbox untuk membantu pembuatannya .. langsung saja siapkan database :

my_foto-300x73

kemudian kita akan masuk ke codingannya :

1.index.php

<?php

include”koneksi.php”;

if($_GET[‘menu’]==”){

echo”<h3>Pengguna Sistem</h3><hr><br>”;
echo”<table border=1 cellpadding=4 cellspacing=0>
<tr bgcolor=#CC9933><td>Id User</td><td>Nama</td><td>Password</td><td>Telpon</td><td>Alamat</td><td>Email</td><td>Instansi</td><td>Status</td><td>Foto</td><td>#</td></tr>”;
$sql=mysql_query(“select * from user”);
while($data=mysql_fetch_array($sql)){

echo”<form method=POST action=’?menu=user’>”;
echo”<tr><td>$data[id_user]</td><td>$data[nama]</td><td>$data[password]</td><td>$data[telpon]</td><td>$data[alamat]</td><td>$data[email]</td><td>$data[instansi]</td><td>$data[status]</td><td><img src=’profile/$data[foto]’ height=50 width=50></td><td><input type=checkbox name=’check[]’ value=’$data[id_user]’></td></tr>”;
}
echo”<tr><td colspan=10 align=right><input type=submit value=’Edit’ name=’ubah’><input type=submit value=’hapus’ name=’delete’ onclick=\”return confirm(‘Yakin Mau Di Hapus?’);\”></form><a href=’?menu=tambah_user’><input type=button value=’Tambah’></a></td></tr>

</table>”;
}

if($_GET[‘menu’]==’user’){
if($_POST[‘ubah’]){
echo”<h3>Ubah User : </h3><hr><br>”;
$cek=$_POST[‘check’];
$jum=count($cek);
for($i=0 ;$i < $jum;$i++){
$sql=mysql_query(“select * from user where id_user=’$cek[$i]'”);
while($data=mysql_fetch_array($sql)){
echo”
<form method=POST action=’aksi.php?act=edit_user’ enctype=’multipart/form-data’>
<input type=hidden name=’id_user[$i]’ value=’$data[id_user]’>
<table border=1 cellpadding=4 cellspacing=0>
<tr><td>id_user</td><td><input type=text name=’id_user[$i]’ value=’$data[id_user]’ disabled size=1></td></tr>
<tr><td>nama</td><td><input type=text name=’nama[$i]’ value=’$data[nama]’></td></tr>
<tr><td>password</td><td><input type=password name=’password[$i]’ value=’$data[password]’></td></tr>
<tr><td>telpon</td><td><input type=text name=’telpon[$i]’ value=’$data[telpon]’></td></tr>
<tr><td>alamat</td><td><input type=text name=’alamat[$i]’ value=’$data[alamat]’></td></tr>
<tr><td>email</td><td><input type=text name=’email[$i]’ value=’$data[email]’></td></tr>
<tr><td>instansi</td><td><input type=text name=’instansi[$i]’ value=’$data[instansi]’></td></tr>
<tr><td>status</td><td><input type=text name=’status[$i]’ value=’$data[status]’></td></tr>
<tr><td>Foto</td><td><img src=’profile/$data[foto]’ height=50 width=50></td></tr>
<tr><td>Foto</td><td><input type=file name=’foto[$i]’></td></tr>”;
}
echo”</table><br><hr><br>”;
}
echo”<input type=submit value=’Simpan’></form>”;
}elseif($_POST[‘delete’]){
$id=$_POST[‘check’];
$ids=count($id);
for($i=0; $i < $ids; $i++){
mysql_query(“delete from user where id_user=’$id[$i]'”);
echo”<script>alert(‘data berhasil di hapus’);window.location=(‘index.php’);</script>”;
}
}

}if($_GET[‘menu’]==’tambah_user’){

echo”<h3>Tambah Userk</h3><hr>
<form method=POST action=’aksi.php?act=tambah_user’ enctype=’multipart/form-data’>
<table border=1 cellpadding=4 cellspacing=0>
<tr><td>Nama </td><td><input type=text name=’nama’></td></tr>
<tr><td>Password</td><td><input type=password name=’password’></td></tr>
<tr><td>Telpon</td><td><input type=text name=’telpon’></td></tr>
<tr><td>Alamat</td><td><input type=text name=’alamat’></td></tr>
<tr><td>Email</td><td><input type=email name=’email’></td></tr>
<tr><td>Instansi</td><td><input type=text name=’instansi’></td></tr>
<tr><td>Status</td><td><input type=text name=’status’></td></tr>
<tr><td>Foto</td><td><input type=file name=’foto’></td></tr>
<tr><td></td><td><input type=submit value=’Simpan’></td></tr>
</table>
</form>
“;

}

?>

//————————————————————————————————————————-

2. koneksi.php

<?php

mysql_connect(“localhost”,”root”,””);
mysql_select_db(“scmwbi”);
?>

//————————————————————————————————————————-

3. aksi.php

<?php

include”koneksi.php”;

if($_GET[‘act’]==’edit_user’){
$jum=$_POST[‘id_user’];
$id=count($jum);
for($i=0;$i<$id;$i++){
$nama=$_POST[‘nama’][$i];
$password=$_POST[‘password’][$i];
$telpon=$_POST[‘telpon’][$i];
$alamat=$_POST[‘alamat’][$i];
$email=$_POST[’email’][$i];
$instansi=$_POST[‘instansi’][$i];
$status=$_POST[‘status’][$i];
$lokasi_file=$_FILES[‘foto’][‘tmp_name’][$i];
$nama_file=$_FILES[‘foto’][‘name’][$i];
if(!empty($lokasi_file)){
move_uploaded_file($lokasi_file,”c:/xampp/htdocs/php/profile/$nama_file”);
mysql_query(“update user set id_user=’$jum[$i]’,nama=’$nama’,password=’$password’,telpon=’$telpon’,alamat=’$alamat’,email=’$email’,instansi=’$instansi’,status=’$status’,foto=’$nama_file’ where id_user=’$jum[$i]'”);
}else{
mysql_query(“update user set id_user=’$jum[$i]’,nama=’$nama’,password=’$password’,telpon=’$telpon’,alamat=’$alamat’,email=’$email’,instansi=’$instansi’,status=’$status’ where id_user=’$jum[$i]'”);
}
header(‘location:index.php’);
}
}

if($_GET[‘act’]==’tambah_user’){
$lokasi_file=$_FILES[‘foto’][‘tmp_name’];
$nama_file=$_FILES[‘foto’][‘name’];
if(!empty($lokasi_file)){
move_uploaded_file($lokasi_file,”c:/xampp/htdocs/php/profile/$nama_file”);
mysql_query(“insert into user(id_user,nama,password,telpon,alamat,email,instansi,status,foto)values(‘$_POST[id_user]’,’$_POST[nama]’,’$_POST[password]’,’$_POST[telpon]’,’$_POST[alamat]’,’$_POST[email]’,’$_POST[instansi]’,’$_POST[status]’,’$nama_file’)”);
header(‘location:index.php’);
}else{
echo”<script>alert(‘Data Harus Dilengkapi !’);window.location=(‘javascript:history.go(-1)’);</script>”;
}
}
?>

//————————————————————————————————————————-

untuk hasil bisa lihat seperti ini :

hasil-300x225

 

untuk download code bisa disini

Bismillah… langsung saja ya. 1. index.php


<!--?php 
echo&quot;
<h3-->Instalan Dengan PHP MYSQL</pre>

<hr />

<form action="proses.php" method="POST">
<table border="1" cellspacing="0" cellpadding="4">
<tbody>
<tr>
<td>Server</td>
<td><input type="text" name="server" /></td>
</tr>
<tr>
<td>Nama</td>
<td><input type="text" name="nama" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="text" name="password" /></td>
</tr>
<tr>
<td>Database</td>
<td><input type="text" name="database" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="INSTALL" /></td>
</tr>
</tbody>
</table>
</form>
<pre>";
?>

2.proses instalasi


<!--?php 
$host=$_POST['server'];
$nama=$_POST['nama'];
$password=$_POST['password'];
$database=$_POST['database'];

$cek=mysql_connect($host,$nama,$password);
$qdb=mysql_query(&quot;create database IF NOT EXISTS $database&quot;);
$buat=mysql_query(&quot;$cek,$qdb&quot;);
if(!buat){
echo&quot;Tidak Bisa Konek , Database TIdak Terbentuk &quot;;
}else{
echo&quot;Koneksi OK <br ?--> Database Tercipta 
 Membuat Table";
$conn=mysql_connect($host,$nama,$password);
if($conn){
mysql_select_db($database);
mysql_query("create table user(id_user int (11) not null,nama varchar (100) not null,password varchar (100) not null,status enum('admin','user') default 'user')");
mysql_query("create table menu(id_menu int (11) not null,nama_menu varchar (100) not null,link varchar (100) not null,aktif enum('Y','N') default 'N')");
echo"
 Instalasi OK -></pre>
<form action="uninstall.php" method="POST"><input type="text" name="server" value="$host" /> <input type="text" name="nama" value="$nama" /> <input type="text" name="password" value="$password" /> <input type="text" name="database" value="$database" /> <input type="submit" value="Uninstall" /></form>
<pre>";
}else{
echo"
NG";
}
}
?>

3.remove.php


<!--?php 
$host=$_POST['server'];
$nama=$_POST['nama'];
$password=$_POST['password'];
$database=$_POST['database'];

mysql_connect($host,$nama,$password);
mysql_select_db($database);
mysql_query(&quot;DROP DATABASE $database&quot;);
echo&quot;Terhapus&quot;;
?-->

untuk hasil bisa dilihat sendiri ya.

download sourcecode