Postingan lainnya
Ambil data sesuai id-nya dan secara bersamaan ada form isian baru sesuai id tersebut
Selamat malam para senior, saya kebingungan bagaimana caranya Ambil data sesuai ID-nya dan secara bersamaan ada form isian baru sesuai ID tersebut. jadi saya ada data di tabel Nota Dinas dengan ID (id_notadinas) kemudian saya ingin ambil ID tersebut untuk input data ditabel Pelaksana yang memiliki isi ID yang sama. intinya saya ingin menggunakan ID di Tabel Nota Dinas untuk digunakan saat input Tabel Pelaksana (otomatis saat saya klik aksi read, tampil data nota dinas berdasarkan ini dan bersamaan ada form isian baru sesuai id yang tampil untuk input data pelaksana SPPD).
untuk codenya seperti ini :
Kode Model :
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Nota_dinas_model extends CI_Model
{
public $table = 'nota_dinas';
public $id = 'id_notadinas';
public $order = 'DESC';
function __construct()
{
parent::__construct();
}
// get all
function get_all()
{
$this->db->order_by($this->id, $this->order);
return $this->db->get($this->table)->result();
}
// get data by id
function get_by_id($id)
{
$this->db->where($this->id, $id);
return $this->db->get($this->table)->row();
}
// get total rows
function total_rows($q = NULL) {
$this->db->like('id_notadinas', $q);
$this->db->or_like('id_kegiatan', $q);
$this->db->or_like('id_rekening', $q);
$this->db->or_like('id_maksud', $q);
$this->db->or_like('no_notadinas', $q);
$this->db->or_like('tgl_notadinas', $q);
$this->db->or_like('dari_notadinas', $q);
$this->db->or_like('kepada_notadinas', $q);
$this->db->or_like('tembusan_notadinas', $q);
$this->db->or_like('perihal_notadinas', $q);
$this->db->or_like('id_tujuan', $q);
$this->db->or_like('tgl_pergi', $q);
$this->db->or_like('tgl_kembali', $q);
$this->db->or_like('lama_pelaksanaan', $q);
$this->db->from($this->table);
return $this->db->count_all_results();
}
// get data with limit and search
function get_limit_data($limit, $start = 0, $q = NULL) {
$this->db->order_by($this->id, $this->order);
$this->db->like('id_notadinas', $q);
$this->db->or_like('id_kegiatan', $q);
$this->db->or_like('id_rekening', $q);
$this->db->or_like('id_maksud', $q);
$this->db->or_like('no_notadinas', $q);
$this->db->or_like('tgl_notadinas', $q);
$this->db->or_like('dari_notadinas', $q);
$this->db->or_like('kepada_notadinas', $q);
$this->db->or_like('tembusan_notadinas', $q);
$this->db->or_like('perihal_notadinas', $q);
$this->db->or_like('id_tujuan', $q);
$this->db->or_like('tgl_pergi', $q);
$this->db->or_like('tgl_kembali', $q);
$this->db->or_like('lama_pelaksanaan', $q);
$this->db->limit($limit, $start);
return $this->db->get($this->table)->result();
}
// insert data
function insert($data)
{
$this->db->insert($this->table, $data);
}
// update data
function update($id, $data)
{
$this->db->where($this->id, $id);
$this->db->update($this->table, $data);
}
// delete data
function delete($id)
{
$this->db->where($this->id, $id);
$this->db->delete($this->table);
}
}
Kode Controller :
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Nota_dinas extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('Nota_dinas_model');
$this->load->library('form_validation');
}
public function index()
{
$q = urldecode($this->input->get('q', TRUE));
$start = intval($this->input->get('start'));
if ($q <> '') {
$config['base_url'] = base_url() . 'nota_dinas/index.html?q=' . urlencode($q);
$config['first_url'] = base_url() . 'nota_dinas/index.html?q=' . urlencode($q);
} else {
$config['base_url'] = base_url() . 'nota_dinas/index.html';
$config['first_url'] = base_url() . 'nota_dinas/index.html';
}
$config['per_page'] = 10;
$config['page_query_string'] = TRUE;
$config['total_rows'] = $this->Nota_dinas_model->total_rows($q);
$nota_dinas = $this->Nota_dinas_model->get_limit_data($config['per_page'], $start, $q);
$this->load->library('pagination');
$this->pagination->initialize($config);
$data = array(
'nota_dinas_data' => $nota_dinas,
'q' => $q,
'pagination' => $this->pagination->create_links(),
'total_rows' => $config['total_rows'],
'start' => $start,
);
$this->load->view('nota_dinas/nota_dinas_list', $data);
}
public function read($id)
{
$row = $this->Nota_dinas_model->get_by_id($id);
if ($row) {
$data = array(
'id_notadinas' => $row->id_notadinas,
'id_kegiatan' => $row->id_kegiatan,
'id_rekening' => $row->id_rekening,
'id_maksud' => $row->id_maksud,
'no_notadinas' => $row->no_notadinas,
'tgl_notadinas' => $row->tgl_notadinas,
'dari_notadinas' => $row->dari_notadinas,
'kepada_notadinas' => $row->kepada_notadinas,
'tembusan_notadinas' => $row->tembusan_notadinas,
'perihal_notadinas' => $row->perihal_notadinas,
'id_tujuan' => $row->id_tujuan,
'tgl_pergi' => $row->tgl_pergi,
'tgl_kembali' => $row->tgl_kembali,
'lama_pelaksanaan' => $row->lama_pelaksanaan,
);
$this->load->view('nota_dinas/nota_dinas_read', $data);
} else {
$this->session->set_flashdata('message', 'Record Not Found');
redirect(site_url('nota_dinas'));
}
}
public function create()
{
$data = array(
'button' => 'Create',
'action' => site_url('nota_dinas/create_action'),
'id_notadinas' => set_value('id_notadinas'),
'id_kegiatan' => set_value('id_kegiatan'),
'id_rekening' => set_value('id_rekening'),
'id_maksud' => set_value('id_maksud'),
'no_notadinas' => set_value('no_notadinas'),
'tgl_notadinas' => set_value('tgl_notadinas'),
'dari_notadinas' => set_value('dari_notadinas'),
'kepada_notadinas' => set_value('kepada_notadinas'),
'tembusan_notadinas' => set_value('tembusan_notadinas'),
'perihal_notadinas' => set_value('perihal_notadinas'),
'id_tujuan' => set_value('id_tujuan'),
'tgl_pergi' => set_value('tgl_pergi'),
'tgl_kembali' => set_value('tgl_kembali'),
'lama_pelaksanaan' => set_value('lama_pelaksanaan'),
);
$this->load->view('nota_dinas/nota_dinas_form', $data);
}
public function create_action()
{
$this->_rules();
if ($this->form_validation->run() == FALSE) {
$this->create();
} else {
$data = array(
'id_kegiatan' => $this->input->post('id_kegiatan',TRUE),
'id_rekening' => $this->input->post('id_rekening',TRUE),
'id_maksud' => $this->input->post('id_maksud',TRUE),
'no_notadinas' => $this->input->post('no_notadinas',TRUE),
'tgl_notadinas' => $this->input->post('tgl_notadinas',TRUE),
'dari_notadinas' => $this->input->post('dari_notadinas',TRUE),
'kepada_notadinas' => $this->input->post('kepada_notadinas',TRUE),
'tembusan_notadinas' => $this->input->post('tembusan_notadinas',TRUE),
'perihal_notadinas' => $this->input->post('perihal_notadinas',TRUE),
'id_tujuan' => $this->input->post('id_tujuan',TRUE),
'tgl_pergi' => $this->input->post('tgl_pergi',TRUE),
'tgl_kembali' => $this->input->post('tgl_kembali',TRUE),
'lama_pelaksanaan' => $this->input->post('lama_pelaksanaan',TRUE),
);
$this->Nota_dinas_model->insert($data);
$this->session->set_flashdata('message', 'Create Record Success');
redirect(site_url('nota_dinas'));
}
}
public function update($id)
{
$row = $this->Nota_dinas_model->get_by_id($id);
if ($row) {
$data = array(
'button' => 'Update',
'action' => site_url('nota_dinas/update_action'),
'id_notadinas' => set_value('id_notadinas', $row->id_notadinas),
'id_kegiatan' => set_value('id_kegiatan', $row->id_kegiatan),
'id_rekening' => set_value('id_rekening', $row->id_rekening),
'id_maksud' => set_value('id_maksud', $row->id_maksud),
'no_notadinas' => set_value('no_notadinas', $row->no_notadinas),
'tgl_notadinas' => set_value('tgl_notadinas', $row->tgl_notadinas),
'dari_notadinas' => set_value('dari_notadinas', $row->dari_notadinas),
'kepada_notadinas' => set_value('kepada_notadinas', $row->kepada_notadinas),
'tembusan_notadinas' => set_value('tembusan_notadinas', $row->tembusan_notadinas),
'perihal_notadinas' => set_value('perihal_notadinas', $row->perihal_notadinas),
'id_tujuan' => set_value('id_tujuan', $row->id_tujuan),
'tgl_pergi' => set_value('tgl_pergi', $row->tgl_pergi),
'tgl_kembali' => set_value('tgl_kembali', $row->tgl_kembali),
'lama_pelaksanaan' => set_value('lama_pelaksanaan', $row->lama_pelaksanaan),
);
$this->load->view('nota_dinas/nota_dinas_form', $data);
} else {
$this->session->set_flashdata('message', 'Record Not Found');
redirect(site_url('nota_dinas'));
}
}
public function update_action()
{
$this->_rules();
if ($this->form_validation->run() == FALSE) {
$this->update($this->input->post('id_notadinas', TRUE));
} else {
$data = array(
'id_kegiatan' => $this->input->post('id_kegiatan',TRUE),
'id_rekening' => $this->input->post('id_rekening',TRUE),
'id_maksud' => $this->input->post('id_maksud',TRUE),
'no_notadinas' => $this->input->post('no_notadinas',TRUE),
'tgl_notadinas' => $this->input->post('tgl_notadinas',TRUE),
'dari_notadinas' => $this->input->post('dari_notadinas',TRUE),
'kepada_notadinas' => $this->input->post('kepada_notadinas',TRUE),
'tembusan_notadinas' => $this->input->post('tembusan_notadinas',TRUE),
'perihal_notadinas' => $this->input->post('perihal_notadinas',TRUE),
'id_tujuan' => $this->input->post('id_tujuan',TRUE),
'tgl_pergi' => $this->input->post('tgl_pergi',TRUE),
'tgl_kembali' => $this->input->post('tgl_kembali',TRUE),
'lama_pelaksanaan' => $this->input->post('lama_pelaksanaan',TRUE),
);
$this->Nota_dinas_model->update($this->input->post('id_notadinas', TRUE), $data);
$this->session->set_flashdata('message', 'Update Record Success');
redirect(site_url('nota_dinas'));
}
}
public function delete($id)
{
$row = $this->Nota_dinas_model->get_by_id($id);
if ($row) {
$this->Nota_dinas_model->delete($id);
$this->session->set_flashdata('message', 'Delete Record Success');
redirect(site_url('nota_dinas'));
} else {
$this->session->set_flashdata('message', 'Record Not Found');
redirect(site_url('nota_dinas'));
}
}
public function _rules()
{
$this->form_validation->set_rules('id_kegiatan', 'id kegiatan', 'trim|required');
$this->form_validation->set_rules('id_rekening', 'id rekening', 'trim|required');
$this->form_validation->set_rules('id_maksud', 'id maksud', 'trim|required');
$this->form_validation->set_rules('no_notadinas', 'no notadinas', 'trim|required');
$this->form_validation->set_rules('tgl_notadinas', 'tgl notadinas', 'trim|required');
$this->form_validation->set_rules('dari_notadinas', 'dari notadinas', 'trim|required');
$this->form_validation->set_rules('kepada_notadinas', 'kepada notadinas', 'trim|required');
$this->form_validation->set_rules('tembusan_notadinas', 'tembusan notadinas', 'trim|required');
$this->form_validation->set_rules('perihal_notadinas', 'perihal notadinas', 'trim|required');
$this->form_validation->set_rules('id_tujuan', 'id tujuan', 'trim|required');
$this->form_validation->set_rules('tgl_pergi', 'tgl pergi', 'trim|required');
$this->form_validation->set_rules('tgl_kembali', 'tgl kembali', 'trim|required');
$this->form_validation->set_rules('lama_pelaksanaan', 'lama pelaksanaan', 'trim|required');
$this->form_validation->set_rules('id_notadinas', 'id_notadinas', 'trim');
$this->form_validation->set_error_delimiters('<span class="text-danger">', '</span>');
}
}
Kode pada Read :
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="<?php echo base_url('assets/bootstrap/css/bootstrap.min.css') ?>"/>
<style>
body{
padding: 15px;
}
</style>
</head>
<body>
<h2 style="margin-top:0px">Nota_dinas Read</h2>
<table class="table">
<tr><td>Id Kegiatan</td><td><?php echo $id_kegiatan; ?></td></tr>
<tr><td>Id Rekening</td><td><?php echo $id_rekening; ?></td></tr>
<tr><td>Id Maksud</td><td><?php echo $id_maksud; ?></td></tr>
<tr><td>No Notadinas</td><td><?php echo $no_notadinas; ?></td></tr>
<tr><td>Tgl Notadinas</td><td><?php echo $tgl_notadinas; ?></td></tr>
<tr><td>Dari Notadinas</td><td><?php echo $dari_notadinas; ?></td></tr>
<tr><td>Kepada Notadinas</td><td><?php echo $kepada_notadinas; ?></td></tr>
<tr><td>Tembusan Notadinas</td><td><?php echo $tembusan_notadinas; ?></td></tr>
<tr><td>Perihal Notadinas</td><td><?php echo $perihal_notadinas; ?></td></tr>
<tr><td>Id Tujuan</td><td><?php echo $id_tujuan; ?></td></tr>
<tr><td>Tgl Pergi</td><td><?php echo $tgl_pergi; ?></td></tr>
<tr><td>Tgl Kembali</td><td><?php echo $tgl_kembali; ?></td></tr>
<tr><td>Lama Pelaksanaan</td><td><?php echo $lama_pelaksanaan; ?></td></tr>
<tr><td></td><td><a href="<?php echo site_url('nota_dinas') ?>" class="btn btn-default">Cancel</a></td></tr>
</table>
</body>
</html>
Mohon pencerahannya dan solusi lain jika ada. terima kasih
Tanggapan
coba pertanyann kamu dibikin simple. Kode yang ditampilkan juga bagian yang relevan saja. Coba bikin program dengan dua table sederhana, dan simulasikan dengan table A dan table B. biar orang yang mau bantu bisa lebih mudah
1 Jawaban:
intinya saya mau ambil id berdasarkan row di tabel dan id tersebut digunakan untuk pengisian form baru mas (untuk tabel yang berbeda)