Anda di halaman 1dari 40

OBJECT ORIENTED PROGRAMMING

Menyelesaikan Studi Kasus


Dengan Web Base Berbasis
Object Oriented Programming

PROGRAM PASCASARJANA (PPs)


7 ETOS KERJA
1. KERJA ADALAH RAHMAT AKU BEKERJA TULUS PENUH
SYUKUR
2. KERJA ADALAH AMANAH AKU BEKERJA BENAR PENUH
TANGGUNG JAWAB
3. KERJA ADALAH PANGGILAN AKU BEKERJA TUNTAS PENUH
INTEGRITAS
4. KERJA ADALAH AKTUALISASI AKU BEKERJA KERAS PENUH
SEMANGAT
5. KERJA ADALAH IBADAH Aku Bekerja serius Penuh Kecintaan
6. KERJA ADALAH SENI AKU BEKERJA CERDAS PENUH
KREATIVITAS
7. KERJA ADALAH KEHORMATAN AKU BEKERJA TEKUN
PENUH KEUNGGULAN
Dari Ibnu Abbas Ra, Sesungguhnya Rasulullah Saw Bersabda : Empat Hal (Kenikmatan)
Bila Seseorang Dikaruniai Allah Seluruhnya, Maka Ia Telah Meraih Kebaikan Dunia Dan
Akhirat, Yaitu :
1.Hati Yang Bersyukur,
2.Lisan Yang Bedzikir,
3.Tubuh Yang Sabar Akan Ujian,
4.Isteri Yang Shalihah Yang Tidak Ada Penghianatan Didalam Dirinya Serta
Memelihara Harta (Suami)nya”
Sumber Hr : Thabrany & Baihaqi
Allah Subhanahu wa Ta'ala berfirman: "Mereka tidak menjadi lemah karena bencana yang menimpa mereka di
jalan Allah, dan tidak lesu dan tidak (pula) menyerah (kepada musuh). Allah menyukai orang-orang yang sabar"
(QS. Ali Imran: 146)
UTS : Buat Program untuk modul sbb:
1.Login.php
2.menu.php (pilihan 1. Add, 2. Browse, 3. List, 4. LogOut)
3.Browse.php
4.Add.php
5.Edit.php
6.Delete.php
7.List.php

Ketentuan :
a)Tabel yang digunakan adalah salah satu tabel dalam tesisnya masing-masing
b)Gunakan framework yang telah di buat sebelumnya.
<wml>
<card id="satu" title="Login">
<table>
<tr><td>Username </td>
<td><input width="90" height="35"
name="user"/></td></tr>
<tr><td>Password </td>
<td><input type="password" width="90" height="35"
name="pass"/></td></tr>
<anchor>
<go method="post" href="proses_login.php">
<postfield name="userL" value="$(user)"/>
<postfield name="passL" value="$(pass)"/>
</go>
Login
</anchor>
</table>
</card>
</wml>
<?php
echo('<?xml version="1.0"?>');
echo('<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML
1.1/EN">');
?>
<wml>
<card title=".:: Menu Pilihan ::." newcontext="true">
<p align="left">
<small>
<br/>
<?php
echo "<a href=\"addbola.php\">Add Bola</a><br/>";
echo "<a href=\"browsebola.php\">Browse Bola</a><br/>";
echo "<a href=\"ListBola.php\">List Bola</a><br/>";
echo "<a href=\"FormLogin.wml\">LogOut</a><br/>";
echo "<br/>";
echo "<br/>";
?>
</small>
</p>
</card>
</wml>
<wml>
<card id="baca" title="Browse Bola">
<table width="30%" border="1" style="border-collapse:collapse">
<small> <tr bgcolor="#FFFF11"><td colspan="6" align="center">
<b>DAFTAR PERTANDINGAN PIALA PRESIDEN 2018</b></td></tr>
<?php
include_once 'Table.php';
$table = new Table('skor_bola');
$users = $table->findAll();
?>
<tr><td colspan="6" align="center"><a href="AddBola.php"><b>Tambah Data
Bola</b></a></td></tr>
<tr> <th>ID</th> <th>Klub1</th> <th>Klub2</th> <th>Skor</th>
<th>Action</th> </tr>
<?php foreach($users as $user){?>
<tr align="center">
<td><?php echo $user->ID;?> <td><?php echo $user->klub1;?></td>
<td><?php echo $user->klub2?></td> <td><?php echo $user->skor1.":".$user-
>skor1?></td><td>
<a href="EditBola.php?ID=<?php echo $user->ID?>">Edit |</a>
<a href="DeleteBola.php?ID=<?php echo $user->ID?>">Delete</a>
</td> </tr><?php }?> </table> </card>
</wml>
<wml>
<card id="baca" title="Add Bola">
<table width="20%" border="0" style="border-collapse:collapse">
<tr bgcolor="#FFFF11"><td colspan="2" align="center"> TAMBAH DATA BOLA</td></tr>
<?php
if($_POST){
include_once 'Table.php';
$bola = new Table('skor_bola');
try{
$bola->save(array(
'klub1' => $_POST['klub1'],
'klub2' => $_POST['klub2'],
'skor1' => $_POST['skor1'],
'skor2' => $_POST['skor2'],
'ID' => ''
));
header("Location: Contoh-5.php");
exit;
}catch(Exception $e){
echo 'Gagal Menyimpan Data Bola';
echo '<br/>Error: '.$e->getMessage();
}
}
echo "<form action='' method='post'>";
echo "<tr><td>Klub1</td><td><input type='text' height='22' width='75' name='klub1'></td></tr>";
echo "<tr><td>Klub2</td><td><input type='text' height='22' width='75' name='klub2'></td></tr>";
echo "<tr><td>Skor1</td><td><input type='text' height='22' width='75' name='skor1'></td></tr>";
echo "<tr><td>Skor2</td><td><input type='text' height='22' width='75' name='skor2'></td></tr>";
echo "<tr><td colspan=2><input type='submit' name='simpan' value='Add'> ";
echo "<a href='Contoh-5.php'>Cancel</a> ";
echo "</td></tr></form>";
?>
</table>
</card>
</wml>
<?php
include_once 'Table.php';
$brs = new Table('skor_bola');
if($_POST){
try{
$data = array(
'klub1' => $_POST['klub1'],
'klub2' => $_POST['klub2'],
'skor1' => $_POST['skor1'],
'skor2' => $_POST['skor2'],
'ID' => $_POST['ID']
);
$brs->update($data,"ID='$_POST[ID]'");
header("Location: menu.php");
exit;
}catch(Exception $e){
echo 'Gagal Mengedit data Bola';
echo '<br/>Error: '.$e->getMessage();
}
}
$currentbrs = $brs->findBy('ID',$_GET[ID]);
$currentbrs = $currentbrs->current();
?>
<table width="40%" border="0" style="border-collapse:collapse">
<form action="" method="post">
<tr><th colspan=7>EDITING DATA BOLA</th></tr>
<tr><th>ID Bola</th><th><input type="text" name="ID" value="<?php echo $currentbrs->ID;?>" readonly="readonly"/></th></tr>
<tr><th>Nama Klub 1</th><th><input type="text" name="klub1" value="<?php echo $currentbrs->klub1;?>" /></th></tr>
<tr><th>Nama Klub 2</th><th><input type="text" name="klub2" value="<? echo $currentbrs->klub2;?>"/></th></tr>
<tr><th>Skor Klub 1</th><th><input type="text" name="skor1" value="<? echo $currentbrs->skor1;?>"/></th></tr>
<tr><th>Skor Klub 2</th><th><input type="text" name="skor2" value="<? echo $currentbrs->skor2;?>"/></th></tr>
<tr><th><input type="submit" value="Edit"/></th></tr>
</form>
</table>
<?php
include_once 'Table.php';
$brs = new Table('skor_bola');
if($_POST){
try{
$data = array(
'klub1' => $_POST['klub1'],
'klub2' => $_POST['klub2'],
'skor1' => $_POST['skor1'],
'skor2' => $_POST['skor2'],
'ID' => $_POST['ID']
);
$brs->delete("ID='$_POST[ID]'");
header("Location: menu.php");
exit;
}catch(Exception $e){
echo 'Gagal Menghapus data Bola';
echo '<br/>Error: '.$e->getMessage();
}
}
$currentbrs = $brs->findBy('ID',$_GET[ID]);
$currentbrs = $currentbrs->current();
?>
<table width="40%" border="0" style="border-collapse:collapse">
<form action="" method="post">
<tr><th colspan=7>DELETING DATA BOLA</th></tr>
<tr><th>ID Bola</th><th><input type="text" name="ID" value="<?php echo $currentbrs->ID;?>"
readonly="readonly"/></th></tr>
<tr><th>Nama Klub 1</th><th><input type="text" name="klub1" value="<?php echo $currentbrs->klub1;?>"
/></th></tr>
<tr><th>Nama Klub 2</th><th><input type="text" name="klub2" value="<? echo $currentbrs->klub2;?>"/></th></tr>
<tr><th>Skor Klub 1</th><th><input type="text" name="skor1" value="<? echo $currentbrs->skor1;?>"/></th></tr>
<tr><th>Skor Klub 2</th><th><input type="text" name="skor2" value="<? echo $currentbrs->skor2;?>"/></th></tr>
<tr><th><input type="submit" value="Delete"/></th></tr>
</form>
</table>
UTS
Buat Program untuk modul sbb:
1.Login.php
2.menu.php (pilihan 1. Add, 2. Browse, 3. List, 4. LogOut)
3.Browse.php
4.Add.php
5.Edit.php
6.Delete.php
7.List.php

Ketentuan :
a)Tabel yang digunakan adalah salah satu tabel dalam tesisnya masing-masing
b)Gunakan framework yang telah di buat sebelumnya.
Nama File : Config.ini
UTS : (Buat Program untuk modul sbb: tabel yang
;database configuration digunakan adalah salah satu tabel dalam tesisnya)
dbhost="localhost"
dbuser="root" 1. Login.php
2. menu.php (pilihan 1. Add, 2. Browse, 3. List, 4.
dbpassword="" LogOut)
dbname=“databasewap" 3. Browse.php
4. Add.php
5. Edit.php
6. Delete.php
7. List.php
Nama File : Config.php
<?php
class Config {
protected static $_config = array();
public static function getConfig($key){
if(!self::$_config){
$filename =
dirname(__FILE__).DIRECTORY_SEPARATOR.'config.ini';
$config = parse_ini_file($filename);
if(false === $config){
throw new Exception('Gagal membaca file konfigurasi');
}
self::$_config = $config;
}

if(isset(self::$_config[$key])){
return self::$_config[$key];
}
}
}
?>
Nama File : Connect1.php
<?php
include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'Config.php';
class Connect {
protected static $_connection;
public static function getConnection(){
if(!self::$_connection){
$dbhost = Config::getConfig('dbhost');
$dbuser = Config::getConfig('dbuser');
$dbpassword = Config::getConfig('dbpassword');
$dbname = Config::getConfig('dbname');
self::$_connection = @mysql_connect($dbhost, $dbuser, $dbpassword);
if(!self::$_connection){ throw new Exception('Gagal melalukan koneksi ke database. '.mysql_error());
}
$result = @mysql_select_db($dbname, self::$_connection);
if(!$result){ throw new Exception('Koneksi gagal: '.mysql_error());
}
}
return self::$_connection;
}
public static function close(){
if(self::$_connection){
mysql_close(self::$_connection);
}
}
}
?>
Nama File : Table.php
<?php
include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'Connect1.php';
class Table {
protected $_tableName;
function __construct($tableName){
$this->_tableName = $tableName;
}
public function connect(){
return Connect::getConnection();
}
public function close(){
Connect::close();
}
function save(array $data){
$sql = "INSERT INTO `".$this->_tableName."` SET";
foreach($data as $field => $value){
$sql .= " `".$field."`='".mysql_real_escape_string($value, Connect::getConnection())."',";
}
$sql = rtrim($sql, ',');
$result = mysql_query($sql, Connect::getConnection());
if(!$result){
throw new Exception('Gagal menyimpan data ke table '.$this->_tableName.': '.mysql_error());
}
}
Lanjutan Nama File : Table.php
function update(array $data, $where = ''){
$sql = "UPDATE `".$this->_tableName."` SET";
foreach($data as $field => $value){
$sql .= " `".$field."`='".mysql_real_escape_string($value, Connect::getConnection())."',";
}
$sql = rtrim($sql, ',');
if($where){
$sql .= " WHERE ".$where;
}
$result = mysql_query($sql, Connect::getConnection());
if(!$result){
throw new Exception('Gagal mengupdate data table '.$this->_tableName.': '.mysql_error());
}
}
function delete($where = ''){
$sql = "DELETE FROM `".$this->_tableName."`";
if($where){
$sql .= " WHERE ".$where;
}
$result = mysql_query($sql, Connect::getConnection());
if(!$result){
throw new Exception('Gagal menghapus data dari table '.$this->_tableName.': '.mysql_error());
}
}
Lanjutan Nama File : Table.php

function findAll(){
include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'Select.php';
$sql = "SELECT * FROM `".$this->_tableName."`";
return new Select($sql);
}

function findBy($field, $value){


include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'Select.php';
$sql = "SELECT * FROM `".$this->_tableName."`";
$sql .=" WHERE `".
$field."`='".mysql_real_escape_string($value,Connect::getConnection())."'";
return new Select($sql);
}
}
?>
<?php Nama File : Select.php
include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'Connect1.php';
class Select implements Iterator{
protected $_query;
protected $_sql;
protected $_pointer = 0;
protected $_numResult = 0;
protected $_results = array();
function __construct($sql){
$this->_sql = $sql;
}
function rewind(){
$this->_pointer = 0;
}
function key(){
return $this->_pointer;
}
protected function _getQuery(){
if(!$this->_query){
$connection = Connect::getConnection();
$this->_query = mysql_query($this->_sql, $connection);
if(!$this->_query){
throw new Exception('Gagal membaca data dari database:'.mysql_error());
}
}
return $this->_query;
}
protected function _getNumResult(){ Lanjutan Nama File : Select.php
if(!$this->_numResult){
$this->_numResult = mysql_num_rows($this->_getQuery());
}
return $this->_numResult;
}
function valid(){
if($this->_pointer >= 0 && $this->_pointer < $this->_getNumResult()){
return true;
}
return false;
}
protected function _getRow($pointer){
if(isset($this->_results[$pointer])){
return $this->_results[$pointer];
}
$row = mysql_fetch_object($this->_getQuery());
if($row){
$this->_results[$pointer] = $row;
}
return $row;
}
Lanjutan Nama File : Select.php

function next(){
$row = $this->_getRow($this->_pointer);
if($row){
$this->_pointer ++;
}
return $row;
}
function current(){
return $this->_getRow($this->_pointer);
}

function close(){
mysql_free_result($this->_getQuery());
Connect::close();
}
}
?>
Script Tabel User : Nama Tabel user
CREATE TABLE IF NOT EXISTS `user` (
`Id_user` int(11) NOT NULL auto_increment,
`Username` char(45) NOT NULL,
`Password` char(15) default NULL,
`Level` char(15) default NULL,
`email` char(35) default NULL,
PRIMARY KEY (`Id_user`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

INSERT INTO `user` (`Id_user`, `Username`, `Password`, `Level`, `email`)


VALUES
(1, 'Administrator', 'soe123', 'a', 'soe@upiyptk.org'),
(2, 'widya', '123456', 'user', 'widya@upiyptk.org'),
(3, 'danny', '123456', 'user', 'danny@upiyptk.org'),
(4, 'debie', '123456', 'user', 'debie@upiyptk.org'),
(5, 'cubo', '123', '2', 'cubo@gmail.co.id');
Nama File : Contoh-1.php

<?
include 'Table.php';
$table = new Table('user'); // user adalah nama tabel
$table->connect();
$table->save(array(
'user_id' => 'admin',
'nama' => 'Admin',
'password' => '123456',
'tipe' => 'USER'
));
$table->close();
?>
Nama File : Contoh-2.php

<?php
include 'Table.php';
$tabel = new Table('user');
$tabel->connect();
$data = array(
'nama' => 'Administrator',
'password' => 'soe165upi'
);
$tabel->update($data, "user_id='admin'");
$tabel->close();
?>
Nama File : Contoh-3.php

<?
include 'Table.php';
$tabel = new Table('user');
$tabel->connect();
$table->delete("user_id='admin'");
$table->close();
?>
Nama File : Contoh-4.php

<?
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("test", $conn);
$sql = "select * from `user`";
$rs = mysql_query($sql);
while($user = mysql_fetch_object($rs)){
echo $user->nama.'-'.$user->user_id.'-'.$user->password.'-'.$user->tipe;
}
?>
Nama File : Contoh-5.php
<?php
include_once 'Table.php';

$table = new Table('user');


$users = $table->findAll();
?>
<table border="1">
<tr>
<th>User Id</th>
<th>Name</th>
<th>Password</th>
<th>Type</th>
<th>Action</th>
</tr>
<?php foreach($users as $user){?>
<tr>
<td><?php echo $user->user_id;?>
<td><?php echo $user->nama;?></td>
<td><?php echo $user->password?></td>
<td><?php echo $user->tipe?></td>
</tr>
<?php }?>
</table>
Script Tabel Wisata : Nama Tabel wisata
CREATE TABLE IF NOT EXISTS `wisata` (
`Id_wisata` int(11) NOT NULL auto_increment,
`nama_wisata` char(45) NOT NULL,
`alamat` char(15) default NULL,
`lat` text,
`lng` char(35) default NULL,
`gambar` char(35) default NULL,
PRIMARY KEY (`Id_wisata`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

INSERT INTO `wisata` (`Id_wisata`, `nama_wisata`, `alamat`, `lat`, `lng`,


`gambar`) VALUES
(1, 'Pantai Padang', 'Padang', 'se', 'Indonesia', '-'),
(2, 'Maling Kundang', 'Teluk Bayur', 'test', 'Indonesia', '-');
Nama File : Index.php
<?php
include_once 'Table.php';
$table = new Table('wisata');
$wisatas = $table->findAll();
?>
<br><br>
<table width="100%" border="1" style="border-collapse:collapse">
<tr><th colspan=7>DAFTAR WISATA KOTA PADANG</th></tr>
<tr><th colspan=7><a href=“Add.php">Add</a></th></tr>
<tr>
<th>id wisata</th>
<th>nama wisata</th>
<th>alamat</th>
<th>lat</th>
<th>lng</th>
<th>gambar</th>
<th>Action</th>
</tr>
Lanjutan Nama File : Index.php

<?php foreach($wisatas as $wisata){?>


<tr>
<td><?php echo $wisata->id_wisata;?>
<td><?php echo $wisata->nama_wisata;?></td>
<td><?php echo $wisata->alamat?></td>
<td><?php echo $wisata->lat?></td>
<td><?php echo $wisata->lng?></td>
<td><?php echo $wisata->gambar?></td>
<td>
<a href=“Edit.php?id_wisata=<?php echo $wisata->id_wisata?>">Edit</a>
<a href=“Delete.php?id_wisata=<?php echo $wisata->id_wisata?>">Delete</a>
</td>
</tr>
<?php }?>
</table>
<?php
if($_POST){ Nama File : Add.php
include_once 'Table.php';
$wisata = new Table('wisata');
try{
$wisata->save(array(
'nama_wisata' => $_POST['nama_wisata'],
'alamat' => $_POST['alamat'],
'lat' => $_POST['lat'],
'lng' => $_POST['lng'],
'gambar' => $_POST['gambar']
));
header("Location: Index.php");
exit;
}catch(Exception $e){
echo 'Gagal Menyimpan Wisata';
echo '<br/>Error: '.$e->getMessage();
}
}
?>
<form action="" method="post">
Nama Wisata: <input type="text" name="nama_wisata"/><br/>
alamat: <input type="text" name="alamat"/><br/>
lat: <input type="text" name="lat"/><br/>
lng: <input type="text" name="lng"/><br/>
Gambar: <input type="text" name="gambar"/><br/>
<input type="submit" value="Save"/>
</form>
<?php Nama File : Edit.php
include_once 'Table.php';
$wisata = new Table('wisata');
if($_POST){
try{
$data = array(
'nama_wisata' => $_POST['nama_wisata'],
'alamat' => $_POST['alamat'],
'lat' => $_POST['lat'],
'lng' => $_POST['lng'],
'gambar' => $_POST['gambar']
);

$wisata->update($data,"id_wisata='$_POST[id_wisata]'");
header("Location: Index.php");
exit;
}catch(Exception $e){
echo 'Gagal Mengedit data Wisata';
echo '<br/>Error: '.$e->getMessage();
}
}
$currentWisata = $wisata->findBy('id_wisata', $_GET['id_wisata']);
$currentWisata = $currentWisata->current();
?>
Lanjutan Nama File : Edit.php

<form action="" method="post">


Id Wisata: <input type="text" name="id_wisata" value="<?php echo
$currentWisata->id_wisata?>" readonly="readonly"/><br/>
Nama Wisata: <input type="text" name="nama_wisata" value="<?php echo
$currentWisata->nama_wisata?>"/><br/>
alamat: <input type="text" name="alamat" value="<?php echo $currentWisata-
>alamat?>"/><br/>
lat: <input type="text" name="lat" value="<?php echo $currentWisata->lat?>"/><br/>
lng: <input type="text" name="lng" value="<?php echo
$currentWisata->lng?>"/><br/>
Gambar: <input type="text" name="gambar" value="<?php echo $currentWisata-
>gambar?>"/><br/>
<input type="submit" value="Update"/>
</form>
<?php Nama File : Delete.php
include_once 'Table.php';
$wisata = new Table('wisata');
if($_POST){
try{
$data = array(
'id_wisata' => $_POST['id_wisata'],
'nama_wisata' => $_POST['nama_wisata'],
'alamat' => $_POST['alamat'],
'lat' => $_POST['lat'],
'lng' => $_POST['lng'],
'gambar' => $_POST['gambar']
);
//$tabel->update($data, "user_id='admin'");
$wisata->delete("id_wisata='$id_wisata'");
header("Location: Index.php");
exit;
}catch(Exception $e){
echo 'Gagal Menghapus data Wisata';
echo '<br/>Error: '.$e->getMessage();
}
}
Lanjutan Nama File : Delete.php

$currentWisata = $wisata->findBy('id_wisata', $_GET['id_wisata']);


$currentWisata = $currentWisata->current();
?>

<form action="" method="post">


Id Wisata: <input type="text" name="id_wisata" value="<?php echo $currentWisata-
>id_wisata?>" readonly="readonly"/><br/>
Nama Wisata: <input type="text" name="nama_wisata" value="<?php echo $currentWisata-
>nama_wisata?>"/><br/>
alamat: <input type="text" name="alamat" value="<?php echo
$currentWisata->alamat?>"/><br/>
lat: <input type="text" name="lat" value="<?php echo $currentWisata->lat?>"/><br/>
lng: <input type="text" name="lng" value="<?php echo $currentWisata->lng?>"/><br/>
Gambar: <input type="text" name="gambar" value="<?php echo $currentWisata->gambar?
>"/><br/>
<input type="submit" value="Delete"/>
</form>
HASIL EKSEKUSI PROGRAM

Anda mungkin juga menyukai