You are on page 1of 5

OOP PHP & MYSQLI

Database : db_akademik
Tabel : tb_mahasiswa
Field Type(Length) Ket
NIM Int(11) Primary key
Nama Varchar(35)
Alamat text
Isi data :
NIM Nama Alamat
201801 Mirnawati Kuningan
201802 Nur Alamsyah Kuningan
201803 Erna Hikmawati Cirebon

index.php
<h1>CRUD OOP PHP</h1>
<h2>DATA AKADEMIK</h2>

<?php
include "menu.php";

echo "<p>";
include "isi.php";
echo "</p>";
?>
menu.php
|<a href=index.php>Home</a>
|<a href=index.php?mn=tm>Mahasiswa</a>
isi.php

<?php
extract($_GET);
if(isset($mn))
{
if($mn=="tm") include "tampil_mahasiswa.php";
else if($mn=="pm") include "proses_mahasiswa.php";
else if($mn=="im") include "input_mahasiswa.php";
else if($mn=="em") include "edit_mahasiswa.php";
}
?>

database.php
<?php

class database{

var $host = "localhost";


var $user = "root";
var $pass = "";
var $db = "db_akademik";

function __construct(){
mysql_connect($this->host, $this->user, $this->pass);
mysql_select_db($this->db);
}

?>
proses_mahasiswa.php
<?php
include 'class_mahasiswa.php';
$mhs = new mahasiswa();

extract($_POST);
$aksi = $_GET['aksi'];

if($aksi == "tambah"){
$mhs->input($a,$b,$c);
header("location:index.php?mn=tm");
}

elseif($aksi == "hapus"){
$mhs->hapus($_GET['id']);
header("location:index.php?mn=tm");
}

elseif($aksi == "update"){
$mhs->update($a,$b,$c);
header("location:index.php?mn=tm");
}
?>

class_mahasiswa.php
<?php
include 'database.php';
class mahasiswa extends database
{
function __construct(){
parent::__construct();
}
function tampil_data(){
$data = mysql_query("select * from tb_mahasiswa");
while($x = mysql_fetch_array($data)){
$hasil[] = $x;
}
return $hasil;
}

function input($nim,$nama,$alamat){
mysql_query("insert into tb_mahasiswa
values('$nim','$nama','$alamat')");
}

function hapus($id){
mysql_query("delete from tb_mahasiswa where nim='$id'");
}

function edit($id){
$data = mysql_query("select * from tb_mahasiswa where
nim='$id'");
while($d = mysql_fetch_array($data)){
$hasil[] = $d;
}
return $hasil;
}

function update($id,$nama,$alamat){
mysql_query("update tb_mahasiswa set nama='$nama',
alamat='$alamat' where nim='$id'");
}
}
?>
tampil_mahasiswa.php
<?php
include 'class_mahasiswa.php';
$mhs = new mahasiswa();
?>
<a href="index.php?mn=im">+ Input Data</a>
<table border="1">
<tr>
<th>No</th>
<th>Nama</th>
<th>Alamat</th>
<th>Opsi</th>
</tr>
<?php
$no = 1;
foreach($mhs->tampil_data() as $x){
?>
<tr>
<td><?php echo $no++; ?></td>
<td><?php echo $x['nim']; ?></td>
<td><?php echo $x['nama']; ?></td>
<td><?php echo $x['alamat']; ?></td>
<td>
<a href="index.php?mn=em&id=<?php echo $x['nim']; ?
>&aksi=edit">Edit</a>
<a href="index.php?mn=pm&id=<?php echo $x['nim']; ?
>&aksi=hapus">Hapus</a>
</td>
</tr>
<?php
}
?>
</table>

input_mahasiswa.php

<form action="index.php?mn=pm&aksi=tambah" method="post">

<table>
<tr>
<td>NIM</td>
<td><input type="text" name="a"></td>
</tr>
<tr>
<td>Nama</td>
<td><input type="text" name="b"></td>
</tr>
<tr>
<td>Alamat</td>
<td><input type="text" name="c"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Simpan"></td>
</tr>
</table>
</form>
edit_mahasiswa.php
<?php
include 'class_mahasiswa.php';
$mhs = new mahasiswa();
?>

<form action="index.php?mn=pm&aksi=update" method="post">


<?php
foreach($mhs->edit($_GET['id']) as $y){
?>
<table>
<tr>
<td>Nama</td>
<td>
<input type="hidden" name="a" value="<?php echo
$y['nim'] ?>">
<input type="text" name="b" value="<?php echo
$y['nama'] ?>">
</td>
</tr>
<tr>
<td>Alamat</td>
<td><input type="text" name="c" value="<?php echo
$y['alamat'] ?>"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Simpan"></td>
</tr>
</table>
<?php } ?>
</form>

You might also like