Langkah awal :
- Buat Database dan Table
- Buat File Controller Selanjutnya setelah kita selesai membuat database dan table seperti gambar di atas maka kita buat file controller dengan nama
- Buat File View
- Kode
index.php
- Kode
edit.php
- Buat File Model
Langkah awal yang harus kita siapkan adalah membuat database
dan table
karena kita butuh data yang harus kita update, buat database dengan nama ci_tutorial
atau terserah teman nama databasenya apa, selanjutnya untuk nama tablenya kasih sama dengan di tutorial ini yaa usahakan, nama tablenya yaitu table kode_area
dan table users
:
Multiple.php
dan langsung saja kopi kode controller dibawah ini :
<?php
Class Multiple extends CI_Controller
{
public function __construct(){
parent::__construct();
$this->load->model('Mcrud');
}
public function index()
{
$data['users'] = $this->db->get('users')->result();
$this->load->view('multiple/index', $data);
}
public function edit($id)
{
$data['kode_area'] = $this->db->get_where('kode_area', ['id_user'=>$id])->result();
$data['get_id'] = $id;
$this->load->view('multiple/edit', $data);
}
public function approval($id)
{
$a = $this->input->post('kode[]');
$b = $this->input->post('status[]');
$i = 1;
foreach($b as $status)
{
if(!empty($status))
{
$where = [
'id_user'=>$id,
'kode'=>$a[$i]
];
$data = ['status'=>$status];
$this->Mcrud->update('kode_area', $data, $where);
$i++;
}
}
$this->session->set_flashdata('pesan', 'Data area user berhasil diverifikasi');
redirect('index.php/multiple');
}
}
?>
Pada langkah ini silahkan buat sebuah folder dengan nama multiple
dan buat 2 file didalamnya index.php
dan edit.php
Silahkan kopi kode file view index.php
dibawah :
<!DOCTYPE html>
<html lang="in">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Update Multiple File</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<nav class="navbar navbar-expand-md navbar-light bg-white shadow-sm">
<div class="container">
<a class="navbar-brand" href="#"></a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="{{ __('Toggle navigation') }}">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="#"><i class="fa fa-home"></i>Table</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#"><i class="fa fa-tasks"></i>Form</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container mt-3">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">
Data Multiple Users
</div>
<div class="col-md-12">
<?php
$pesan = $this->session->flashdata('pesan');
if($pesan)
{
?>
<div class="alert alert-success">
<?= $pesan ?>
</div>
<?php
}
?>
</div>
<div class="card-body">
<table class="table table-striped">
<tr>
<th>Name</th>
<th>Action</th>
</tr>
<?php
foreach ($users as $row) {
?>
<tr>
<td><?= $row->name ?></td>
<td>
<a href="<?= site_url('index.php/multiple/edit/'.$row->id) ?>" class="btn btn-success btn-sm">Approval</a>
</td>
</tr>
<?php
}
?>
</table>
</div>
<div class="card-footer">
Page
</div>
</div>
</div>
</div>
</div>
</body>
</html>
Silahkan kopi kode file view edit.php
dibawah :
<!DOCTYPE html>
<html lang="in">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Update Multiple File</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<nav class="navbar navbar-expand-md navbar-light bg-white shadow-sm">
<div class="container">
<a class="navbar-brand" href="#"></a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="{{ __('Toggle navigation') }}">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="#"><i class="fa fa-home"></i>Table</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#"><i class="fa fa-tasks"></i>Form</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container mt-3">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">
Edit Multiple Data
</div>
<form method="post" action="<?= site_url('index.php/multiple/approval/'.$get_id) ?>">
<div class="card-body">
<table class="table table-striped">
<tr>
<th>Kode Area</th>
<th>Status</th>
<th>Approval</th>
</tr>
<?php
$i=1;
foreach ($kode_area as $row) { ?>
<input type="hidden" name="kode[<?= $i ?>]" value="<?= $row->kode ?>">
<tr>
<td><?= $row->kode ?></td>
<td>
<?php
if($row->status == 0)
{
?>
<span class="badge badge-warning">Pending</span>
<?php
}
else
{
echo $row->status == 1 ? '<span class="badge badge-success">Diterima</span>' : '<span class="badge badge-danger">Ditolak</span>';
}
?>
</td>
<td>
<label>
<input type="radio" name="status[<?= $i ?>]" value="1" required> Diterima
</label>
<label>
<input type="radio" name="status[<?= $i ?>]" value="2" required> Ditolak
</label>
</td>
</tr>
<?php
$i++;
}
?>
</table>
</div>
<div class="card-footer text-right">
<a href="<?= site_url('index.php/multiple') ?>" class="btn btn-secondary">Kembali</a>
<button type="submit" class="btn btn-primary">Approval</button>
</div>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
Terakhir anda buat file model dengan nama Mcrud.php
dan silahkan anda kopi kode dibawah ini :
<?php
Class Mcrud extends CI_Model
{
public function update($table, $data, $where)
{
$this->db->where($where)
->update($table, $data);
return TRUE;
}
}
Terimakasih Gan. Banyakin lagi tutor CI3 nya
ReplyDeleteTerima kasih gan, kunjungi tutorial batu form loop, tambahan input selec box menggunakn database dengan codeigniter 4
Delete