Pada pembahasan kali ini saya ingin membahas seluruhnya, mulai dari :
- multi add data
- multi edit data, dan
- multi delete data
- PHP 5.6
- Mysql 5.7
- Codeigniter 3.1.4
Tanpa banyak basa basi mari kita mulai.
STEP BY STEP
CONFIGURASI
Sebelum memula, pertama-tama kita harus melakukan konfigurasi codeigniternya dulu, dianaranya :
autoload.php
dibagian ini cukup load library session dan database saja.
$autoload['libraries'] = array('database','session');
config.php
Dibagian ini, replace kode base_url pada baris 26 dengan kode di bawah ini.
$http = 'http' . ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 's' : '') . '://';
$newurl = str_replace("index.php","", $_SERVER['SCRIPT_NAME']);
$config['base_url'] = "$http" . $_SERVER['SERVER_NAME'] . "" . $newurl;
Kode di atas berfungsi untuk membuat URL yang dinamis.
database.php
Pada bagian ini pertama kamu harus membuat databasenya terlebih dahulu (namanya bebas), kemudian eksekusi query sql berikut :
-- phpMyAdmin SQL Dump
-- version 4.6.5.2
-- https://www.phpmyadmin.net/
--
-- Host: localhost
-- Erstellungszeit: 15. Mai 2017 um 11:54
-- Server-Version: 10.1.21-MariaDB
-- PHP-Version: 5.6.30
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 utf8mb4 */;
--
-- Datenbank: `data_tutorial`
--
-- --------------------------------------------------------
--
-- Tabellenstruktur für Tabelle `murid`
--
CREATE TABLE `murid` (
`id` int(11) NOT NULL,
`nama` varchar(30) NOT NULL,
`umur` char(3) NOT NULL,
`kelas` char(3) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Daten für Tabelle `murid`
--
INSERT INTO `murid` (`id`, `nama`, `umur`, `kelas`) VALUES
(1, 'Sisca', '21', '12'),
(2, 'Jojo', '20', '12'),
(3, 'Joni', '21', '11');
--
-- Indizes der exportierten Tabellen
--
--
-- Indizes für die Tabelle `murid`
--
ALTER TABLE `murid`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT für exportierte Tabellen
--
--
-- AUTO_INCREMENT für Tabelle `murid`
--
ALTER TABLE `murid`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=9;
/*!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 */;
Jika sudah, silahkan atur koneksi databasenya sesuai settingan web server di lokal kamu.
routes.php
Dibagian ini cukup konfigurasi default controllernya saya, seperti ini :
$route['default_controller'] = 'multi_crud';
CONTROLLERS
Buat controller baru dengan nama Multi_crud.php lalu salin kode di bawah ini :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Multi_crud extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model("multi_crud_model","get_db");
}
function index()
{
$q["data"] = $this->get_db->get_list()->result();
$q["data_cout"] = $this->get_db->get_list()->num_rows();
$this->load->view("daftar", $q);
}
function tambah()
{
$get = $this->input->get();
$q['total_form'] = $get['total_form'];
$this->load->view('tambah', $q);
}
function tambah_proses()
{
$post = $this->input->post();
$result = array();
$total_post = count($post['nama']);
foreach($post['nama'] AS $key => $val)
{
$result[] = array(
"nama" => $post['nama'][$key],
"umur" => $post['umur'][$key],
"kelas" => $post['kelas'][$key]
);
}
$this->get_db->post_add($result);
$this->session->set_flashdata('notif', '<p style="color:green;font-weight:bold;">'.$total_post.' data berhasil di simpan!</p>');
redirect('multi_crud');
}
function sunting_hapus()
{
$post = $this->input->post();
$check = $post['check'];
if(isset($check))
{
if(isset($post['sunting']))
{
$q['data'] = $this->get_db->get_edit($post)->result();
$q['data_count'] = $this->get_db->get_edit($post)->num_rows();
$this->load->view('sunting', $q);
}
elseif(isset($post['hapus']))
{
$this->get_db->post_delete($post);
$this->session->set_flashdata('notif', '<p style="color:green;font-weight:bold;">'.count($check).' data berhasil dihapus!</p>');
redirect('multi_crud');
}
}
else
{
$this->session->set_flashdata('notif', '<p style="color:red;font-weight:bold;">Harap centang dulu datanya!</p>');
redirect('multi_crud');
}
}
function sunting_proses()
{
$post = $this->input->post();
$result = array();
$total_post = count($post['id']);
foreach($post['id'] AS $key => $val)
{
$result[] = array(
"id" => $post['id'][$key],
"nama" => $post['nama'][$key],
"umur" => $post['umur'][$key],
"kelas" => $post['kelas'][$key]
);
}
$this->get_db->post_edit($result);
$this->session->set_flashdata('notif', '<p style="color:green;font-weight:bold;">'.$total_post.' data berhasil di sunting!</p>');
redirect('multi_crud');
}
}
MODELS
Buat models dengan nama Multi_crud_model.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Multi_crud_model extends CI_Model
{
function get_list()
{
$this->db->select("id, nama, umur, kelas");
$this->db->from("murid");
$this->db->order_by('id', 'DESC');
$q = $this->db->get();
return $q;
}
function get_edit($post)
{
$this->db->select("id, nama, umur, kelas");
$this->db->from("murid");
$this->db->where_in('id', $post['check']);
$this->db->order_by('id', 'DESC');
$q = $this->db->get();
return $q;
}
function post_add($result = array())
{
$total_array = count($result);
if($total_array != 0)
{
$this->db->insert_batch('murid', $result);
}
}
function post_edit($result = array())
{
$total_array = count($result);
if($total_array != 0)
{
$this->db->update_batch('murid', $result, 'id');
}
}
function post_delete($post = array())
{
$total_array = count($post);
if($total_array != 0)
{
$this->db->where_in('id', $post['check']);
$this->db->delete('murid');
}
}
}
VIEWS
Lalu yang terakhir buat viewnya, pertama buat view dengan nama daftar.php
<html>
<head>
<title>Multi CRUD by k4ng</title>
</head>
<body>
<h1>Multi CRUD by k4ng</h1>
<?=form_open("multi_crud/tambah", array("method" => "GET"));?>
<fieldset>
<legend><b>Tambah data</b></legend>
<p>Silahkan ubah angka pada form di bawah dengan angka yang kamu inginkan.</p>
<label>Buat</label>
<input type="number" name="total_form" value="1" style="width:50px;">
<label> form</label>
<input type="submit" name="submit" value="ok">
</fieldset>
<?=form_close();?>
<?=$this->session->flashdata('notif');?>
<fieldset>
<legend><b>Daftar Murid</b></legend>
<p style="color:red;font-weight:bold;">*) Centang terlebih dahulu jika ingin mengubah atau menghapus data!</p>
<?=form_open("multi_crud/sunting_hapus");?>
<table cellpadding="5" cellspacing="5">
<tr>
<td>#</td>
<td>Nama</td>
<td>Umur</td>
<td>Kelas</td>
</tr>
<?php
if($data_cout == 0)
{
echo "
<tr>
<td colspan='4' align='center' style='color:#B0BEC5;'>Tidak ada data murid!</td>
</tr>
";
}
else
{
foreach ($data as $v) {
echo "
<tr>
<td><input type='checkbox' name='check[]' value='{$v->id}'></td>
<td>".$v->nama."</td>
<td>".$v->umur."</td>
<td>".$v->kelas."</td>
</tr>
";
}
}
?>
</table>
<input type="submit" name="sunting" value="Sunting">
<input type="submit" name="hapus" value="Hapus">
<?=form_close();?>
</fieldset>
</body>
</html>
Lalu yang kedua sunting.php
<html>
<head>
<title>Sunting Data ~ Multi CRUD by k4ng</title>
</head>
<body>
<h1>Multi CRUD by k4ng</h1>
<?=form_open("multi_crud/sunting_proses");?>
<fieldset>
<legend><b>Sunting data</b></legend>
<table cellpadding="5" cellspacing="5">
<?php
foreach ($data as $key => $v)
{
echo "
<tr>
<td><input type='hidden' name='id[]' value='{$v->id}'></td>
<td><input type='text' name='nama[]' placeholder='nama' value='{$v->nama}'></td>
<td><input type='number' name='umur[]' placeholder='umur' value='{$v->umur}' style='width:60px;'></td>
<td><input type='number' name='kelas[]' placeholder='kelas' value='{$v->kelas}' style='width:60px;'></td>
</tr>
";
}
?>
</table>
<input type="submit" name="sunting" value="Simpan perubahan">
<input type="reset" value="Reset">
</fieldset>
<?=form_close();?>
</body>
</html>
Dan yang terakhir tambah.php
<html>
<head>
<title>Tambah Data ~ Multi CRUD by k4ng</title>
</head>
<body>
<h1>Multi CRUD by k4ng</h1>
<?=form_open("multi_crud/tambah_proses");?>
<fieldset>
<legend><b>Tambah data</b></legend>
<table cellpadding="5" cellspacing="5">
<?php
for ($i=0; $i < $total_form; $i++)
{
echo "
<tr>
<td><input type='text' name='nama[]' placeholder='nama'></td>
<td><input type='number' name='umur[]' placeholder='umur' style='width:60px;'></td>
<td><input type='number' name='kelas[]' placeholder='kelas' style='width:60px;'></td>
</tr>
";
}
?>
</table>
<input type="submit" name="simpan" value="Simpan">
<input type="reset" value="Reset">
</fieldset>
<?=form_close();?>
</body>
</html>
Untuk tampilan aplikasi bisa di lihat pada gambar di atas, jika kamu ingin mendapatkan kode lengkapnya, kamu bisa unduh disini : unduh sekarang
Mungkin artikel saya cukupkan sampai disini, semoga bermanfaat, terima kasih.
terima kasih gan sharing ilmunya akhirnya bisa multi insert..
BalasHapus(y) sukses selalu gan
Sama-sama gan, semoga manfaat ilmunya :)
HapusPosting Komentar