Pada bagian ini kita akan
membahas mengenai pembuatan file class dan file Index untuk menangani semua
halaman dengan sistem modular. File-file ini akan menjadi inti dari aplikasi
yang akan kita buat. Jika Anda belum membuat database dan mengerti susunan
folder yang harus dibuat sebelumnya, silahkan baca dulu postingan sebelumnya Membuat Halaman Adminisnistrator dan CRUD Menggunakan Konsep Pemrograman Berbasis Object (OOP) Bagian 2/6
Object Oriented Programming |
- Pembuatan file Class yang diletakkan di dalam folder phpclass.
- CConnection.php
1: <?php
2: class CConnection{
3: var $koneksi;
4: public function closeConnection(){
5: mysql_close($this->koneksi);
6: }
7: public function openConnection(){
8: $server = 'localhost';
9: $database= 'db_universitas';
10: $user = 'root';
11: $password= 'password';
12: $this->koneksi = mysql_connect($server,$user,$password) or die (mysql_error());
13: if($this->koneksi){
14: mysql_select_db($database) or die (mysql_error());
15: }else{
16: echo "koneksi ke database gagal";
17: }
18: }
19: }
20: ?>
1: <?php
2: require("CConnection.php");
3: class CMahasiswa{
4: private $c, $nim, $nmmhs, $jnskel, $agama;
5: private $insert, $update;
6: public function getNim(){
7: return $this->nim;
8: }
9: public function setNim($nim){
10: $this->nim = $nim;
11: }
12: public function getNamamahasiswa(){
13: return $this->nmmhs;
14: }
15: public function setNamamahasiswa ($nmmhs){
16: $this->nmmhs = $nmmhs;
17: }
18: public function getJeniskelamin(){
19: return $this->jnskel;
20: }
21: public function setJeniskelamin($jnskel){
22: $this->jnskel=$jnskel;
23: }
24: public function getAgama(){
25: return $this->agama;
26: }
27: public function setAgama($agama){
28: $this->agama=$agama;
29: }
30: public function getInsert(){
31: $insert = false;
32: $sql = "insert into mahasiswa (nim, nmmhs, jnskel, agama) values ('".$this->getNim()."', '".$this->getNamamahasiswa()."', '".$this->getJeniskelamin()."', '".$this->getAgama()."')";
33: $c= new CConnection();
34: $c->openConnection();
35: $query = mysql_query($sql);
36: if($query){
37: $insert = true;
38: }
39: $c->closeConnection();
40: return $insert;
41: }
42: public function getList(){
43: $sql = "select * from mahasiswa";
44: $c = new CConnection();
45: $c->openConnection();
46: $query= mysql_query($sql) or die (mysql_error());
47: return $query;
48: }
49: public function getPage($x,$y){
50: $sql = "select * from mahasiswa limit $x,$y";
51: $c = new CConnection();
52: $c->openConnection();
53: $query2= mysql_query($sql) or die (mysql_error());
54: return $query2;
55: }
56: public function getDelete($isi){
57: $sql = "delete from mahasiswa where nim='$isi'";
58: $c = new CConnection();
59: $c->openConnection();
60: $query=mysql_query($sql) or die (mysql_error());
61: return $query;
62: }
63: public function getUpdate($nim){
64: $update = false;
65: $sql = "UPDATE mahasiswa SET nim = '".$this->getNim()."', nmmhs ='".$this->getNamamahasiswa()."', jnskel='".$this->getJeniskelamin()."', agama='".$this->getAgama()."' where nim='$nim'";
66: $c= new CConnection();
67: $c->openConnection();
68: $query = mysql_query($sql);
69: if($query){
70: $update = true;
71: }
72: $c->closeConnection();
73: return $update;
74: }
75: public function getEdit($nim){
76: $sql = "select * from mahasiswa where nim='$nim'";
77: $c = new CConnection();
78: $c->openConnection();
79: $query= mysql_query($sql) or die (mysql_error());
80: return $query;
81: }
82: }
83: ?>
1: <?php
2: require ("CConnection.php");
3: class CMatkul{
4: private $kode, $matakuliah, $sks, $dosen, $c;
5: private $insert;
6: public function getKode(){
7: return $this->kode;
8: }
9: public function setKode($kode){
10: $this->kode=$kode;
11: }
12: public function getMatakuliah(){
13: return $this->matakuliah;
14: }
15: public function setMatakuliah($matakuliah){
16: $this->matakuliah=$matakuliah;
17: }
18: public function getSks(){
19: return $this->sks;
20: }
21: public function setSks($sks){
22: $this->sks=$sks;
23: }
24: public function getDosen(){
25: return $this->dosen;
26: }
27: public function setDosen($dosen){
28: $this->dosen=$dosen;
29: }
30: public function getList(){
31: $sql = "select * from matkul";
32: $c = new CConnection();
33: $c->openConnection();
34: $query = mysql_query($sql) or die (mysql_error());
35: return $query;
36: }
37: public function getInsert(){
38: $insert = false;
39: $sql = "insert into matkul (kode, matakuliah, sks, dosen) values ('".$this->getKode()."','".$this->getMatakuliah()."','".$this->getSks()."','".$this->getDosen()."')";
40: $c = new CConnection();
41: $c->openConnection();
42: $query = mysql_query($sql) or die (mysql_error());
43: if($query){
44: $insert = true;
45: }
46: $c->closeConnection();
47: return $insert;
48: }
49: public function getPage($x,$y){
50: $sql = "select * from matkul limit $x,$y";
51: $c = new CConnection();
52: $c->openConnection();
53: $query2= mysql_query($sql) or die (mysql_error());
54: return $query2;
55: }
56: public function getDelete($kodeMatkul){
57: $sql = "delete from matkul where kode ='$kodeMatkul'";
58: $c = new CConnection();
59: $c->openConnection();
60: $query = mysql_query($sql) or die (mysql_error());
61: return $query;
62: }
63: public function getUpdate($kode){
64: $update = false;
65: $sql = "UPDATE matkul SET kode = '".$this->getKode()."', dosen ='".$this->getDosen()."', matakuliah='".$this->getMatakuliah()."', sks='".$this->getSks()."' where kode='$kode'";
66: $c= new CConnection();
67: $c->openConnection();
68: $query = mysql_query($sql);
69: if($query){
70: $update = true;
71: }
72: $c->closeConnection();
73: return $update;
74: }
75: public function getEdit($kode){
76: $sql = "select * from matkul where kode='$kode'";
77: $c = new CConnection();
78: $c->openConnection();
79: $query= mysql_query($sql) or die (mysql_error());
80: return $query;
81: }
82: }
83: ?>
1: <?php
2: require("CConnection.php");
3: class CAdmin{
4: private $c, $nama, $password, $username;
5: private $insert, $update;
6: /*
7: public function CMahasiswa(){
8: }
9: */
10: public function getNama(){
11: return $this->nama;
12: }
13: public function setNama($nama){
14: $this->nama = $nama;
15: }
16: public function getPassword(){
17: return $this->password;
18: }
19: public function setPassword ($password){
20: $this->password = md5($password);
21: }
22: public function getUsername(){
23: return $this->username;
24: }
25: public function setUsername($username){
26: $this->username=$username;
27: }
28: public function getInsert(){
29: $insert = false;
30: $sql = "insert into admin (nama, pass, user) values ('".$this->getNama()."', '".$this->getPassword()."', '".$this->getUsername()."')";
31: $c= new CConnection();
32: $c->openConnection();
33: $query = mysql_query($sql);
34: if($query){
35: $insert = true;
36: }else{
37: $insert = mysql_error();
38: }
39: $c->closeConnection();
40: return $insert;
41: }
42: public function getList(){
43: $sql = "select * from admin";
44: $c = new CConnection();
45: $c->openConnection();
46: $query= mysql_query($sql) or die (mysql_error());
47: return $query;
48: }
49: public function getPage($x,$y){
50: $sql = "select * from admin limit $x,$y";
51: $c = new CConnection();
52: $c->openConnection();
53: $query2= mysql_query($sql) or die (mysql_error());
54: return $query2;
55: }
56: public function getDelete($isi){
57: $sql = "delete from admin where user='$isi'";
58: $c = new CConnection();
59: $c->openConnection();
60: $query=mysql_query($sql) or die (mysql_error());
61: return $query;
62: }
63: public function getUpdate($nim){
64: $update = false;
65: $sql = "UPDATE admin SET nama = '".$this->getNama()."', pass ='".$this->getPassword()."', user='".$this->getUsername()."' where user='$username'";
66: $c= new CConnection();
67: $c->openConnection();
68: $query = mysql_query($sql);
69: if($query){
70: $update = true;
71: }
72: $c->closeConnection();
73: return $update;
74: }
75: public function getEdit($nim){
76: $sql = "select * from admin where nim='$nim'";
77: $c = new CConnection();
78: $c->openConnection();
79: $query= mysql_query($sql) or die (mysql_error());
80: return $query;
81: }
82: }
83: ?>
1: <?php
2: require("CConnection.php");
3: class CLogin{
4: private $c, $password, $username;
5: private $login;
6: public function getPassword(){
7: return $this->password;
8: }
9: public function setPassword ($password){
10: $this->password = md5($password);
11: }
12: public function getUsername(){
13: return $this->username;
14: }
15: public function setUsername($username){
16: $this->username=$username;
17: }
18: public function getLogin($username, $password){
19: $login = false;
20: $sql = "select * from admin where user='$username' and pass='$password'";
21: $c = new CConnection();
22: $c->openConnection();
23: $query= mysql_query($sql);
24: $cek = mysql_num_rows($query);
25: if($cek){
26: session_start();
27: $data = mysql_fetch_array($query);
28: $log = mysql_query("Update admin set log ='".date('Y-m-d H:i:s',time())."'where user='$username' and pass='$password'");
29: $_SESSION['user']=$data['nama'];
30: $login = true;
31: }else{
32: $login = false;
33: }
34: return $login;
35: }
36: }
37: ?>
1: <?php
2: session_start();
3: ?>
4: <!DOCTYPE html>
5: <html>
6: <head>
7: <title>Portal With OOP</title>
8: <link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" media="screen" />
9: </head>
10: <body>
11: <?php include "navbar.php"; ?>
12: <div class="container-fluid">
13: <div class="row">
14: <div class="col-md-8 col-md-offset-2">
15: <div class="panel panel-primary">
16: <div class="panel-heading">
17: <?php
18: if(isset($_SESSION['user']) != ''){
19: switch(isset($_GET['m'])){
20: case 'listMahasiswa':
21: echo "Daftar Mahasiswa";
22: break;
23: case 'listMatakuliah':
24: echo "Daftar Matakuliah";
25: break;
26: case 'listAdmin':
27: echo "Daftar Admin";
28: break;
29: case 'formMahasiswa':
30: echo "Form Input Mahasiswa";
31: break;
32: case 'formMatakuliah':
33: echo "Form Input Matakuliah";
34: break;
35: case 'formUpdateMatakuliah':
36: echo "Form Update Matakuliah";
37: break;
38: case 'formUpdateMahasiswa':
39: echo "Form Update Mahasiswa";
40: break;
41: default:
42: echo "Selamat Datang";
43: break;
44: }
45: }else{
46: echo "Selamat Datang";
47: }
48: ?>
49: </div>
50: <div class="panel-body">
51: <?php
52: if(isset($_SESSION['user']) == ''){
53: if(isset($_GET['m']) =='formAdmin'){
54: include "formAdmin.php";
55: }else{
56: include "formLogin.php";
57: }
58: }else{
59: if(isset($_GET['m'])){
60: $page = $_GET['m'].".php"; //listMahasiswa.php
61: }
62: if (empty($_GET['m'])){
63: include "home.php";
64: }else
65: if (!file_exists($page)){
66: echo "Tidak ada halaman";
67: }else{
68: include "$page";
69: }
70: }
71: ?>
72: </div>
73: <div class="panel-footer">
74: Copyright © 2015 by <a href="http://plus.google.com/+ErfianJunianto">Erfian Junianto</a> |
75: <a href="http://www.zonabelajar.net"> www.zonabelajar.net </a>
76: </div>
77: </div>
78: </div>
79: </div>
80: </div>
81: <script src="js/jquery.min.js"></script>
82: <script src="js/bootstrap.min.js"></script>
83: </body>
84: </html>
UPDATED: yang bertanda merah, jika sebelumnya tidak menggunakan isset dan memunculkan notice pada sebagian browser. Dan juga posisi session_start() dipindahkan ke paling atas, karena di beberapa browser ada yang error. Kode ini sudah di test. Jika ada masalah silahkan komentar.Okey, sampai disini dulu, kita akan bahas selanjutnya di bagian 4 yaitu Membuat Halaman Adminisnistrator dan CRUD Menggunakan Konsep Pemrograman Berbasis Object (OOP) Bagian 4/6. Silahkan komentar yaa.. :) Download css dan kawan-kawan disini. Silahkan ekstrak di dalam folder latihan.
UPDATE:
Lihat demo programnya: demo
Download full code nya: download code
0 Response to " Membuat Halaman Adminisnistrator dan CRUD Menggunakan Konsep Pemrograman Berbasis Object (OOP) Bagian 3/6 "
Post a Comment