MEMBUAT CRUD DENGAN CODEIGNITER : INPUT DATA KE DATABASE
MEMBUAT CRUD DENGAN CODEIGNITER : INPUT DATA KE DATABASE
CODEIGNITER ADALAH SEBUAH WEB FRAMEWORK YANG DIKEMBANGKAN DENGAN BAHASA PEMROGRAMMAN PHP, CODEIGNITER SENDIRI MERUPAKAN FRAMEWORK YANG SANGAT POPULER SEKALI DI INDONESIA, BANYAK DEVELOPER / PROGRAMMER MENGEMBANGKAN APLIKASI MAUPUN WEBSITENYA MENGGUNAKAN FRAMWORK INI, CODEIGNITER JUGA DIKENAL SEBAGAI FRAMEWORK BERUKURAN KECIL DAN MEMILIKI KECEPATAN YANG DIBILANG SANGAT CEPAT.
CODEIGNITER MENGGUNAKAN KONSEP MVC (MODEL-VIEW-CONTROLLER) ADALAH SEBUAH METODE UNTUK MEMBUAT SEBUAH APLIKASI DENGAN MEMISAHKAN DATA (MODEL) DARI TAMPILAN (VIEW) DAN CARA BAGAIMANA MEMPROSESNYA (CONTROLLER).
STEP 1
OKE LANGSUNG SAJA KITA MULAI, SILAHKAN DOWNLOAD CODEIGNITER DI SITUS RESMINYA WWW.CODEIGNITER.COM ATAU BISA KE REPOSITORINYA HTTPS://GITHUB.COM/BCIT-CI/CODEIGNITER. SETELAH BERHASIL DIDOWNLOAD, SILAHKAN EXTRACT DAN COPYKAN KE HTDOCS (JIKA MENGGUNAKAN XAMPP) TEMAN-TEMAN.
STEP 2
KONFIGURASI APLIKASI
OKE UNTUK PERTAMA KITA AKAN MENKONFIGURASI APLIKASI KITA SEPERTI, MENGATUR ALAMAT APLIKASI(BASE_URL)
, DATABASE
, AUTOLOAD
, DLL.
LANGSUNG SAJA BUKA FILE DATABASE.PHP
DI APPLICATION/CONFIG/DATABASE.PHP
, UBAH MENJADI SEPERTI INI:
$ACTIVE_GROUP = 'DEFAULT';
$QUERY_BUILDER = TRUE;
$DB['DEFAULT'] = ARRAY(
'DSN' => '',
'HOSTNAME' => 'LOCALHOST',
'USERNAME' => 'ROOT',
'PASSWORD' => '', //SESUAIKAN PASSWORD PHPMYADMIN TEMAN-TEMAN
'DATABASE' => 'CI_CRUD',
'DBDRIVER' => 'MYSQLI',
'DBPREFIX' => '',
'PCONNECT' => FALSE,
'DB_DEBUG' => (ENVIRONMENT !== 'PRODUCTION'),
'CACHE_ON' => FALSE,
'CACHEDIR' => '',
'CHAR_SET' => 'UTF8',
'DBCOLLAT' => 'UTF8_GENERAL_CI',
'SWAP_PRE' => '',
'ENCRYPT' => FALSE,
'COMPRESS' => FALSE,
'STRICTON' => FALSE,
'FAILOVER' => ARRAY(),
'SAVE_QUERIES' => TRUE
);
SELANJUTNYA BUKA FILE AUTOLOAD.PHP
DI FOLDER APPLICATION/CONFIG
DAN UBAH BAGIAN KODE BERIKUT INI:
// CONFIG AUTOLOAD LIBRARIES
$AUTOLOAD['LIBRARIES'] = ARRAY('');
MENJADI SEPERTI INI:
$AUTOLOAD['LIBRARIES'] = ARRAY('DATABASE', 'SESSION');
//CONFIG AUTOLOAD HELPER
$AUTOLOAD['HELPER'] = ARRAY('');
MENJADI SEPERTI INI:
$AUTOLOAD['HELPER'] = ARRAY('URL', 'FORM');
SELANJUTNYA BUKA FILE CONFIG.PHP
DI APPLICATION/CONFIG/CONFIG.PHP
, UBAH BAGIAN KODE-KODE DIBAWAH INI:
$CONFIG['BASE_URL'] = "";
MENJADI SEPERTI BERIKUT INI
$CONFIG['BASE_URL'] = "HTTP://".$_SERVER['HTTP_HOST'].
STR_REPLACE(BASENAME($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$CONFIG['ENCRYPTION_KEY'] = '';
MENJADI SEPERTI BERIKUT INI
$CONFIG['ENCRYPTION_KEY'] = 'CODEIGNITERCRUDBASIC';
$CONFIG['CSRF_PROTECTION'] = FALSE;
$CONFIG['CSRF_TOKEN_NAME'] = 'CSRF_TOKEN_NAME';
$CONFIG['CSRF_COOKIE_NAME'] = 'CSRF_COOKIE_NAME';
$CONFIG['CSRF_EXPIRE'] = 7200;
$CONFIG['CSRF_REGENERATE'] = TRUE;
$CONFIG['CSRF_EXCLUDE_URIS'] = ARRAY();
MENJADI SEPERTI BERIKUT INI
$CONFIG['CSRF_PROTECTION'] = TRUE;
$CONFIG['CSRF_TOKEN_NAME'] = 'CSRF_TOKEN';
$CONFIG['CSRF_COOKIE_NAME'] = 'CSRF_COOKIE';
$CONFIG['CSRF_EXPIRE'] = 7200;
$CONFIG['CSRF_REGENERATE'] = TRUE;
$CONFIG['CSRF_EXCLUDE_URIS'] = ARRAY();
SELANJUTNYA, BUATLAH SEBUAH DATABASE BARU DI, HTTP://LOCALHOST/PHPMYADMIN, BERI NAMA DATABASENYA DENGAN CI_CRUD
KEMUDIAN IMPORT SCRIPT BERIKUT INI KEDALAM MENU SQL.
-- PHPMYADMIN SQL DUMP
-- VERSION 4.8.0
-- HTTPS://WWW.PHPMYADMIN.NET/
--
-- HOST: LOCALHOST
-- GENERATION TIME: MAY 17, 2018 AT 05:35 AM
-- SERVER VERSION: 5.7.9
-- PHP VERSION: 7.2.2
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET TIME_ZONE = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES UTF8MB4 */;
--
-- DATABASE: `CI_CRUD`
--
-- --------------------------------------------------------
--
-- TABLE STRUCTURE FOR TABLE `TBL_BUKU`
--
CREATE TABLE `TBL_BUKU` (
`ID_BUKU` INT(11) NOT NULL,
`NO_ISBN` VARCHAR(100) NOT NULL,
`NAMA_BUKU` VARCHAR(100) NOT NULL,
`TANGGAL_TERBIT` DATE NOT NULL,
`PENGARANG` VARCHAR(100) NOT NULL
) ENGINE=INNODB DEFAULT CHARSET=LATIN1;
--
-- DUMPING DATA FOR TABLE `TBL_BUKU`
--
INSERT INTO `TBL_BUKU` (`ID_BUKU`, `NO_ISBN`, `NAMA_BUKU`, `TANGGAL_TERBIT`, `PENGARANG`) VALUES
(2, '0002349876', 'PANDAI FRAMEWORK CODEIGNITER 1 MINGGU', '2018-05-22', 'FIKA RIDAUL MAULAYYA'),
(4, '0002349444', 'JAGO JAVASCRIPT', '2018-05-24', 'RIZQI MAULANA'),
(5, '0002343909', 'MENGUASAI FRAMEWORK RUBY ON RAILS', '2018-05-26', 'CAHYADI TRIYANSYAH'),
(6, '0002349097', 'MENDALAMI VUE JS ', '2018-02-19', 'YUDI PURWANTO');
--
-- INDEXES FOR DUMPED TABLES
--
--
-- INDEXES FOR TABLE `TBL_BUKU`
--
ALTER TABLE `TBL_BUKU`
ADD PRIMARY KEY (`ID_BUKU`);
--
-- AUTO_INCREMENT FOR DUMPED TABLES
--
--
-- AUTO_INCREMENT FOR TABLE `TBL_BUKU`
--
ALTER TABLE `TBL_BUKU`
MODIFY `ID_BUKU` INT(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7;
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
STEP 3
MEMBUAT APLIKASI
SELANJUTNYA KITA BUAT SEBUAH CONTROLLER BARU DI FOLDER APPLICATION/CONTROLLERS
, SIMPAN DENGAN NAMA BUKU.PHP
, KEMUDIAN MASUKKAN KODE BERIKUT INI:
<?PHP
DEFINED('BASEPATH') OR EXIT('NO DIRECT SCRIPT ACCESS ALLOWED');
CLASS BUKU EXTENDS CI_CONTROLLER {
PUBLIC FUNCTION __CONSTRUCT(){
PARENT ::__CONSTRUCT();
//LOAD MODEL
$THIS->LOAD->MODEL('MODEL_BUKU');
}
PUBLIC FUNCTION INDEX()
{
$DATA = ARRAY(
'TITLE' => 'DATA BUKU',
'DATA_BUKU' => $THIS->MODEL_BUKU->GET_ALL(),
);
$THIS->LOAD->VIEW('DATA_BUKU', $DATA);
}
PUBLIC FUNCTION TAMBAH()
{
$DATA = ARRAY(
'TITLE' => 'TAMBAH DATA BUKU'
);
$THIS->LOAD->VIEW('TAMBAH_BUKU', $DATA);
}
PUBLIC FUNCTION SIMPAN()
{
$DATA = ARRAY(
'NO_ISBN' => $THIS->INPUT->POST("NO_ISBN"),
'NAMA_BUKU' => $THIS->INPUT->POST("NAMA_BUKU"),
'TANGGAL_TERBIT' => $THIS->INPUT->POST("TANGGAL_TERBIT"),
'PENGARANG' => $THIS->INPUT->POST("PENGARANG"),
);
$THIS->MODEL_BUKU->SIMPAN($DATA);
$THIS->SESSION->SET_FLASHDATA('NOTIF', '<DIV CLASS="ALERT ALERT-SUCCESS ALERT-DISMISSIBLE"> SUCCESS! DATA BERHASIL DISIMPAN DIDATABASE.
</DIV>');
//REDIRECT
REDIRECT('BUKU/');
}
PUBLIC FUNCTION EDIT($ID_BUKU)
{
$ID_BUKU = $THIS->URI->SEGMENT(3);
$DATA = ARRAY(
'TITLE' => 'EDIT DATA BUKU',
'DATA_BUKU' => $THIS->MODEL_BUKU->EDIT($ID_BUKU)
);
$THIS->LOAD->VIEW('EDIT_BUKU', $DATA);
}
PUBLIC FUNCTION UPDATE()
{
$ID['ID_BUKU'] = $THIS->INPUT->POST("ID_BUKU");
$DATA = ARRAY(
'NO_ISBN' => $THIS->INPUT->POST("NO_ISBN"),
'NAMA_BUKU' => $THIS->INPUT->POST("NAMA_BUKU"),
'TANGGAL_TERBIT' => $THIS->INPUT->POST("TANGGAL_TERBIT"),
'PENGARANG' => $THIS->INPUT->POST("PENGARANG"),
);
$THIS->MODEL_BUKU->UPDATE($DATA, $ID);
$THIS->SESSION->SET_FLASHDATA('NOTIF', '<DIV CLASS="ALERT ALERT-SUCCESS ALERT-DISMISSIBLE"> SUCCESS! DATA BERHASIL DIUPDATE DIDATABASE.
</DIV>');
//REDIRECT
REDIRECT('BUKU/');
}
PUBLIC FUNCTION HAPUS($ID_BUKU)
{
$ID['ID_BUKU'] = $THIS->URI->SEGMENT(3);
$THIS->MODEL_BUKU->HAPUS($ID);
//REDIRECT
REDIRECT('BUKU/');
}
}
SELANJUTNYA KITA BUAT SEBUAH MODE BARU DI FOLDER APPLICATION/MODELS
, SIMPAN DENGAN NAMA MODEL_BUKU.PHP
, KEMUDIAN MASUKKAN KODE BERIKUT INI:
<?PHP
DEFINED('BASEPATH') OR EXIT('NO DIRECT SCRIPT ACCESS ALLOWED');
CLASS MODEL_BUKU EXTENDS CI_MODEL{
PUBLIC FUNCTION GET_ALL()
{
$QUERY = $THIS->DB->SELECT("*")
->FROM('TBL_BUKU')
->ORDER_BY('ID_BUKU', 'DESC')
->GET();
RETURN $QUERY->RESULT();
}
PUBLIC FUNCTION SIMPAN($DATA)
{
$QUERY = $THIS->DB->INSERT("TBL_BUKU", $DATA);
IF($QUERY){
RETURN TRUE;
}ELSE{
RETURN FALSE;
}
}
PUBLIC FUNCTION EDIT($ID_BUKU)
{
$QUERY = $THIS->DB->WHERE("ID_BUKU", $ID_BUKU)
->GET("TBL_BUKU");
IF($QUERY){
RETURN $QUERY->ROW();
}ELSE{
RETURN FALSE;
}
}
PUBLIC FUNCTION UPDATE($DATA, $ID)
{
$QUERY = $THIS->DB->UPDATE("TBL_BUKU", $DATA, $ID);
IF($QUERY){
RETURN TRUE;
}ELSE{
RETURN FALSE;
}
}
PUBLIC FUNCTION HAPUS($ID)
{
$QUERY = $THIS->DB->DELETE("TBL_BUKU", $ID);
IF($QUERY){
RETURN TRUE;
}ELSE{
RETURN FALSE;
}
}
}
SETELAH ITU KITA BUAT 3 BUAH FILE VIEW UNTUK TAMPILAN APLIKASI KITA DI FOLDER APPLICATION/VIEWS
, BUATLAH 3 FILE YAITU:
- DATA_BUKU.PHP
- TAMBAH_BUKU.PHP
- EDIT_BUKU.PHP
BUKA FILE DATA_BUKU.PHP
, KEMUDIAN MASUKKAN KODE BERIKUT INI:
<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE><?PHP ECHO $TITLE ?></TITLE>
<LINK REL="STYLESHEET" TYPE="TEXT/CSS" HREF="HTTPS://CDNJS.CLOUDFLARE.COM/AJAX/LIBS/TWITTER-BOOTSTRAP/4.1.1/CSS/BOOTSTRAP.MIN.CSS">
<LINK REL="STYLESHEET" TYPE="TEXT/CSS" HREF="HTTPS://CDN.DATATABLES.NET/1.10.16/CSS/DATATABLES.BOOTSTRAP4.MIN.CSS">
</HEAD>
<BODY>
<DIV CLASS="CONTAINER" STYLE="MARGIN-TOP: 80PX">
<?PHP ECHO $THIS->SESSION->FLASHDATA('NOTIF') ?>
<A HREF="<?PHP ECHO BASE_URL() ?>BUKU/TAMBAH/" CLASS="BTN BTN-MD BTN-SUCCESS">TAMBAH BUKU</A>
<HR>
<!-- TABLE -->
<DIV CLASS="TABLE-RESPONSIVE">
<TABLE ID="TABLE" CLASS="TABLE TABLE-STRIPED TABLE-BORDERED TABLE-HOVER">
<THEAD>
<TR>
<TH>NO.</TH>
<TH>NO ISBN</TH>
<TH>NAMA BUKU</TH>
<TH>TANGGAL TERBIT</TH>
<TH>PENGARANG</TH>
<TH>OPTIONS</TH>
</TR>
</THEAD>
<TBODY>
<?PHP
$NO = 1;
FOREACH($DATA_BUKU AS $HASIL){
?>
<TR>
<TD><?PHP ECHO $NO++ ?></TD>
<TD><?PHP ECHO $HASIL->NO_ISBN ?></TD>
<TD><?PHP ECHO $HASIL->NAMA_BUKU ?></TD>
<TD><?PHP ECHO $HASIL->TANGGAL_TERBIT ?></TD>
<TD><?PHP ECHO $HASIL->PENGARANG ?></TD>
<TD>
<A HREF="<?PHP ECHO BASE_URL() ?>BUKU/EDIT/<?PHP ECHO $HASIL->ID_BUKU ?>" CLASS="BTN BTN-SM BTN-SUCCESS">EDIT</A>
<A HREF="<?PHP ECHO BASE_URL() ?>BUKU/HAPUS/<?PHP ECHO $HASIL->ID_BUKU ?>" CLASS="BTN BTN-SM BTN-DANGER">HAPUS</A>
</TD>
</TR>
<?PHP } ?>
</TBODY>
</TABLE>
</DIV>
</DIV>
<SCRIPT TYPE="TEXT/JAVASCRIPT" SRC="HTTPS://CODE.JQUERY.COM/JQUERY-3.3.1.MIN.JS"></SCRIPT>
<SCRIPT TYPE="TEXT/JAVASCRIPT" SRC="HTTPS://CDNJS.CLOUDFLARE.COM/AJAX/LIBS/TWITTER-BOOTSTRAP/4.1.1/JS/BOOTSTRAP.MIN.JS"></SCRIPT>
<SCRIPT TYPE="TEXT/JAVASCRIPT" SRC="HTTPS://CDN.DATATABLES.NET/1.10.16/JS/JQUERY.DATATABLES.MIN.JS"></SCRIPT>
<SCRIPT TYPE="TEXT/JAVASCRIPT" SRC="HTTPS://CDN.DATATABLES.NET/1.10.16/JS/DATATABLES.BOOTSTRAP4.MIN.JS"></SCRIPT>
<SCRIPT>
$('#TABLE').DATATABLE( {
AUTOFILL: TRUE
} );
</SCRIPT>
</BODY>
</HTML>
APABILA APLIKASI DIJALANKAN DENGAN MENGETIKKAN ALAMAT, HTTP://LOCALHOST/FOLDER-PROJECT/INDEX.PHP/BUKU, KURANG LEBIH TAMPILANNYA AKAN SEPERTI INI:
SELANJUTNYA BUKA FILE TAMBAH_BUKU.PHP
, KEMUDIAN MASUKKAN KODE BERIKUT INI:
<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE><?PHP ECHO $TITLE ?></TITLE>
<LINK REL="STYLESHEET" TYPE="TEXT/CSS" HREF="HTTPS://CDNJS.CLOUDFLARE.COM/AJAX/LIBS/TWITTER-BOOTSTRAP/4.1.1/CSS/BOOTSTRAP.MIN.CSS">
</HEAD>
<BODY>
<DIV CLASS="CONTAINER" STYLE="MARGIN-TOP: 80PX">
<DIV CLASS="COL-MD-12">
<?PHP ECHO FORM_OPEN('BUKU/SIMPAN') ?>
<DIV CLASS="FORM-GROUP">
<LABEL FOR="TEXT">NO ISBN</LABEL>
<INPUT TYPE="TEXT" NAME="NO_ISBN" CLASS="FORM-CONTROL" PLACEHOLDER="MASUKKAN NO. ISBN">
</DIV>
<DIV CLASS="FORM-GROUP">
<LABEL FOR="TEXT">NAMA BUKU</LABEL>
<INPUT TYPE="TEXT" NAME="NAMA_BUKU" CLASS="FORM-CONTROL" PLACEHOLDER="MASUKKAN NAMA BUKU">
</DIV>
<DIV CLASS="FORM-GROUP">
<LABEL FOR="TEXT">TANGGAL TERBIT</LABEL>
<INPUT TYPE="DATE" NAME="TANGGAL_TERBIT" CLASS="FORM-CONTROL" >
</DIV>
<DIV CLASS="FORM-GROUP">
<LABEL FOR="TEXT">PENGARANG</LABEL>
<INPUT TYPE="TEXT" NAME="PENGARANG" CLASS="FORM-CONTROL" >
</DIV>
<BUTTON TYPE="SUBMIT" CLASS="BTN BTN-MD BTN-SUCCESS">SIMPAN</BUTTON>
<BUTTON TYPE="RESET" CLASS="BTN BTN-MD BTN-WARNING">RESET</BUTTON>
<?PHP ECHO FORM_CLOSE() ?>
</DIV>
</DIV>
<SCRIPT TYPE="TEXT/JAVASCRIPT" SRC="HTTPS://CODE.JQUERY.COM/JQUERY-3.3.1.MIN.JS"></SCRIPT>
<SCRIPT TYPE="TEXT/JAVASCRIPT" SRC="HTTPS://CDNJS.CLOUDFLARE.COM/AJAX/LIBS/TWITTER-BOOTSTRAP/4.1.1/JS/BOOTSTRAP.MIN.JS"></SCRIPT>
</BODY>
</HTML>
APABILA APLIKASI DIJALANKAN DENGAN MENGETIKKAN ALAMAT, HTTP://LOCALHOST/FOLDER-PROJECT/INDEX.PHP/BUKU/TAMBAH, KURANG LEBIH TAMPILANNYA AKAN SEPERTI INI:
SELANJUTNYA BUKA FILE EDIT_BUKU.PHP
, KEMUDIAN MASUKKAN KODE BERIKUT INI:
<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE><?PHP ECHO $TITLE ?></TITLE>
<LINK REL="STYLESHEET" TYPE="TEXT/CSS" HREF="HTTPS://CDNJS.CLOUDFLARE.COM/AJAX/LIBS/TWITTER-BOOTSTRAP/4.1.1/CSS/BOOTSTRAP.MIN.CSS">
</HEAD>
<BODY>
<DIV CLASS="CONTAINER" STYLE="MARGIN-TOP: 80PX">
<DIV CLASS="COL-MD-12">
<?PHP ECHO FORM_OPEN('BUKU/UPDATE') ?>
<DIV CLASS="FORM-GROUP">
<LABEL FOR="TEXT">NO ISBN</LABEL>
<INPUT TYPE="TEXT" NAME="NO_ISBN" VALUE="<?PHP ECHO $DATA_BUKU->NO_ISBN ?>" CLASS="FORM-CONTROL" PLACEHOLDER="MASUKKAN NO. ISBN">
<INPUT TYPE="HIDDEN" VALUE="<?PHP ECHO $DATA_BUKU->ID_BUKU ?>" NAME="ID_BUKU">
</DIV>
<DIV CLASS="FORM-GROUP">
<LABEL FOR="TEXT">NAMA BUKU</LABEL>
<INPUT TYPE="TEXT" NAME="NAMA_BUKU" VALUE="<?PHP ECHO $DATA_BUKU->NAMA_BUKU ?>" CLASS="FORM-CONTROL" PLACEHOLDER="MASUKKAN NAMA BUKU">
</DIV>
<DIV CLASS="FORM-GROUP">
<LABEL FOR="TEXT">TANGGAL TERBIT</LABEL>
<INPUT TYPE="DATE" NAME="TANGGAL_TERBIT" VALUE="<?PHP ECHO $DATA_BUKU->TANGGAL_TERBIT ?>" CLASS="FORM-CONTROL" >
</DIV>
<DIV CLASS="FORM-GROUP">
<LABEL FOR="TEXT">PENGARANG</LABEL>
<INPUT TYPE="TEXT" NAME="PENGARANG" VALUE="<?PHP ECHO $DATA_BUKU->PENGARANG ?>" CLASS="FORM-CONTROL" >
</DIV>
<BUTTON TYPE="SUBMIT" CLASS="BTN BTN-MD BTN-SUCCESS">UPDATE</BUTTON>
<BUTTON TYPE="RESET" CLASS="BTN BTN-MD BTN-WARNING">RESET</BUTTON>
<?PHP ECHO FORM_CLOSE() ?>
</DIV>
</DIV>
<SCRIPT TYPE="TEXT/JAVASCRIPT" SRC="HTTPS://CODE.JQUERY.COM/JQUERY-3.3.1.MIN.JS"></SCRIPT>
<SCRIPT TYPE="TEXT/JAVASCRIPT" SRC="HTTPS://CDNJS.CLOUDFLARE.COM/AJAX/LIBS/TWITTER-BOOTSTRAP/4.1.1/JS/BOOTSTRAP.MIN.JS"></SCRIPT>
</BODY>
</HTML>
APABILA APLIKASI DIJALANKAN DENGAN MENGETIKKAN ALAMAT, HTTP://LOCALHOST/FOLDER-PROJECT/INDEX.PHP/BUKU/EDIT/2, KURANG LEBIH TAMPILANNYA AKAN SEPERTI INI:
UNTUK SOURCE CODE SILAHKAN DOWNLOAD DI HTTPS://GITHUB.COM/MAULAYYACYBER/CRUD-CODEIGNITER
ALAMAT APLIKASI(BASE_URL)
, DATABASE
, AUTOLOAD
, DLL.DATABASE.PHP
DI APPLICATION/CONFIG/DATABASE.PHP
, UBAH MENJADI SEPERTI INI:$ACTIVE_GROUP = 'DEFAULT';
$QUERY_BUILDER = TRUE;
$DB['DEFAULT'] = ARRAY(
'DSN' => '',
'HOSTNAME' => 'LOCALHOST',
'USERNAME' => 'ROOT',
'PASSWORD' => '', //SESUAIKAN PASSWORD PHPMYADMIN TEMAN-TEMAN
'DATABASE' => 'CI_CRUD',
'DBDRIVER' => 'MYSQLI',
'DBPREFIX' => '',
'PCONNECT' => FALSE,
'DB_DEBUG' => (ENVIRONMENT !== 'PRODUCTION'),
'CACHE_ON' => FALSE,
'CACHEDIR' => '',
'CHAR_SET' => 'UTF8',
'DBCOLLAT' => 'UTF8_GENERAL_CI',
'SWAP_PRE' => '',
'ENCRYPT' => FALSE,
'COMPRESS' => FALSE,
'STRICTON' => FALSE,
'FAILOVER' => ARRAY(),
'SAVE_QUERIES' => TRUE
);
AUTOLOAD.PHP
DI FOLDER APPLICATION/CONFIG
DAN UBAH BAGIAN KODE BERIKUT INI:$AUTOLOAD['LIBRARIES'] = ARRAY('');
$AUTOLOAD['LIBRARIES'] = ARRAY('DATABASE', 'SESSION');
$AUTOLOAD['HELPER'] = ARRAY('');
$AUTOLOAD['HELPER'] = ARRAY('URL', 'FORM');
CONFIG.PHP
DI APPLICATION/CONFIG/CONFIG.PHP
, UBAH BAGIAN KODE-KODE DIBAWAH INI:$CONFIG['BASE_URL'] = "";
$CONFIG['BASE_URL'] = "HTTP://".$_SERVER['HTTP_HOST'].
STR_REPLACE(BASENAME($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$CONFIG['ENCRYPTION_KEY'] = '';
$CONFIG['ENCRYPTION_KEY'] = 'CODEIGNITERCRUDBASIC';
$CONFIG['CSRF_PROTECTION'] = FALSE;
$CONFIG['CSRF_TOKEN_NAME'] = 'CSRF_TOKEN_NAME';
$CONFIG['CSRF_COOKIE_NAME'] = 'CSRF_COOKIE_NAME';
$CONFIG['CSRF_EXPIRE'] = 7200;
$CONFIG['CSRF_REGENERATE'] = TRUE;
$CONFIG['CSRF_EXCLUDE_URIS'] = ARRAY();
$CONFIG['CSRF_PROTECTION'] = TRUE;
$CONFIG['CSRF_TOKEN_NAME'] = 'CSRF_TOKEN';
$CONFIG['CSRF_COOKIE_NAME'] = 'CSRF_COOKIE';
$CONFIG['CSRF_EXPIRE'] = 7200;
$CONFIG['CSRF_REGENERATE'] = TRUE;
$CONFIG['CSRF_EXCLUDE_URIS'] = ARRAY();
CI_CRUD
KEMUDIAN IMPORT SCRIPT BERIKUT INI KEDALAM MENU SQL.-- PHPMYADMIN SQL DUMP
-- VERSION 4.8.0
-- HTTPS://WWW.PHPMYADMIN.NET/
--
-- HOST: LOCALHOST
-- GENERATION TIME: MAY 17, 2018 AT 05:35 AM
-- SERVER VERSION: 5.7.9
-- PHP VERSION: 7.2.2
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET TIME_ZONE = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES UTF8MB4 */;
--
-- DATABASE: `CI_CRUD`
--
-- --------------------------------------------------------
--
-- TABLE STRUCTURE FOR TABLE `TBL_BUKU`
--
CREATE TABLE `TBL_BUKU` (
`ID_BUKU` INT(11) NOT NULL,
`NO_ISBN` VARCHAR(100) NOT NULL,
`NAMA_BUKU` VARCHAR(100) NOT NULL,
`TANGGAL_TERBIT` DATE NOT NULL,
`PENGARANG` VARCHAR(100) NOT NULL
) ENGINE=INNODB DEFAULT CHARSET=LATIN1;
--
-- DUMPING DATA FOR TABLE `TBL_BUKU`
--
INSERT INTO `TBL_BUKU` (`ID_BUKU`, `NO_ISBN`, `NAMA_BUKU`, `TANGGAL_TERBIT`, `PENGARANG`) VALUES
(2, '0002349876', 'PANDAI FRAMEWORK CODEIGNITER 1 MINGGU', '2018-05-22', 'FIKA RIDAUL MAULAYYA'),
(4, '0002349444', 'JAGO JAVASCRIPT', '2018-05-24', 'RIZQI MAULANA'),
(5, '0002343909', 'MENGUASAI FRAMEWORK RUBY ON RAILS', '2018-05-26', 'CAHYADI TRIYANSYAH'),
(6, '0002349097', 'MENDALAMI VUE JS ', '2018-02-19', 'YUDI PURWANTO');
--
-- INDEXES FOR DUMPED TABLES
--
--
-- INDEXES FOR TABLE `TBL_BUKU`
--
ALTER TABLE `TBL_BUKU`
ADD PRIMARY KEY (`ID_BUKU`);
--
-- AUTO_INCREMENT FOR DUMPED TABLES
--
--
-- AUTO_INCREMENT FOR TABLE `TBL_BUKU`
--
ALTER TABLE `TBL_BUKU`
MODIFY `ID_BUKU` INT(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7;
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
APPLICATION/CONTROLLERS
, SIMPAN DENGAN NAMA BUKU.PHP
, KEMUDIAN MASUKKAN KODE BERIKUT INI:<?PHP
DEFINED('BASEPATH') OR EXIT('NO DIRECT SCRIPT ACCESS ALLOWED');
CLASS BUKU EXTENDS CI_CONTROLLER {
PUBLIC FUNCTION __CONSTRUCT(){
PARENT ::__CONSTRUCT();
//LOAD MODEL
$THIS->LOAD->MODEL('MODEL_BUKU');
}
PUBLIC FUNCTION INDEX()
{
$DATA = ARRAY(
'TITLE' => 'DATA BUKU',
'DATA_BUKU' => $THIS->MODEL_BUKU->GET_ALL(),
);
$THIS->LOAD->VIEW('DATA_BUKU', $DATA);
}
PUBLIC FUNCTION TAMBAH()
{
$DATA = ARRAY(
'TITLE' => 'TAMBAH DATA BUKU'
);
$THIS->LOAD->VIEW('TAMBAH_BUKU', $DATA);
}
PUBLIC FUNCTION SIMPAN()
{
$DATA = ARRAY(
'NO_ISBN' => $THIS->INPUT->POST("NO_ISBN"),
'NAMA_BUKU' => $THIS->INPUT->POST("NAMA_BUKU"),
'TANGGAL_TERBIT' => $THIS->INPUT->POST("TANGGAL_TERBIT"),
'PENGARANG' => $THIS->INPUT->POST("PENGARANG"),
);
$THIS->MODEL_BUKU->SIMPAN($DATA);
$THIS->SESSION->SET_FLASHDATA('NOTIF', '<DIV CLASS="ALERT ALERT-SUCCESS ALERT-DISMISSIBLE"> SUCCESS! DATA BERHASIL DISIMPAN DIDATABASE.
</DIV>');
//REDIRECT
REDIRECT('BUKU/');
}
PUBLIC FUNCTION EDIT($ID_BUKU)
{
$ID_BUKU = $THIS->URI->SEGMENT(3);
$DATA = ARRAY(
'TITLE' => 'EDIT DATA BUKU',
'DATA_BUKU' => $THIS->MODEL_BUKU->EDIT($ID_BUKU)
);
$THIS->LOAD->VIEW('EDIT_BUKU', $DATA);
}
PUBLIC FUNCTION UPDATE()
{
$ID['ID_BUKU'] = $THIS->INPUT->POST("ID_BUKU");
$DATA = ARRAY(
'NO_ISBN' => $THIS->INPUT->POST("NO_ISBN"),
'NAMA_BUKU' => $THIS->INPUT->POST("NAMA_BUKU"),
'TANGGAL_TERBIT' => $THIS->INPUT->POST("TANGGAL_TERBIT"),
'PENGARANG' => $THIS->INPUT->POST("PENGARANG"),
);
$THIS->MODEL_BUKU->UPDATE($DATA, $ID);
$THIS->SESSION->SET_FLASHDATA('NOTIF', '<DIV CLASS="ALERT ALERT-SUCCESS ALERT-DISMISSIBLE"> SUCCESS! DATA BERHASIL DIUPDATE DIDATABASE.
</DIV>');
//REDIRECT
REDIRECT('BUKU/');
}
PUBLIC FUNCTION HAPUS($ID_BUKU)
{
$ID['ID_BUKU'] = $THIS->URI->SEGMENT(3);
$THIS->MODEL_BUKU->HAPUS($ID);
//REDIRECT
REDIRECT('BUKU/');
}
}
APPLICATION/MODELS
, SIMPAN DENGAN NAMA MODEL_BUKU.PHP
, KEMUDIAN MASUKKAN KODE BERIKUT INI:<?PHP
DEFINED('BASEPATH') OR EXIT('NO DIRECT SCRIPT ACCESS ALLOWED');
CLASS MODEL_BUKU EXTENDS CI_MODEL{
PUBLIC FUNCTION GET_ALL()
{
$QUERY = $THIS->DB->SELECT("*")
->FROM('TBL_BUKU')
->ORDER_BY('ID_BUKU', 'DESC')
->GET();
RETURN $QUERY->RESULT();
}
PUBLIC FUNCTION SIMPAN($DATA)
{
$QUERY = $THIS->DB->INSERT("TBL_BUKU", $DATA);
IF($QUERY){
RETURN TRUE;
}ELSE{
RETURN FALSE;
}
}
PUBLIC FUNCTION EDIT($ID_BUKU)
{
$QUERY = $THIS->DB->WHERE("ID_BUKU", $ID_BUKU)
->GET("TBL_BUKU");
IF($QUERY){
RETURN $QUERY->ROW();
}ELSE{
RETURN FALSE;
}
}
PUBLIC FUNCTION UPDATE($DATA, $ID)
{
$QUERY = $THIS->DB->UPDATE("TBL_BUKU", $DATA, $ID);
IF($QUERY){
RETURN TRUE;
}ELSE{
RETURN FALSE;
}
}
PUBLIC FUNCTION HAPUS($ID)
{
$QUERY = $THIS->DB->DELETE("TBL_BUKU", $ID);
IF($QUERY){
RETURN TRUE;
}ELSE{
RETURN FALSE;
}
}
}
APPLICATION/VIEWS
, BUATLAH 3 FILE YAITU:DATA_BUKU.PHP
, KEMUDIAN MASUKKAN KODE BERIKUT INI:<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE><?PHP ECHO $TITLE ?></TITLE>
<LINK REL="STYLESHEET" TYPE="TEXT/CSS" HREF="HTTPS://CDNJS.CLOUDFLARE.COM/AJAX/LIBS/TWITTER-BOOTSTRAP/4.1.1/CSS/BOOTSTRAP.MIN.CSS">
<LINK REL="STYLESHEET" TYPE="TEXT/CSS" HREF="HTTPS://CDN.DATATABLES.NET/1.10.16/CSS/DATATABLES.BOOTSTRAP4.MIN.CSS">
</HEAD>
<BODY>
<DIV CLASS="CONTAINER" STYLE="MARGIN-TOP: 80PX">
<?PHP ECHO $THIS->SESSION->FLASHDATA('NOTIF') ?>
<A HREF="<?PHP ECHO BASE_URL() ?>BUKU/TAMBAH/" CLASS="BTN BTN-MD BTN-SUCCESS">TAMBAH BUKU</A>
<HR>
<!-- TABLE -->
<DIV CLASS="TABLE-RESPONSIVE">
<TABLE ID="TABLE" CLASS="TABLE TABLE-STRIPED TABLE-BORDERED TABLE-HOVER">
<THEAD>
<TR>
<TH>NO.</TH>
<TH>NO ISBN</TH>
<TH>NAMA BUKU</TH>
<TH>TANGGAL TERBIT</TH>
<TH>PENGARANG</TH>
<TH>OPTIONS</TH>
</TR>
</THEAD>
<TBODY>
<?PHP
$NO = 1;
FOREACH($DATA_BUKU AS $HASIL){
?>
<TR>
<TD><?PHP ECHO $NO++ ?></TD>
<TD><?PHP ECHO $HASIL->NO_ISBN ?></TD>
<TD><?PHP ECHO $HASIL->NAMA_BUKU ?></TD>
<TD><?PHP ECHO $HASIL->TANGGAL_TERBIT ?></TD>
<TD><?PHP ECHO $HASIL->PENGARANG ?></TD>
<TD>
<A HREF="<?PHP ECHO BASE_URL() ?>BUKU/EDIT/<?PHP ECHO $HASIL->ID_BUKU ?>" CLASS="BTN BTN-SM BTN-SUCCESS">EDIT</A>
<A HREF="<?PHP ECHO BASE_URL() ?>BUKU/HAPUS/<?PHP ECHO $HASIL->ID_BUKU ?>" CLASS="BTN BTN-SM BTN-DANGER">HAPUS</A>
</TD>
</TR>
<?PHP } ?>
</TBODY>
</TABLE>
</DIV>
</DIV>
<SCRIPT TYPE="TEXT/JAVASCRIPT" SRC="HTTPS://CODE.JQUERY.COM/JQUERY-3.3.1.MIN.JS"></SCRIPT>
<SCRIPT TYPE="TEXT/JAVASCRIPT" SRC="HTTPS://CDNJS.CLOUDFLARE.COM/AJAX/LIBS/TWITTER-BOOTSTRAP/4.1.1/JS/BOOTSTRAP.MIN.JS"></SCRIPT>
<SCRIPT TYPE="TEXT/JAVASCRIPT" SRC="HTTPS://CDN.DATATABLES.NET/1.10.16/JS/JQUERY.DATATABLES.MIN.JS"></SCRIPT>
<SCRIPT TYPE="TEXT/JAVASCRIPT" SRC="HTTPS://CDN.DATATABLES.NET/1.10.16/JS/DATATABLES.BOOTSTRAP4.MIN.JS"></SCRIPT>
<SCRIPT>
$('#TABLE').DATATABLE( {
AUTOFILL: TRUE
} );
</SCRIPT>
</BODY>
</HTML>
TAMBAH_BUKU.PHP
, KEMUDIAN MASUKKAN KODE BERIKUT INI:<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE><?PHP ECHO $TITLE ?></TITLE>
<LINK REL="STYLESHEET" TYPE="TEXT/CSS" HREF="HTTPS://CDNJS.CLOUDFLARE.COM/AJAX/LIBS/TWITTER-BOOTSTRAP/4.1.1/CSS/BOOTSTRAP.MIN.CSS">
</HEAD>
<BODY>
<DIV CLASS="CONTAINER" STYLE="MARGIN-TOP: 80PX">
<DIV CLASS="COL-MD-12">
<?PHP ECHO FORM_OPEN('BUKU/SIMPAN') ?>
<DIV CLASS="FORM-GROUP">
<LABEL FOR="TEXT">NO ISBN</LABEL>
<INPUT TYPE="TEXT" NAME="NO_ISBN" CLASS="FORM-CONTROL" PLACEHOLDER="MASUKKAN NO. ISBN">
</DIV>
<DIV CLASS="FORM-GROUP">
<LABEL FOR="TEXT">NAMA BUKU</LABEL>
<INPUT TYPE="TEXT" NAME="NAMA_BUKU" CLASS="FORM-CONTROL" PLACEHOLDER="MASUKKAN NAMA BUKU">
</DIV>
<DIV CLASS="FORM-GROUP">
<LABEL FOR="TEXT">TANGGAL TERBIT</LABEL>
<INPUT TYPE="DATE" NAME="TANGGAL_TERBIT" CLASS="FORM-CONTROL" >
</DIV>
<DIV CLASS="FORM-GROUP">
<LABEL FOR="TEXT">PENGARANG</LABEL>
<INPUT TYPE="TEXT" NAME="PENGARANG" CLASS="FORM-CONTROL" >
</DIV>
<BUTTON TYPE="SUBMIT" CLASS="BTN BTN-MD BTN-SUCCESS">SIMPAN</BUTTON>
<BUTTON TYPE="RESET" CLASS="BTN BTN-MD BTN-WARNING">RESET</BUTTON>
<?PHP ECHO FORM_CLOSE() ?>
</DIV>
</DIV>
<SCRIPT TYPE="TEXT/JAVASCRIPT" SRC="HTTPS://CODE.JQUERY.COM/JQUERY-3.3.1.MIN.JS"></SCRIPT>
<SCRIPT TYPE="TEXT/JAVASCRIPT" SRC="HTTPS://CDNJS.CLOUDFLARE.COM/AJAX/LIBS/TWITTER-BOOTSTRAP/4.1.1/JS/BOOTSTRAP.MIN.JS"></SCRIPT>
</BODY>
</HTML>
EDIT_BUKU.PHP
, KEMUDIAN MASUKKAN KODE BERIKUT INI:<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE><?PHP ECHO $TITLE ?></TITLE>
<LINK REL="STYLESHEET" TYPE="TEXT/CSS" HREF="HTTPS://CDNJS.CLOUDFLARE.COM/AJAX/LIBS/TWITTER-BOOTSTRAP/4.1.1/CSS/BOOTSTRAP.MIN.CSS">
</HEAD>
<BODY>
<DIV CLASS="CONTAINER" STYLE="MARGIN-TOP: 80PX">
<DIV CLASS="COL-MD-12">
<?PHP ECHO FORM_OPEN('BUKU/UPDATE') ?>
<DIV CLASS="FORM-GROUP">
<LABEL FOR="TEXT">NO ISBN</LABEL>
<INPUT TYPE="TEXT" NAME="NO_ISBN" VALUE="<?PHP ECHO $DATA_BUKU->NO_ISBN ?>" CLASS="FORM-CONTROL" PLACEHOLDER="MASUKKAN NO. ISBN">
<INPUT TYPE="HIDDEN" VALUE="<?PHP ECHO $DATA_BUKU->ID_BUKU ?>" NAME="ID_BUKU">
</DIV>
<DIV CLASS="FORM-GROUP">
<LABEL FOR="TEXT">NAMA BUKU</LABEL>
<INPUT TYPE="TEXT" NAME="NAMA_BUKU" VALUE="<?PHP ECHO $DATA_BUKU->NAMA_BUKU ?>" CLASS="FORM-CONTROL" PLACEHOLDER="MASUKKAN NAMA BUKU">
</DIV>
<DIV CLASS="FORM-GROUP">
<LABEL FOR="TEXT">TANGGAL TERBIT</LABEL>
<INPUT TYPE="DATE" NAME="TANGGAL_TERBIT" VALUE="<?PHP ECHO $DATA_BUKU->TANGGAL_TERBIT ?>" CLASS="FORM-CONTROL" >
</DIV>
<DIV CLASS="FORM-GROUP">
<LABEL FOR="TEXT">PENGARANG</LABEL>
<INPUT TYPE="TEXT" NAME="PENGARANG" VALUE="<?PHP ECHO $DATA_BUKU->PENGARANG ?>" CLASS="FORM-CONTROL" >
</DIV>
<BUTTON TYPE="SUBMIT" CLASS="BTN BTN-MD BTN-SUCCESS">UPDATE</BUTTON>
<BUTTON TYPE="RESET" CLASS="BTN BTN-MD BTN-WARNING">RESET</BUTTON>
<?PHP ECHO FORM_CLOSE() ?>
</DIV>
</DIV>
<SCRIPT TYPE="TEXT/JAVASCRIPT" SRC="HTTPS://CODE.JQUERY.COM/JQUERY-3.3.1.MIN.JS"></SCRIPT>
<SCRIPT TYPE="TEXT/JAVASCRIPT" SRC="HTTPS://CDNJS.CLOUDFLARE.COM/AJAX/LIBS/TWITTER-BOOTSTRAP/4.1.1/JS/BOOTSTRAP.MIN.JS"></SCRIPT>
</BODY>
</HTML>
Komentar
Posting Komentar