Tutorial CRUD CodeIgniter dan Grocery Crud
Tutorial CRUD CodeIgniter dan Grocery Crud
Dalam blog ini saya akan memberi tutorial mengenai crud menggunakan framework CodeIgniter dan Grocery Grud.
Langkah pertama yang harus dilakukan yaitu download framework CodeIgniter dan Grocery Crud. Anda bisa mendownload CodeIgniter di website https://codeigniter.com/download
Dan untuk mendowload Grocery
Crud, Anda dapat mendownloadnya di https://www.grocerycrud.com/downloads
Lalu kita tes framework CI
yang sudah diinstal untuk berjalan, caranya buka browser lagu ketik alamat
localhost/codeigniter/
Setelah itu ubah konfigurasi database CI dalam file database.php (codeigniter/application/config/database.php). Masukan nama database Anda dengan benar dan usernamenya. Defaut username = root.
Buat sebuah file php dengan script sebagai berikut, simpan dengan nama Core.php di dalam folder controllers (xampp\htdocs\codeigniter\application\controllers).
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Core extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->database();
}
public function index()
{
echo "<h1>Welcome to the world of Codeigniter</h1>";//Just an example to ensure that we get into the function
die();
}
}
/* End of file Core.php */
/* Location: ./application/controllers/Core.php */
Tes file tersebut di browser dengan alamat url : localhost/codeigniter/index.php/core
Edit kembali file Core.php dengan isi sebagai berikut
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Core extends CI_Controller {
function __construct()
{
parent::__construct();
/* Standard Libraries of codeigniter are required */
$this->load->database();
$this->load->helper('url');
/* ------------------ */
$this->load->library('grocery_CRUD');
}
public function index()
{
echo "<h1>Welcome to the world of Codeigniter</h1>";//Just an example to ensure that we get into the function
die();
}
public function tbfile()
{
$crud = new grocery_CRUD();
$crud->set_table('tbfile');
$output = $this->grocery_crud->render();
echo "<pre>";
print_r($output);
echo "</pre>";
die();
}
}
/* End of file Core.php */
/* Location: ./application/controllers/Core.php */
Panggil file base_url dengan alamat localhost/codeigniter/index.php/core/tbfile di browser. Jika tidak terjadi error, maka akan ada tampimpilan sebagai berikut :
Selanjutnya buat tampilan template dengan script sebagai berikut :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<?php
foreach($css_files as $file): ?>
<link type="text/css" rel="stylesheet" href="<?php echo $file; ?>" />
<?php endforeach; ?>
<?php foreach($js_files as $file): ?>
<script src="<?php echo $file; ?>"></script>
<?php endforeach; ?>
<style type='text/css'>
body
{
font-family: Arial;
font-size: 14px;
}
a {
color: blue;
text-decoration: none;
font-size: 14px;
}
a:hover
{
text-decoration: underline;
}
</style>
</head>
<body>
<!-- Beginning header -->
<div>
<a href='<?php echo site_url('examples/offices_management')?>'>Offices</a> |
<a href='<?php echo site_url('examples/employees_management')?>'>Employees</a> |
<a href='<?php echo site_url('examples/customers_management')?>'>Customers</a> |
<a href='<?php echo site_url('examples/orders_management')?>'>Orders</a> |
<a href='<?php echo site_url('examples/products_management')?>'>Products</a> |
<a href='<?php echo site_url('examples/film_management')?>'>Films</a>
</div>
<!-- End of header-->
<div style='height:20px;'></div>
<div>
<?php echo $output; ?>
</div>
<!-- Beginning footer -->
<div>Footer</div>
<!-- End of Footer -->
</body></html>
Simpan file dengan nama core_template.php
Ubah kembali file Core.php dengan script sebagai berikut :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Core extends CI_Controller {
function __construct()
{
parent::__construct();
/* Standard Libraries of codeigniter are required */
$this->load->database();
$this->load->helper('url');
/* ------------------ */
$this->load->library('grocery_CRUD');
}
public function index()
{
echo "<h1>Welcome to the world of Codeigniter</h1>";//Just an example to ensure that we get into the function
die();
}
public function tbfile()
{
$crud = new grocery_CRUD();
$crud->set_table('tbfile');
$output = $crud->render();
$this->_example_output($output);
}
function _example_output($output = null)
{
$this->load->view('core_template.php',$output);
}
}
/* End of file Core.php */
Jalankan kembali dengan panggil url localhost/codeigniter/index.php/core/tbfile. Maka tampilan akan jadi sebagai berikut. Crud dengan menggunakan CI dan Grocery Crud telah berhasil.














Komentar
Posting Komentar