Category Archives: CodeIgniter

How to make 2 submit in form using codeignter

sometime you need two submit if form each submit has difference function like one submit for print and one again for searching data by periode for example, let say you have form like bellow

<h1 class="text-center">Form Laporan</h1>
  <form class="form-horizontal" role="form" action="<?php echo base_url()?>admin/laporan/submit" method="post" enctype="multipart/form-data">
  

<div class="form-group">
      <label for="lastname" class="col-sm-4 control-label">Dari</label>
      <div class="col-sm-3">
          <input type="text" class="form-control"  name="dari" required="" id="tanggal">
      </div>
 
</div>

<div class="form-group">
      <label for="lastname" class="col-sm-4 control-label">Sampai </label>
      <div class="col-sm-3">
          <input type="text" class="form-control"  name="sampai" required="" id="date" >
      </div>
</div>
 <div class="form-group">
      <div class="col-sm-offset-5 col-sm-3">
         <button type="submit" class="btn btn-primary" name="cetak">Cetak</button>
         <button type="submit" class="btn btn-default" name="cari">Cari</button>
      </div>
   </div>
</form>
    

and here your controller


public function submit ()
                {
if(isset($_POST['cetak']))
    {
//do something here
  }
 else if (isset($_POST['cari']))
        {
      //do something here
       }
  

well that simple tutorial for me, Cheers (y)

 

 

 

multi language in codeigniter from database

this is my short tutorial about multi languange. first you should download database here

and we are going to set up bassic instalation here is my config database

  • $db['default']['hostname'] = 'localhost';
    $db['default']['username'] = 'root';
    $db['default']['password'] = '';
    $db['default']['database'] = 'test';
    $db['default']['dbdriver'] = 'mysql';

and copas 2 file bellow here to your application/core there is give it name MY_Config.php

  • <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    // CodeIgniter i18n library by Jérôme Jaglale
    // http://maestric.com/en/doc/php/codeigniter_i18n
    // version 10 - May 10, 2012
    
    class MY_Config extends CI_Config {
    
     function site_url($uri = '')
     {
     if (is_array($uri))
     {
     $uri = implode('/', $uri);
     }
    
     if (class_exists('CI_Controller'))
     {
     $CI =& get_instance();
     $uri = $CI->lang->localized($uri);
     }
    
     return parent::site_url($uri);
     }
    
    }
    
    /* End of file */
    

and this give it name MY_Config.php

  • <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    // CodeIgniter i18n library by Jérôme Jaglale
    // http://maestric.com/en/doc/php/codeigniter_i18n
    // version 10 - May 10, 2012
    
    class MY_Lang extends CI_Lang {
    
     /**************************************************
     configuration
     ***************************************************/
    
     // languages
     var $languages = array(
     'en' => 'english',
     'fr' => 'french'
     );
    
     // special URIs (not localized)
     var $special = array (
    
     );
    
     // where to redirect if no language in URI
     var $default_uri = ''; 
    
     /**************************************************/
    
     function __construct()
     {
     parent::__construct(); 
    
     global $CFG;
     global $URI;
     global $RTR;
    
     $segment = $URI->segment(1);
    
     if (isset($this->languages[$segment])) // URI with language -> ok
     {
     $language = $this->languages[$segment];
     $CFG->set_item('language', $language);
    
     }
     else if($this->is_special($segment)) // special URI -> no redirect
     {
     return;
     }
     else // URI without language -> redirect to default_uri
     {
     // set default language
     $CFG->set_item('language', $this->languages[$this->default_lang()]);
    
     // redirect
     header("Location: " . $CFG->site_url($this->localized($this->default_uri)), TRUE, 302);
     exit;
     }
     }
    
     // get current language
     // ex: return 'en' if language in CI config is 'english'
     function lang()
     {
     global $CFG;
     $language = $CFG->item('language');
    
     $lang = array_search($language, $this->languages);
     if ($lang)
     {
     return $lang;
     }
    
     return NULL; // this should not happen
     }
    
     function is_special($uri)
     {
     $exploded = explode('/', $uri);
     if (in_array($exploded[0], $this->special))
     {
     return TRUE;
     }
     if(isset($this->languages[$uri]))
     {
     return TRUE;
     }
     return FALSE;
     }
    
     function switch_uri($lang)
     {
     $CI =& get_instance();
    
     $uri = $CI->uri->uri_string();
     if ($uri != "")
     {
     $exploded = explode('/', $uri);
     if($exploded[0] == $this->lang())
     {
     $exploded[0] = $lang;
     }
     $uri = implode('/',$exploded);
     }
     return $uri;
     }
    
     // is there a language segment in this $uri?
     function has_language($uri)
     {
     $first_segment = NULL;
    
     $exploded = explode('/', $uri);
     if(isset($exploded[0]))
     {
     if($exploded[0] != '')
     {
     $first_segment = $exploded[0];
     }
     else if(isset($exploded[1]) && $exploded[1] != '')
     {
     $first_segment = $exploded[1];
     }
     }
    
     if($first_segment != NULL)
     {
     return isset($this->languages[$first_segment]);
     }
    
     return FALSE;
     }
    
     // default language: first element of $this->languages
     function default_lang()
     {
     foreach ($this->languages as $lang => $language)
     {
     return $lang;
     }
     }
    
     // add language segment to $uri (if appropriate)
     function localized($uri)
     {
     if($this->has_language($uri)
     || $this->is_special($uri)
     || preg_match('/(.+)\.[a-zA-Z0-9]{2,4}$/', $uri))
     {
     // we don't need a language segment because:
     // - there's already one or
     // - it's a special uri (set in $special) or
     // - that's a link to a file
     }
     else
     {
     $uri = $this->lang() . '/' . $uri;
     }
    
     return $uri;
     }
    
    }
    
    /* End of file */
    

let’s create controller just change welcome.php

  • <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Welcome extends CI_Controller {
    
     public function __construct() {
     parent::__construct();
    
     $this->load->model('welcome_model');
    
     $this->lang->load('welcome');
     }
    
     public function index()
     {
     $current_lang = $this->lang->lang();
    
     $data['records'] = $this->welcome_model->get_all_data($current_lang);
    
     $this->load->view('welcome_message',$data);
     }
    }
    
    /* End of file welcome.php */
    /* Location: ./application/controllers/welcome.php */

and model here give it name welcome_model.php

  • <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Welcome_model extends CI_Model {
     function get_all_data($lang)
     {
     return $this->db->get_where('language',array('lang'=>$lang))->result();
     }
    }

also view

  • <!DOCTYPE html>
    <html lang="en">
    <head>
     <meta charset="utf-8">
     <title>Welcome to CodeIgniter</title>
    
     <style type="text/css">
    
     ::selection{ background-color: #E13300; color: white; }
     ::moz-selection{ background-color: #E13300; color: white; }
     ::webkit-selection{ background-color: #E13300; color: white; }
    
     body {
     background-color: #fff;
     margin: 40px;
     font: 13px/20px normal Helvetica, Arial, sans-serif;
     color: #4F5155;
     }
    
     a {
     color: #003399;
     background-color: transparent;
     font-weight: normal;
     }
    
     h1 {
     color: #444;
     background-color: transparent;
     border-bottom: 1px solid #D0D0D0;
     font-size: 19px;
     font-weight: normal;
     margin: 0 0 14px 0;
     padding: 14px 15px 10px 15px;
     }
    
     code {
     font-family: Consolas, Monaco, Courier New, Courier, monospace;
     font-size: 12px;
     background-color: #f9f9f9;
     border: 1px solid #D0D0D0;
     color: #002166;
     display: block;
     margin: 14px 0 14px 0;
     padding: 12px 10px 12px 10px;
     }
    
     #body{
     margin: 0 15px 0 15px;
     }
    
     p.footer{
     text-align: right;
     font-size: 11px;
     border-top: 1px solid #D0D0D0;
     line-height: 32px;
     padding: 0 10px 0 10px;
     margin: 20px 0 0 0;
     }
    
     #container{
     margin: 10px;
     border: 1px solid #D0D0D0;
     -webkit-box-shadow: 0 0 8px #D0D0D0;
     }
     </style>
    </head>
    <body>
    
    <div id="container">
     <h1>Welcome to CodeIgniter!</h1>
    
     <?php echo anchor('kategori', 'Kategori');?>
    
     <?php echo anchor($this->lang->switch_uri('fr'),'French'); echo ' | '.anchor($this->lang->switch_uri('en'),'English');?>
     <div id="body">
    
     <?php if(count($records)>0):?>
    
     <?php foreach($records as $record):?>
    
     <?php echo $record->title; ?>
     <br>
     <?php echo $record->text; ?>
    
     <?php endforeach; ?>
     <?php endif; ?>
     </div>
    
    </div>
    
    </body>
    </html>

in this application i just create french and also english, if you are going to change languange just open it application/core/MY_Lang.php change languange what you want to do example ‘in’=>indonesia and also open database and make sure same in database.

  • var $languages = array(
     'en' => 'english',
     'fr' => 'french'
     );

well below here is screen shot my simple application multi languange english and french
paint

well i’m sorry guys if i’m not good to tell tutorial here, whole script you can get it here

this tutorial came from here http://news.nationalgeographic.com/news/2015/02/150206-global-warming-sea-slugs-animals-ocean-science-california/

feel free to comment here bro because i’m still learn Codeigniter, if you have another way to make multi languange, would you share it here ?

Tutorial Part 2 CRUD using Codeigniter and Bootstrap (Delete in array using checkbox)

well after first tutorial here i will continue taht tutorial about CRUD today i will make about delete using checkbox and let change view to make edit and delete here is whole script view

  <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8;charset=utf-8" />
 <meta name="robots" content="index, follow" />
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <!-- Bootstrap -->
 <link href="//cdn.datatables.net/plug-ins/3cfcc339e89/integration/bootstrap/3/dataTables.bootstrap.css" rel="stylesheet" media="screen"> 

 <!-- Bootstrap -->
 <link href="<?php echo base_url()?>asset/css/bootstrap.min.css" rel="stylesheet" media="screen">
 <link href="<?php echo base_url()?>asset/css/bootstrap-theme.min.css" rel="stylesheet" media="screen">
 <link href="<?php echo base_url()?>asset/css/style.css" rel="stylesheet" media="screen">
 <link href="<?php echo base_url()?>asset/css/costum/flexslider.css" rel="stylesheet" media="screen">
 <script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
 <script src="//cdn.datatables.net/1.10.4/js/jquery.dataTables.min.js"></script>
 <script src="//cdn.datatables.net/plug-ins/3cfcc339e89/integration/bootstrap/3/dataTables.bootstrap.js"></script>
 <script type="text/javascript">
 $(document).ready(function() {
 $('#example').dataTable();
 } );
 </script>
 </head>
 <body>
 
 <form name="indonesia" action="<?php echo site_url('admin/karir/delete_multiple'); ?>" method="post"> 
 
 <button type="submit" class="btn btn-danger" name="hapus" value="hapus">Hapus</button>
 
 <?php echo anchor('admin/karir/tambah', 'Tambah Karir');?>
 <table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
 <thead>
 <tr>
 <th>
 <button type="button" class="btn btn-info" onClick="check_all()" >Check</button>
 <button type="button" class="btn btn-success" onClick="uncheck_all()" >Un-Check</button>
 </th>
 <th>Id</th>
 <th>Judul</th>
 <th>Status</th>
 </tr>
 </thead>
 
 <tbody>
 <?php
 foreach ($ListKarir->result() as $row)
 {
 ?>
 <tr>
 <td><input type="checkbox" name="item[]" id="item[]" value="<?=$row->id_project ?>"></td>
 <td><?=$row->id_project ?></td>
 <td><?=$row->judul ?></td>
 <td><?=$row->status ?></td>
 </tr>
 <?php } ?>
 </tbody>
 </table>
 
 </form> 

 <!-- javascript check and check all -->
<script type="text/javascript">
$(document).ready(function() {
 $('#example').dataTable();
} );
</script>
<script language="javascript">
function check_all()
{
 var chk = document.getElementsByName('item[]');
 for (i = 0; i < chk.length; i++)
 chk[i].checked = true ;
}
 
function uncheck_all()
{
 var chk = document.getElementsByName('item[]');
 for (i = 0; i < chk.length; i++)
 chk[i].checked = false ;
}
</script>
 </body>
</html>
  and then you should make controller for it, here is controller

  • <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Karir extends CI_Controller {
     public function index()
     {
     // here is data from db which get from model
     $data['ListKarir'] = $this->karir_model->getAllKarir();
    
     //here is view with data parsing from model
     $this->load->view("admin/karir", $data);
     }
    
     //Proses it to model
     public function delete_multiple()
     {
     $this->karir_model->remove_checked_indonesia();
     redirect('admin/karir');
     }

and the last we create model

 

<?php
 class Karir_model extends CI_Model
 {
 function __construct()
 {
 parent::__construct();
 }
 function getAllKarir()
 {
 $this->db->from("tabel_project");
 return $this->db->get();
 }
 
 function remove_checked_indonesia() 
 {
 $action = $this->input->post('hapus');
 if ($action) 
 {
 $delete = $this->input->post('item');
 for ($i=0; $i < count($delete) ; $i++) 
 { 
 $this->db->where('id_project', $delete[$i]);
 $this->db->delete('tabel_project');
 }
 }
 }

and your interface will be look like this, if any question please be nice to comment here

 

check

Tutorial Part 1 CRUD using Codeigniter and Bootstrap

well ini share tutorial di weekend tentang CRUD menggunakan codeigniter dan bootstrap, let check this out gan.

Berikut databasenya

  • -- phpMyAdmin SQL Dump
    -- version 3.5.2.2
    -- http://www.phpmyadmin.net
    --
    -- Inang: 127.0.0.1
    -- Waktu pembuatan: 02 Feb 2015 pada 08.19
    -- Versi Server: 5.5.27
    -- Versi PHP: 5.4.7
    
    SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
    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 utf8 */;
    
    --
    -- Basis data: `cms_ci`
    --
    
    -- --------------------------------------------------------
    
    --
    -- Struktur dari tabel `tabel_project`
    --
    
    CREATE TABLE IF NOT EXISTS `tabel_project` (
     `id_project` int(5) NOT NULL AUTO_INCREMENT,
     `artikel` text NOT NULL,
     `judul` text NOT NULL,
     `status` varchar(50) NOT NULL,
     PRIMARY KEY (`id_project`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=20 ;
    
    --
    -- Dumping data untuk tabel `tabel_project`
    --
    
    INSERT INTO `tabel_project` (`id_project`, `artikel`, `judul`, `status`) VALUES
    (2, '3434', '343', '343'),
    (3, '343', '343', '343'),
    (4, '232', '232', '232'),
    (5, '232', '232', '232'),
    (6, '232', '232', '232'),
    (7, '232', '232', '232'),
    (8, '232', '232', '232'),
    (9, '232', '23', '232'),
    (10, '3232', '232', '232'),
    (11, '232', '232', '232'),
    (12, '3232', '232', '232'),
    (13, '232', '232', '232'),
    (14, '2323', '2323', '2323'),
    (15, '23', '23323', '232'),
    (16, '23232', '2323', '232'),
    (17, '23232', '3232', '23232'),
    (18, '232', '232', '2323'),
    (19, '2323', '2323', '23232');
    
    /*!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 */;
    

first you should create controller kasih nama karir.php

  • <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Karir extends CI_Controller {
    public function index()
     {
     // here is data from db which get from model
     $data['ListKarir'] = $this->karir_model->getAllKarir();
    
     //here is view with data parsing from model
     $this->load->view("admin/karir", $data);
     }
    }

    dan kita buat model atau view nya ini, kita buat viewnya saja dulu ya, di dalam view buat folder admin dan kasih nama karir.php berikut codenya

      <html>
     <head>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8;charset=utf-8" />
     <meta name="robots" content="index, follow" />
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <!-- Bootstrap -->
     <link href="//cdn.datatables.net/plug-ins/3cfcc339e89/integration/bootstrap/3/dataTables.bootstrap.css" rel="stylesheet" media="screen"> 
    
     <!-- Bootstrap -->
     <link href="<?php echo base_url()?>asset/css/bootstrap.min.css" rel="stylesheet" media="screen">
     <link href="<?php echo base_url()?>asset/css/bootstrap-theme.min.css" rel="stylesheet" media="screen">
     <link href="<?php echo base_url()?>asset/css/style.css" rel="stylesheet" media="screen">
     <link href="<?php echo base_url()?>asset/css/costum/flexslider.css" rel="stylesheet" media="screen">
     <script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
     <script src="//cdn.datatables.net/1.10.4/js/jquery.dataTables.min.js"></script>
     <script src="//cdn.datatables.net/plug-ins/3cfcc339e89/integration/bootstrap/3/dataTables.bootstrap.js"></script>
     <script type="text/javascript">
     $(document).ready(function() {
     $('#example').dataTable();
     } );
     </script>
     </head>
     <body>
     <table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
     <thead>
     <tr>
     <th>Name</th>
     <th>Position</th>
     <th>Office</th>
     </tr>
     </thead>
     <tbody>
     <?php
     foreach ($ListKarir->result() as $row)
     {
     ?>
     <tr>
     <td><?=$row->id_project ?></td>
     <td><?=$row->judul ?></td>
     <td><?=$row->artikel ?></td>
     </tr>
     <?php } ?>
     </tbody>
     </table>
     </body>
    </html>

dan berikut modelnya kasih nama karir_model.php

<?php
 class Karir_model extends CI_Model
 {
 function __construct()
 {
 parent::__construct();
 }
 function getAllKarir()
 {
 $this->db->from("tabel_project");
 return $this->db->get();
 }
 } 

berikut penampkannya, maaf kalau codenya agak kacau cause ini dari folder kerjaan ane
karir

Kalau ada pertanyaan komen aja di mari gan, Happy Weekend deh

Tutorial how to make simple Tab using Codeigniter and Jquery

well this is a very simpel tab using codeigniter, oke let’s make it, first you should download codeigniter the last version while donload you visit here, see documentasi jquery about tab, after done download codeigniter you should extract it in your directory, let say i have directory name codeigniter_jquery, see my directory

codeigniter

Codeigniter

well open your browser localhost/codeigniter_jquery/ and you will default codeigniter original, oke just open your view welcome_message.php and change whole script to be like this

  • <!doctype html>
    <html lang="en">
    <head>
     <meta charset="utf-8">
     <title>jQuery UI Tabs - Default functionality</title>
     <link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
     <script src="//code.jquery.com/jquery-1.10.2.js"></script>
     <script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
     <link rel="stylesheet" href="/resources/demos/style.css">
     <script>
     $(function() {
     $( "#tabs" ).tabs();
     });
     </script>
    </head>
    <body>
    
    <div id="tabs">
     <ul>
     <li><a href="#tabs-1">Nunc tincidunt</a></li>
     <li><a href="#tabs-2">Proin dolor</a></li>
     <li><a href="#tabs-3">Aenean lacinia</a></li>
     </ul>
     <div id="tabs-1">
     <p>Proin elit arcu, rutrum commodo, vehicula tempus, commodo a, risus. Curabitur nec arcu. Donec sollicitudin mi sit amet mauris. Nam elementum quam ullamcorper ante. Etiam aliquet massa et lorem. Mauris dapibus lacus auctor risus. Aenean tempor ullamcorper leo. Vivamus sed magna quis ligula eleifend adipiscing. Duis orci. Aliquam sodales tortor vitae ipsum. Aliquam nulla. Duis aliquam molestie erat. Ut et mauris vel pede varius sollicitudin. Sed ut dolor nec orci tincidunt interdum. Phasellus ipsum. Nunc tristique tempus lectus.</p>
     </div>
     <div id="tabs-2">
     <p>Morbi tincidunt, dui sit amet facilisis feugiat, odio metus gravida ante, ut pharetra massa metus id nunc. Duis scelerisque molestie turpis. Sed fringilla, massa eget luctus malesuada, metus eros molestie lectus, ut tempus eros massa ut dolor. Aenean aliquet fringilla sem. Suspendisse sed ligula in ligula suscipit aliquam. Praesent in eros vestibulum mi adipiscing adipiscing. Morbi facilisis. Curabitur ornare consequat nunc. Aenean vel metus. Ut posuere viverra nulla. Aliquam erat volutpat. Pellentesque convallis. Maecenas feugiat, tellus pellentesque pretium posuere, felis lorem euismod felis, eu ornare leo nisi vel felis. Mauris consectetur tortor et purus.</p>
     </div>
     <div id="tabs-3">
     <p>Mauris eleifend est et turpis. Duis id erat. Suspendisse potenti. Aliquam vulputate, pede vel vehicula accumsan, mi neque rutrum erat, eu congue orci lorem eget lorem. Vestibulum non ante. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Fusce sodales. Quisque eu urna vel enim commodo pellentesque. Praesent eu risus hendrerit ligula tempus pretium. Curabitur lorem enim, pretium nec, feugiat nec, luctus a, lacus.</p>
     <p>Duis cursus. Maecenas ligula eros, blandit nec, pharetra at, semper at, magna. Nullam ac lacus. Nulla facilisi. Praesent viverra justo vitae neque. Praesent blandit adipiscing velit. Suspendisse potenti. Donec mattis, pede vel pharetra blandit, magna ligula faucibus eros, id euismod lacus dolor eget odio. Nam scelerisque. Donec non libero sed nulla mattis commodo. Ut sagittis. Donec nisi lectus, feugiat porttitor, tempor ac, tempor vitae, pede. Aenean vehicula velit eu tellus interdum rutrum. Maecenas commodo. Pellentesque nec elit. Fusce in lacus. Vivamus a libero vitae lectus hendrerit hendrerit.</p>
     </div>
    </div>
    </body>
    </html>

and you will see simple tab, oke that’s litle bite tutorial for me, have a beers :D