TEKNOLOGI WEB
MODUL 6
Disusun oleh :
DSE – A
2023
DAFTAR ISI
DAFTAR ISI............................................................................................................................... i
i
BAB I HASIL PRAKTIKUM
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
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.
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);
}
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);
}
5
}
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
7
Gambar 3 - Proses Update Edit 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);
}
9
}
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"));
function deleteCity($id){
$this->db->where("ID",$id);
return $this->db->delete("City");
}
}
?>
b. Screenshoot
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
13
$data['language'] = $this -> LanguageModel->
getLanguage();
$data['country'] = $this -> CountryModel
->getCountryByCode($code) -> row();
$this -> load ->view("negara_update",$data);
}
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
17
Gambar 9 - Edit Data Negara "aruba"
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