CodeIgniter Snippet

Config.php

Membuat redirect kode secara Default agar bisa menampilkan berdasarkan projek aplikasi web anda yang dibuat. Semisal anda menaruh aplikasi projek pada htdocs/namafolder/namafolder anda tidak perlu merubah base_url nya menjadi localhost/namafolder/namafolder

Dan jika URL hosting anda menggunakan https:// anda juga dapat me redirect langsung dengan cara seperti pada kode berikut:

$root=(isset($_SERVER['HTTPS']) ? "https://" : "http://").$_SERVER['HTTP_HOST'];
$root.= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
$config['base_url']=$root;

htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Header set X-XSS-Protection "1; mode=block" 
</IfModule>

Template views

Themes.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Themes {
	protected $_ci;
	function __construct(){
		$this->_ci=&get_instance();
	}
	function display($set, $data=null){
		$data['_content']=$this->_ci->load->view($set,$data,true);
		$this->_ci->load->view('themes.php',$data);
	}
}

/* End of file Themes.php */
/* Location: ./application/libraries/Themes.php */

Anda bisa menggunakan Library diatas untuk memudahkan load views tanpa harus membuat header, content, footer yang diulang-ulang untuk meloadnya dan anda cukup deklarasi pada Controller seperti berikut :

Home Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends CI_Controller {
	
	public function __construct()
	{
		parent::__construct();
	}
	public function index()
	{
		$this->data['title']='Home';
		$this->themes->display('content/home',$this->data);
	}

}
themes.php (views)
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<title><?php echo $title ?></title>
	<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
	<!-- Bootstrap 3.3.7 -->
	<link rel="stylesheet" href="<?php echo base_url('assets/css/bootstrap.min.css');?>">
	<link rel="stylesheet" href="<?php echo base_url('assets/css/font-awesome.min.css');?>">
	<script src="<?php echo base_url('assets/js/jquery.min.js');?>"></script>
    <script src="<?php echo base_url('assets/js/bootstrap.min.js');?>"></script>
</head>
<body>
<div class="wrapper">
  <header>
    <ul>
      <li> <?php echo anchor('home','Home');?></li>
      <li> <?php echo anchor('about','About');?></li>
    </ul>
  </header>
  <div class="content">
    //Display Content in here
    <?php echo $_content; ?>
  </div>
  <footer>
    <?php echo  (ENVIRONMENT === 'development') ?  'CodeIgniter Version <strong>' . CI_VERSION . '</strong>' : '' ?>
    <strong>Copyright &copy; <?php echo date('Y') ?> <a href="https://herusulistiono.net" target="_blank">Heru Sulistiono</a>.</strong> All rights reserved.
  </footer>
</div>
</body>
</html>

Helper

Membuat Jumlah terbilang seperti Kwitansi

Helper Terbilang

rpkata_helper.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
function penyebut($nilai) {
    $nilai = abs($nilai);
    $huruf = array("", "satu", "dua", "tiga", "empat", "lima", "enam", "tujuh", "delapan", "sembilan", "sepuluh", "sebelas");
    $temp = "";
    if ($nilai < 12) {
        $temp = " ". $huruf[$nilai];
    } else if ($nilai <20) {
        $temp = penyebut($nilai - 10). " belas";
    } else if ($nilai < 100) {
        $temp = penyebut($nilai/10)." puluh". penyebut($nilai % 10);
    } else if ($nilai < 200) {
        $temp = " seratus" . penyebut($nilai - 100);
    } else if ($nilai < 1000) {
        $temp = penyebut($nilai/100) . " ratus" . penyebut($nilai % 100);
    } else if ($nilai < 2000) {
        $temp = " seribu" . penyebut($nilai - 1000);
    } else if ($nilai < 1000000) {
        $temp = penyebut($nilai/1000) . " ribu" . penyebut($nilai % 1000);
    } else if ($nilai < 1000000000) {
        $temp = penyebut($nilai/1000000) . " juta" . penyebut($nilai % 1000000);
    } else if ($nilai < 1000000000000) {
        $temp = penyebut($nilai/1000000000) . " milyar" . penyebut(fmod($nilai,1000000000));
    } else if ($nilai < 1000000000000000) {
        $temp = penyebut($nilai/1000000000000) . " trilyun" . penyebut(fmod($nilai,1000000000000));
    }     
    return $temp;
}
function terbilang($nilai) {
    if($nilai<0) {
        $hasil = "minus ". trim(penyebut($nilai));
    } else {
        $hasil = trim(penyebut($nilai));
    }           
    return $hasil;
}
/* End of file rpkata_helper.php */
/* Location: ./application/helpers/rpkata_helper.php */
Views - Data
Menampilkan cukup dengan seperti
<?php echo strtoupper(terbilang($jumlah)); ?>

Helper Active Menu Class Controller

menu_helper.php
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
if(!function_exists('active_menu')) {
  function activate_menu($controller) {
    // Getting CI class instance.
    $CI = get_instance();
    // Getting router class to active.
    $class = $CI->router->fetch_class();
    return ($class == $controller) ? 'active' : '';
  }
}
Views Link
<ul>
  <li class="<?php echo active_menu('home');?>"> 
    <?php echo anchor('home','Home');?></li>
  <li class="<?php echo active_menu('about');?>"> 
    <?php echo anchor('about','About');?></li>
</ul>

Form Validation Bootsrap

Application => Views
<section class="py-2">
	<div class="row">
		<div class="col-lg-12 mb-4">
			<div class="card">
            <div class="card-header bg-primary text-white">
            	<div class="row">
	         		<div class="col-lg">
	         			<h6 class="text-uppercase mb-0"><?php echo $title; ?></h6>
	         		</div>
	         		<div class="col-lg">
	         			<div class="float-right">
			  					<button type="button" data-toggle="modal" data-target="#provinsi_modal" class="btn btn-rounded btn-secondary btn-sm">ADD</button>
			  				</div>
	         		</div>
	         	</div>
            </div>
            <div class="card-body">
            	<div class="table-responsive">
				   	<table id="provinsi_data" class="table table-hover table-striped table-sm card-text">
					    	<thead>
								<tr>
									<th width="5%" class="text-center">NO</th>
									<th>PROVINSI NAME</th>
									<th width="5%" class="text-center">#</th>
								</tr>
							</thead>
							<tbody></tbody>
				   	</table>
					</div>
            </div>
         </div>
		</div>
	</div>	
</section>
<div class="modal fade" id="provinsi_modal" data-backdrop="static" tabindex="-1" role="dialog" aria-hidden="true">
	<div class="modal-dialog" role="document">
		<div class="modal-content">
			<?php echo form_open('#',array("id"=>"form","autocomplete"=>"off")); ?>
			<div class="modal-header bg-primary border-0">
				<h5 class="modal-title text-white">ADD NEW PROVINSI</h5>
				<button type="button" class="close" data-dismiss="modal" aria-label="Close" onclick="return location.reload()">
					<span aria-hidden="true">&times;</span>
				</button>
			</div>
			<div class="modal-body p-3">
				<div id="provinsi_name-group" class="form-group">
					<label>PROVINSI:</label>
					<input type="text" name="provinsi_name" id="provinsi_name" class="form-control form-control-sm" placeholder="PROVINSI"/>
				</div>
				<input type="hidden" name="prov_id"/>
			</div>
			<div class="modal-footer border-0">
				<button type="button" id="save" class="btn btn-primary btn-rounded" onclick="insert_provinsi()">SAVE</button>
				<button type="button" id="update" class="btn btn-warning btn-rounded" disabled="disabled" onclick="update_provinsi()">UPDATE</button>
				<button type="button" class="btn btn-danger btn-rounded" data-dismiss="modal">CANCEL</button>
			</div>
			<?php echo form_close(); ?>
		</div>
    </div>
</div>

<script>
function insert_provinsi() {
		data = $('#form').serialize();
		$('#save').text('please wait.....!!');
    $('#save').attr('disabled',true);
 		$('.form-group, .form-control').removeClass('is-invalid');
  	$('.invalid-feedback').remove();
 		$.ajax({
 			url: '<?php echo site_url('provinsi/insert_provinsi');?>',
 			type: 'POST',
 			dataType: 'JSON',
 			data: data,
 			encode:true,
 			success:function(data) {
 				console.log(data);
	      	if (!data.success) {
	      		$('#save').text('SAVE');
          		$('#save').attr('disabled',false);
	      		if (data.errors.provinsi_name) {
		            $('#provinsi_name-group, #provinsi_name').addClass('is-invalid');
		            $('#provinsi_name-group').append('<div class="invalid-feedback">'+data.errors.provinsi_name+'</div>');
		            $('#provinsi_name').focus();
		            return false;
		         }
	      	}else{
	      		var msg = data.message;
	            setTimeout(function () {
				      		window.location.reload()
				      },800);
	      	}
 			}
 		})
	}
</script>
Application => Controller
public function insert_provinsi()
	{
		$errors;
		$info=array();
		if($this->input->post('provinsi_name')==='' || $this->input->post('provinsi_name'))
			$this->form_validation->set_rules('provinsi_name','PROVINSI','required|is_unique[provinsi.provinsi_name]',array('is_unique'=> '%s already exists !!.'));
			if ($this->form_validation->run('provinsi_name') === FALSE)
				$errors['provinsi_name']=validation_errors();

		if ($this->form_validation->run()===TRUE) {
			$data = array(
				'provinsi_name' => ucwords($this->input->post('provinsi_name')),
			);
			$this->provinsi_m->insert($data);
			$info['success']=TRUE;
	      $info['message']='Successfully';
		}else{
			$info['success']=FALSE;
			$info['errors']=$errors;
		}
		$this->output->set_content_type('application/json')->set_output(json_encode($info));
	}

Upload Image

Datatable Load Data

Last updated