Friday, October 11, 2019

Image CRUD dengan Grocerycrud dan Codeigniter

Yang harus dilakukan adalah :


  • Download GroceryCrud image crud di grocerycrud.com/imgae-crud
  • Extract hasil downloadnya, lalu copy and replace pada folder codeigniter
  • Buka phpMyAdmin di http://localhost/phpmyadmin/
  • Buat database baru, contoh : db_image
  • lalu klik pada menu SQL
  • Pastekan examples_database.sql kedalam SQL hasilnya akan seperti berikut :


  • Selanjutna konfigurasi database db_image dengan codeigniter
  • Buka file database.php di xampp\htdocs\ImageCrud\application\config
  • isi username dengan root dan database dengan db_image

  • Buat controller di xampp\htdocs\application\controllers, Sebagai contoh : sibobi.php
  • ketikkan kode berikut :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class sibobi extends CI_Controller {

                function __construct()
                {
                                parent::__construct();
                               
                                /* Standard Libraries */
                                $this->load->database();
                                /* ------------------ */
                               
                                $this->load->helper('url'); //Just for the examples, this is not required thought for the library
                               
                                $this->load->library('image_CRUD');
                }
               
                function _example_output($output = null)
                {
                                $this->load->view('v_image.php',$output);         
                }
               
                function index()
                {
                                $this->_example_output((object)array('output' => '' , 'js_files' => array() , 'css_files' => array()));
                }             
               
                function example1()
                {
                                $image_crud = new image_CRUD();
                               
                                $image_crud->set_primary_key_field('id');
                                $image_crud->set_url_field('url');
                                $image_crud->set_table('example_1')
                                                ->set_image_path('assets/uploads');
                                               
                                $output = $image_crud->render();
                               
                                $this->_example_output($output);
                }
               
                function example2()
                {
                                $image_crud = new image_CRUD();
                               
                                $image_crud->set_primary_key_field('id');
                                $image_crud->set_url_field('url');
                                $image_crud->set_table('example_2')
                                ->set_ordering_field('priority')
                                ->set_image_path('assets/uploads');
                                               
                                $output = $image_crud->render();
               
                                $this->_example_output($output);
                }

                function example3()
                {
                                $image_crud = new image_CRUD();
               
                                $image_crud->set_primary_key_field('id');
                                $image_crud->set_url_field('url');
                                $image_crud->set_table('example_3')
                                ->set_relation_field('category_id')
                                ->set_ordering_field('priority')
                                ->set_image_path('assets/uploads');
                                               
                                $output = $image_crud->render();
               
                                $this->_example_output($output);
                }

                function example4()
                {
                                $image_crud = new image_CRUD();
               
                                $image_crud->set_primary_key_field('id');
                                $image_crud->set_url_field('url');
                                $image_crud->set_title_field('title');
                                $image_crud->set_table('example_4')
                                ->set_ordering_field('priority')
                                ->set_image_path('assets/uploads');
                                               
                                $output = $image_crud->render();
               
                                $this->_example_output($output);
                }
               
                function simple_photo_gallery()
                {
                                $image_crud = new image_CRUD();
                               
                                $image_crud->unset_upload();
                                $image_crud->unset_delete();
                               
                                $image_crud->set_primary_key_field('id');
                                $image_crud->set_url_field('url');
                                $image_crud->set_table('example_4')
                                ->set_image_path('assets/uploads');
                               
                                $output = $image_crud->render();
                               
                                $this->_example_output($output);                        
                }
}
  • Selanjutnya buat v_image.php di xampp\htdocs\application\views, lalu ketikkan kode berikut
    <!DOCTYPE html>
    <html>
    <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>
        <div>
            <a href='<?php echo site_url('sibobi/example1')?>'>Example 1 - Simple</a> |
            <a href='<?php echo site_url('sibobi/example2')?>'>Example 2 - Ordering</a> |
            <a href='<?php echo site_url('sibobi/example3/22')?>'>Example 3 - With group id</a> |
            <a href='<?php echo site_url('sibobi/example4')?>'>Example 4 - Images with title</a> |
            <a href='<?php echo site_url('sibobi/simple_photo_gallery')?>'>Simple Photo Gallery</a>
        </div>
        <div style='height:20px;'></div> 
        <div>
            <?php echo $output; ?>
        </div>
    </body>
    </html> 


  • cobalah klik menu, dan masukan image yang lain ke dalam databasenya.

Thursday, October 10, 2019

CRUD Dengan CodeIgniter dan GroceryCrud

  • Yang harus dilakukan adalah
  • Download codeigniter
  • Download grocerycrud
  • Extract codeigniter dan grocerycrud
  • Copy and replace semua isi dari grocerycrud ke dalam codeigniter
  • Simpan folder codeigniter di directory xampp\htdocs
  • Buat sebuah database di localhost/phpMyAdmin dengan nama table files_db

  • Buatlah table dengan nama tb_file dengan columns sebanyak 7

  • Isi table dengan ketentuan sebagai berikut :

  • Atur konfigurasi database di codeigniter (\application\config\database.php)
  • Rubah username menjadi "root" dan database menjadi "files_db"

  • Buat sebuah controllers baru di \application\controllers dengan nama main.php
  • isi main.php dengan :
<?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 files()
    {
        $db_multimedia = new grocery_CRUD();
        $db_multimedia->set_table('tb_file');
        $output = $db_multimedia->render();

        $this->_example_output($output);       
    }

    function _example_output($output = null)

    {
        $this->load->view('v_main.php',$output);   
    }
}

  • Buat view di directory \application\views dengan nama v_main.php
  • isi v_main.php dengan :
<!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 -->
<!-- End of header-->
    <div style='height:20px;'></div> 
    <div>
        <?php echo $output; ?>

    </div>
<!-- Beginning footer -->
<div>Footer</div>
<!-- End of Footer -->
</body>
</html>

  •  Setelah selesai, buka browser lalu ketikkan
    http://localhost/Codeigniter/index.php/main/files
  • Maka akan muncul tampilan sebagai berikut :

  • Cobalah isi dengan tekan Add Record

  • Tekan Save and go back to list, dan hasilnya akan terlihat