Anda di halaman 1dari 22

LAPORAN PRAKTIKUM

TEKNOLOGI WEB

MODUL 6

QUERY BUILDER CODEIGNITER DAN CRUD

Disusun oleh :

Indri Febriana Putri 3411201021

DSE – A

PROGRAM STUDI INFORMATIKA

FAKULTAS SAINS DAN INFORMATIKA

UNIVERSITAS JENDRAL ACHMAD YANI

2023
DAFTAR ISI

DAFTAR ISI............................................................................................................................... i

BAB I HASIL PRAKTIKUM ................................................................................................... 1

I.1. Latihan 1: Tambah Data................................................................................................... 1

I.2. Latihan 2: Edit Data ......................................................................................................... 3

I.3. Latihan 3: Hapus Data ..................................................................................................... 9

BAB II TUGAS PRAKTIKUM............................................................................................... 13

II.1. CRUD Daftar Negara ................................................................................................... 13

BAB III KESIMPULAN.......................................................................................................... 20

i
BAB I HASIL PRAKTIKUM

I.1. Latihan 1: Tambah Data


a. Source Code
Controller: City.php
<?php
class City extends CI_Controller{
public function index(){
$this -> load -> library("table");
$this -> load -> model('CityModel','','TRUE');
$data['city'] = $this -> CityModel -> getCity();
$this -> load -> view("city",$data);
}

public function __construct(){


parent::__construct();
$this -> load -> model("CityModel","",TRUE);
}

public function tambah(){


$this -> load -> model('CountryModel');
$data['country'] = $this -> CountryModel ->
getCountry();
$this -> load -> view("kota_tambah",$data);
}
}
?>

View: kota_tambah.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>Tambah Kota</title>
<style>
label {
display: inline-block;
width: 100px;

1
}
</style>
</head>
<body>
<h1>Tambah Kota</h1>
<form action="<?php
site_url('city/prosesTambah'); ?>"method="post">
<label>Nama</label><input type="text" name="name"><br>
<label>Code Negara</label><Select name="code">
<?php
foreach ($country -> result() as $ctr) {
echo '<option value="'.$ctr -> Code.'">'.$ctr ->
Code.'</option>';
}

?></Select><br>
<label>District</label><input type="text"name="area"><br>
<label>Populasi</label><input type="text"name="populasi"><br>
<input type="submit" value="Tambah">
</form>
</body>
</html>

Model: CityModel.php
<?php
class CityModel extends CI_Model{
function getCity(){
return $this -> db -> get("city");
}

function insertCity(){
$city = array("Name" => $this -> input ->
post("name"), "CountryCode" => $this -> input ->
post("code"),"District" => $this -> input -> post("area"),
"Population" => $this -> input -> post("populasi"));
return $this -> db -> insert('City',$city);
}
}
?>

2
b. Screenshoot

Gambar 1 - Tambah Data City

c. Analisis
Pada latihan 1 ini merupakan proses untuk menambahkan data pada tabel city.
Sebelumnya tambahkan $autoload['helper'] = array('url'); pada file autoload. Pada
bagian controller yaitu City.php menambahkan function baru berupa function
__construct() berfungsi untuk melakukan autoload untuk model city pada class city,
function tambah() berfungsi untuk menambahkan data pada tabel city, serta function
prosesTambah() berfungsi untuk melakukan proses penambahan data. Pada bagian
view yaitu program kota_tambah.php merupakan 4 program berupa form untuk
menambahkan data yang berisi nama, code negara, district dan populasi. Pada bagian
model yaitu program CityModel.php tambahkan function insertCity() yang berfungsi
untuk pembuatan intruksi tambah data dengan menggunakan query bilder.

I.2. Latihan 2: Edit Data


a. Source Code

View: City.php

<html>
<head>
<title>Daftar Kota</title>
</head>
<body>
<h1>Daftar Kota</h1>
<?php

3
$template = array('table_open' => '<table border ="1">'
);
$this -> table -> set_template($template);
$this -> table ->
set_heading("Nama","Negara","Populasi","Aksi");
foreach($city -> result() as $r){
$edit = '<a href="'.site_url("city/update/".$r
->ID).'">edit</a>';
$hapus = '<a href="'.site_url("city/hapus/".$r
->ID).'">hapus</a>';
$this -> table -> add_row($r -> Name, $r ->CountryCode, $r ->
Population, $edit."".$hapus);
}

echo $this -> table -> generate();


?>6
</body>
</html>

Model: CityModel.php

<?php
class CityModel extends CI_Model{
function getCity(){
return $this -> db -> get("city");
}

function insertCity(){
$city = array("Name" => $this -> input ->
post("name"),"CountryCode" => $this -> input
->post("code"),"District" => $this -> input
->post("area"),"Population" => $this -> input -> post("populasi"));
return $this -> db -> insert('City',$city);
}

function getCityById($id){
$this -> db -> where("ID",$id);
return $this -> db -> get('City');
}

4
function updateCity($id){
$city = array("Name" => $this -> input ->
post("name"),"CountryCode" => $this -> input -> post("code"),
"District" => $this -> input -> post("area"), "Population" => $this
-> input -> post("populasi"));
$this -> db -> where("ID",$id);
return $this -> db -> update("City",$city);
}
}
?>

Controller: City.php

<?php
class City extends CI_Controller{
public function index(){
$this -> load -> library("table");
$this -> load -> model('CityModel','','TRUE');
$data['city'] = $this -> CityModel -> getCity();
$this -> load -> view("city",$data);
}

public function __construct(){


parent::__construct();
$this -> load -> model("CityModel","",TRUE);
}

public function tambah(){


$this -> load -> model('CountryModel');
$data['country'] = $this -> CountryModel ->
getCountry();
$this -> load -> view("kota_tambah",$data);
}

public function prosesTambah(){


if($this -> CityModel -> insertCity()){
redirect(site_url("city"));
}else{
redirect(site_url("city/tambah"));
}

5
}

public function update($id){


$this -> load -> model('CountryModel');
$data['country'] = $this -> CountryModel ->
getCountry();
$data['city'] = $this -> CityModel -> getCityById($id)
-> row();
$this -> load -> view("kota_update",$data);
}

public function prosesUpdate($id){


if($this -> CityModel -> updateCity($id)){
redirect(site_url("city"));
}else{
redirect(site_url("city/update/$id"));
}
}
}
?>

Views: kota_update.php

<!DOCTYPE html>
<html lang="en">
<head>
<title>Update Kota</title>
<style>
label {
display: inline-block;
width: 100px;
}
</style>
</head>
<body>
<h1>Update Kota</h1>
<form action="<?php echo
site_url('City/prosesupdate/'.$city->ID);
?>" method="post">

6
<label>Nama</label><input type="text" name=" name"value="<?php echo
$city -> Name; ?>"><br>
<label>Code Negara</label><Select name="code" value="<?php echo
$city -> CountryCode; ?>">
<?php
foreach ($city -> result() as $ctr) {
$selected = "";
if($city -> CountryCode == $ctr -> Code){
$selected = "selected";
}
echo '<option value="'.$ctr -> Code.'"'.$selected.'>'.$ctr ->
Code.'</option>';
}
?>
</Select><br>
<label>District</label><input type="text" name="area" value="
<?php echo $city->District; ?>"><br>
<label>Populasi</label><input type="text"name="populasi" value="
<?php echo $city->Population; ?>"><br>
<input type="submit" value="Update">
</form>
</body>
</html>

b. Screenshoot

Gambar 2 - Sebelum Edit Data City

7
Gambar 3 - Proses Update Edit Data City

Gambar 4 - Setelah Update Data City

c. Analisis

Pada latihan 2 yaitu merupakan proses untuk melakukan update data. Didalam model
yaitu program CityModel.php tambahkan function getCityById($id) yang berfungsi
untuk menampilkan data yang dipilih berdasarkan primary keynya dan function
updateCity($id) yang berfungsi untuk menyimpan data yang telah diinputkan kedalam
database. Kemudian pada Controller yaitu program City.php tambahkan function
update($id) yang berfungsi untuk melakukan update data, dan function
prosesUpdate($id) berfungsi untuk melakukan proses perubahan pada data yang telah
dimasukan.

8
I.3. Latihan 3: Hapus Data
a. Source Code

Controller : City.php

<?php
class City extends CI_Controller{
public function index(){
$this -> load -> library("table");
$this -> load -> model('CityModel','','TRUE');
$data['city'] = $this -> CityModel -> getCity();
$this -> load -> view("city",$data);
}

public function __construct(){


parent::__construct();
$this -> load -> model("CityModel","",TRUE);
}

public function tambah(){


$this -> load -> model('CountryModel');
$data['country'] = $this -> CountryModel ->
getCountry();
$this -> load -> view("kota_tambah",$data);
}

public function prosesTambah(){


if($this -> CityModel -> insertCity()){
redirect(site_url("city"));
}else{
redirect(site_url("city/tambah"));
}
}

public function update($id){


$this -> load -> model('CountryModel');
$data['country'] = $this -> CountryModel ->
getCountry();
$data['city'] = $this -> CityModel ->getCityById($id)
-> row();
$this -> load -> view("kota_update",$data);

9
}

public function prosesUpdate($id){


if($this -> CityModel -> updateCity($id)){
redirect(site_url("city"));
}else{
redirect(site_url("city/update/$id"));
}
}

public function hapus($id){


$this->CityModel->deleteCity($id);
redirect(site_url("city"));
}
}
?>

Model : CityModel.php

<?php
class CityModel extends CI_Model{
function getCity(){
return $this -> db -> get("city");
}

function insertCity(){
$city = array("Name" => $this -> input ->
post("name"),"CountryCode" => $this -> input
->post("code"),"District" => $this -> input
->post("area"),"Population" => $this -> input ->post("populasi"));
return $this -> db -> insert('City',$city);
}

function getCityById($id){
$this -> db -> where("ID",$id);
return $this -> db -> get('City');
}

function updateCity($id){

10
$city = array("Name" => $this -> input ->
post("name"),"CountryCode" => $this -> input
->post("code"),"District" => $this -> input
->post("area"),"Population" => $this -> input ->post("populasi"));

$this -> db -> where("ID",$id);


return $this -> db -> update("City",$city);
}

function deleteCity($id){
$this->db->where("ID",$id);
return $this->db->delete("City");
}
}
?>

b. Screenshoot

Gambar 5 - Hapus Data City

Gambar 6 - Data City "Kabul" Telah Terhapus

11
c. Analisis

Pada latihan 3 merupakan proses untuk menghapus data pada tabel city. Didalam Model
yaitu CityModel.php tambahkan funciton deleteCity($id) yang berfungsi untuk
menghapus data pada tabel city yang dipilih. Kemudian pada Controller yaitu City.php
tambahkan funciton hapus($id) yang berfungsi untuk memanggil function
deleteCity($id) pada saat button hapus diklik.

12
BAB II TUGAS PRAKTIKUM

II.1. CRUD Daftar Negara


a. Source Code
Controller : Country.php
<?php
class Country extends CI_Controller{
public function index(){
$this -> load -> library("table");
$this -> load ->model('CountryModel','','TRUE');
$data['country'] = $this -> CountryModel
->getCountry();
$this -> load -> view("country",$data);
}

public function __construct(){


parent::__construct();
$this -> load ->model("CountryModel","",TRUE);
}

public function tambah(){


$this -> load -> model('CountryModel');
$data['country'] = $this -> CountryModel
->getCountry();
$this -> load ->view("negara_tambah",$data);
}

public function prosesTambah(){


if($this -> CountryModel ->insertCountry()){
redirect(site_url("country"));
}else{
redirect(site_url("country/tambah"));
}
}

public function update($code){


$this -> load -> model('CityModel');
$data['city'] = $this -> CityModel ->getCity();
$this -> load -> model('LanguageModel');

13
$data['language'] = $this -> LanguageModel->
getLanguage();
$data['country'] = $this -> CountryModel
->getCountryByCode($code) -> row();
$this -> load ->view("negara_update",$data);
}

public function prosesUpdate($code){


if($this -> CountryModel ->updateCountry($code)){
redirect(site_url("country"));
}else{
redirect(site_url("country/update/$code"));
}
}

public function hapus($code){


$this -> CountryModel ->deleteCountry($code);
redirect(site_url("country"));
}
}
?>

Model : CountryModel.php
<?php
class CountryModel extends CI_Model{
function getCountry(){
return $this -> db -> get("country");
}

function insertCountry(){
$country = array("Code" => $this -> input
->post("code"),"Name" => $this -> input ->post("nama"));
return $this -> db ->insert('Country',$country);
}

function getCountryByCode($code){
$this -> db -> where("Code",$code);
return $this -> db -> get('Country');
}

14
function updateCountry($code){
$country = array("Code" => $this -> input
->post("code"),"Name" => $this -> input ->post("nama"));
$this -> db -> where("Code",$code);
return $this -> db ->update("Country",$country);
}

function deleteCountry($code){
$this -> db -> where("Code",$code);
return $this -> db ->delete("Country");
}
}
?>

Views : country.php
<!DOCTYPE html>
<html>
<head>
<title>Daftar Negara</title>
</head>
<body>
<h1>Daftar Negara</h1>
<?php
$template = array('table_open' => '<table border="1">');
$this->table->set_template($template);
$this->table->set_heading("Code","Name","Aksi");
foreach ($country->result() as $r) {
$edit = '<a
href="'.site_url("country/update/".$r->Code).'">edit</a>';
$hapus = '<a
href="'.site_url("country/hapus/".$r->Code).'">hapus</a>';
$this->table->add_row($r->Code, $r->Name,
$edit."".$hapus); }

echo $this->table->generate();
?>
</body>
</html>

15
Views : negara_tambah.php

<!DOCTYPE html>
<html lang="en">
<head>
<title>Tambah Negara</title>
<style>
label {
display: inline-block;
width: 100px;
}
</style>
</head>
<body>
<h1>Tambah Negara</h1>
<form action="<?php
site_url('country/prosesTambah'); ?>" method="post">
<label>Code</label><input type="text"name="code"><br>
<label>Name</label><input type="text"name="nama"><br>
<input type="submit" value="Tambah">
</form>
</body>
</html>

Views : negara_update.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>Update Negara</title>
<style>
label {
display: inline-block;
width: 100px;
}
</style>
</head>
<body>
<h1>Update Negara</h1>
<form action="<?php echo
site_url('Country/prosesUpdate/'.$country->Code);

16
?>" method="post">
<label>Code</label><input type="text" name="code"value="<?php
echo $country -> Code; ?>"><br>
<label>Name</label><input type="text" name="nama"value="<?php
echo $country -> Name; ?>"><br>
<input type="submit" value="Update">
</form>
</body>
</html>

b. Screenshoot

Gambar 7 - Tambah Data Negara

Gambar 8 -Tampil Data Negara

17
Gambar 9 - Edit Data Negara "aruba"

Gambar 10 - Proses Update Edit Data Negara "aruba"

Gambar 11 - Hasil Edit Data Negara "arubaaaa"

Gambar 12 - Hapus Data Negara "arubaaaa"

18
c. Analisis
Program diatas merupakan proses untuk melakukan CRUD pada tabel Country. Untuk
melakukan proses tambah data membutuhkan function tambah() dan function
prosesTambah() yang disimpan didalam program Country.php (Controller), kemudian
membutuhkan funciton insertCountry() yang disimpan didalam program
CountryModel.php (Model), serta negara_tambah.php yang digunakan sebagai form
untuk menambahkan data. Untuk melakukan proses edit dibutuhkan function update()
dan function prosesUpdate() pada Country.php (Controller), penambahahan function
getCountryByCode() dan function updateCountry() pada CountryModel.php (Model),
pembuatan program negara_update.php yang digunakan untuk melakukan perubahan
data, serta penambahan beberapa script pada city.php (Views) yang digunakan untuk
menampilkan button hapus dan edit. Untuk melakukan proses hapus tambahkan
function hapus() pada Country.php (Controller), tambahkan funciton deleteCountry()
pada CountryModel.php (Model).

19
BAB III KESIMPULAN

Pada praktikum pertemuan 6 model 6 ini membahas mengenai Query Builder Codeigniter dan
CRUD. CRUD atau sebuah proses create, read, update dan delete merupakan bentuk fungsi
yang biasa digunakan dalam suatu aplikasi dengan menggunakan database. Query Builder
merupakan suatu class dalam codeigniter yang membantu mempermudah dalam penggunaan
database, yaitu seperti menampilkan, menambah, melakukan perubahan data dan menghapus
data dengan menggunakan code yang lebih sedikit.

20

Anda mungkin juga menyukai