Codeigniter CRUD dengan Plugin Grocery CRUD

Jika Anda benar-benar baru untuk pengalaman CodeIgniter dan Anda tidak dapat masih memahami cara yang Anda dapat membuat CRUD, ini CodeIgniter CRUD tutorial ini hanya untuk Anda. Di bawah ini Anda akan melihat dengan langkah-langkah bagaimana membuat kelontong kerja CRUD dan cara menginstalnya ke sebuah proyek baru. Semua contoh di bawah ini untuk CodeIgniter 2.x

Langkah ke 1. Pertama-tama download CodeIgniter dan pastikan bahwa Anda memiliki proyek layar selamat datang. Jadi jika layar pertama Anda proyek Anda terlihat seperti gambar di bawah, Anda hanya dipasang kerangka CodeIgniter untuk proyek Anda.


Welcome Page Codeigniter
Langkah ke 2. Sekarang kita siap untuk mengkonfigurasi database kami untuk proyek CodeIgniter kami. Koneksi database untuk CodeIgniter dapat dikonfigurasi pada: your_project / application / config / database.php File akan terlihat seperti ini:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 5.1.6 or newer
... mpla mpla mpla
*/
 
$active_group = 'default';
$active_record = TRUE;
 
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = '';
$db['default']['password'] = '';
$db['default']['database'] = '';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
$db['default']['failover'] = array();
 
/* End of file database.php */
/* Location: ./application/config/database.php */
 


sekarang ubah  setelan database.php pada bagian ini saja :

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '1234'; //Pretty secure don't you think?
$db['default']['database'] = 'my_new_cms';


Langkah ke 3. Mari kita membuat kontroler pertama kami. Mari kita nama kontroler pertama kami Utama. Jadi untuk melakukan itu, Anda akan pergi ke your_project / application / controllers / dan tambahkan main.php, yang akan terlihat seperti ini:


<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Main 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 main.php */
/* Location: ./application/controllers/main.php */


Jika ketika di jalankan pada direktori url http://localhost/your_web/index.php, terdapat tampilan seperti ini....berarti anda belum mempunyai database dan tabel di dalam mysql


Codeigniter Database Error
Langkah ke 4.Buat tabel Anda dalam database Anda. Katakanlah kita memiliki tabel dengan nama tabel "employee". Kode SQL bahwa Anda dapat memasukkan adalah:


CREATE TABLE IF NOT EXISTS `employees` (
  `employeeNumber` int(11) NOT NULL AUTO_INCREMENT,
  `lastName` varchar(50) NOT NULL,
  `firstName` varchar(50) NOT NULL,
  `extension` varchar(10) NOT NULL,
  `email` varchar(100) NOT NULL,
  `officeCode` varchar(10) NOT NULL,
  `file_url` varchar(250) CHARACTER SET utf8 NOT NULL,
  `jobTitle` varchar(50) NOT NULL,
  PRIMARY KEY (`employeeNumber`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1703 ;

INSERT INTO `employees` (`employeeNumber`, `lastName`, `firstName`, `extension`, `email`, `officeCode`, `file_url`, `jobTitle`) VALUES
(1002, 'Murphy', 'Diane', 'x5800', 'dmurphy@classicmodelcars.com', '1', '', 'President'),
(1056, 'Patterson', 'Mary', 'x4611', 'mpatterso@classicmodelcars.com', '1', '', 'VP Sales'),
(1076, 'Firrelli', 'Jeff', 'x9273', 'jfirrelli@classicmodelcars.com', '1', '', 'VP Marketing'),
(1088, 'Patterson', 'William', 'x4871', 'wpatterson@classicmodelcars.com', '6', '', 'Sales Manager (APAC)'),
(1102, 'Bondur', 'Gerard', 'x5408', 'gbondur@classicmodelcars.com', '4', 'pdftest.pdf', 'Sale Manager (EMEA)'),
(1143, 'Bow', 'Anthony', 'x5428', 'abow@classicmodelcars.com', '1', '', 'Sales Manager (NA)'),
(1165, 'Jennings', 'Leslie', 'x3291', 'ljennings@classicmodelcars.com', '1', '', 'Sales Rep'),
(1166, 'Thompson', 'Leslie', 'x4065', 'lthompson@classicmodelcars.com', '1', '', 'Sales Rep'),
(1188, 'Firrelli', 'Julie', 'x2173', 'jfirrelli@classicmodelcars.com', '2', 'test-2.pdf', 'Sales Rep'),
(1216, 'Patterson', 'Steve', 'x4334', 'spatterson@classicmodelcars.com', '2', '', 'Sales Rep'),
(1286, 'Tseng', 'Foon Yue', 'x2248', 'ftseng@classicmodelcars.com', '3', '', 'Sales Rep'),
(1323, 'Vanauf', 'George', 'x4102', 'gvanauf@classicmodelcars.com', '3', '', 'Sales Rep'),
(1337, 'Bondur', 'Loui', 'x6493', 'lbondur@classicmodelcars.com', '4', '', 'Sales Rep'),
(1370, 'Hernandez', 'Gerard', 'x2028', 'ghernande@classicmodelcars.com', '4', '', 'Sales Rep'),
(1401, 'Castillo', 'Pamela', 'x2759', 'pcastillo@classicmodelcars.com', '4', '', 'Sales Rep'),
(1501, 'Bott', 'Larry', 'x2311', 'lbott@classicmodelcars.com', '7', '', 'Sales Rep'),
(1504, 'Jones', 'Barry', 'x102', 'bjones@classicmodelcars.com', '7', '', 'Sales Rep'),
(1611, 'Fixter', 'Andy', 'x101', 'afixter@classicmodelcars.com', '6', '', 'Sales Rep'),
(1612, 'Marsh', 'Peter', 'x102', 'pmarsh@classicmodelcars.com', '6', '', 'Sales Rep'),
(1619, 'King', 'Tom', 'x103', 'tking@classicmodelcars.com', '6', '', 'Sales Rep'),
(1621, 'Nishi', 'Mami', 'x101', 'mnishi@classicmodelcars.com', '5', '', 'Sales Rep'),
(1625, 'Kato', 'Yoshimi', 'x102', 'ykato@classicmodelcars.com', '5', '', 'Sales Rep'),
(1702, 'Gerard', 'Martin', 'x2312', 'mgerard@classicmodelcars.com', '4', '', 'Sales Rep');
 



Langkah ke 5. Pastikan bahwa Anda telah menginstal kelontong CRUD benar pada proyek Anda dengan menambahkan semua file dan folder untuk proyek Anda. Setelah akhir instalasi, proyek Anda akan harus melihat mirip dengan struktur ini:
website_folder/ 
–––– application/ 
–––––––– config/ 
–––––––––––– autoload.php 
–––––––––––– ... 
–––––––––––– doctypes.php 
–––––––––––– foreign_chars.php 
–––––––––––– grocery_crud.php 
–––––––––––– ... 
–––––––– controllers/ 
–––––––––––– examples.php 
–––––––––––– index.html 
–––––––––––– welcome.php 
–––––––– libraries/ 
–––––––––––– grocery_crud.php 
–––––––––––– index.html 
–––––––– models/ 
–––––––––––– grocery_crud_model.php 
–––––––––––– index.html 
–––––––– views/ 
–––––––––––– example.php 
–––––––––––– index.html 
–––––––––––– welcome_message.php 
–––– assets/ 
–––––––– grocery_crud/ 
–––––––––––– css/ 
–––––––––––– js/ 
–––––––––––– texteditor/ 
–––––––––––– themes/ 
–––––––– uploads/ 
–––––––– index.html 
–––– system/ 
–––– user_guide/ 
–––– change_log.txt 
–––– example_database.sql 
–––– index.php 
–––– licence-grocery-crud.txt 
–––– license.txt
Langkah Ke 6. kemudian kita pergi ke controller dan menambahkan beberapa hal untuk membuat toko CRUD bekerja. Tambahkan metode karyawan seperti contoh di bawah:


<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Main 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 employees()
    {
        $this->grocery_crud->set_table('employees');
        $output = $this->grocery_crud->render();
 
        echo "<pre>";
        print_r($output);
        echo "</pre>";
        die();
    }
}
 
/* End of file main.php */
/* Location: ./application/controllers/main.php */
 


Catatan Penting: Pastikan bahwa Anda tidak memiliki kelontong CRUD dengan fungsi indeks controller Anda karena merupakan masalah yang diketahui bahwa itu tidak akan bekerja pada indeks. Hanya memindahkannya ke metode lain. Misalnya "karyawan" atau sesuatu yang lain kecuali indeks.

Langkah Ke 7. Sekarang mari kita membuat pandangan kita. Hasil yang telah kita lihat kita dapat menggunakannya untuk memiliki kelontong CRUD untuk proyek kami. Kita hanya perlu pandangan untuk melakukannya. Jadi pandangan akan terlihat seperti ini:


 
<!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>


dan ubah sedikit controllernya seperti :



<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Main 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 employees()
    {
        $this->grocery_crud->set_table('employees');
        $output = $this->grocery_crud->render();
 
        $this->_example_output($output);        
    }
 
    function _example_output($output = null)
 
    {
        $this->load->view('our_template.php',$output);    
    }
}
 
/* End of file main.php */
/* Location: ./application/controllers/main.php */


sekarang buka browser dan ketikkan URL ini di alamat webnya http://localhost/your_project/index.php/main/employees sehingga tampilan akan sebagai berikut.


Employees example


gimana...??? mudah bukan..??......menggunakan Framework CodeIgniter dengan tool Grocery CRUD...
anda bisa lihat tutorial dan trik-trik Grocery CRUD di Nugashare ....

0 komentar:

Copyright © 2012 Nugasoft Production.