I have an account and diklat table, how do I want to insert data into diklat table that matches the login username?
Tabel Akun
Tabel diklat
Controller diklat
public function tambah()
{
$this->form_validation->set_rules('f_namadiklat', 'nama_diklat', 'required|trim', ['required' => 'Nama Diklat Belum Diisi!']);
$this->form_validation->set_rules('f_penyelenggara', 'penyelenggara', 'required|trim', ['required' => 'Penyelenggara Diklat Harus Diisi!']);
$this->form_validation->set_rules('f_tempatdiklat', 'tempat_diklat', 'required|trim', ['required' => 'Tempat Diklat Harus Diisi!']);
$data['title'] = ' Tambah Diklat';
$data['akun'] = $this->db->get_where('akun', ['email' => $this->session->userdata('email')])->row_array();
if ($this->form_validation->run() == false) {
$this->load->view('templates/header', $data);
$this->load->view('templates/top_bar');
$this->load->view('templates/sidebarmenu');
$this->load->view('diklat/tambah');
$this->load->view('templates/footer');
}else
{
if ($data)
{
$array = [
'nama_diklat' => $this->input->post('f_namadiklat'),
'jenis_diklat' => $this->input->post('f_jenisdiklat'),
'penyelenggara' => $this->input->post('f_penyelenggara'),
'tempat_diklat' => $this->input->post('f_tempatdiklat'),
'waktu_diklat' => $this->input->post('tgl_diklat'),
];
//insert to database
$this->db->insert('diklat', $array);
$this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">
Data Berhasil ditambah</div>');
redirect('diklat/index');
}else
{
$this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">
Data Gagal Disimpan</div>');
redirect('diklat/tambah');
}
}
}
function get all data from tabel diklat join akun
when table diklat has been added successfully, data on a different id_akun also appears.
function view_diklat()
{
$query = "SELECT * FROM diklat JOIN akun ON diklat.id_akun = akun.id";
return $this->db->query($query)->result_array();
}
controller function index diklat
public function index()
{
$data['title'] = 'Pelatihan & Seminar';
$data['akun'] = $this->db->get_where('akun', ['email' => $this->session->userdata('email')])->row_array();
$data['data'] = $this->diklat_m->view_diklat();
$this->load->view('templates/header', $data);
$this->load->view('templates/sidebarmenu');
$this->load->view('templates/top_bar');
$this->load->view('diklat/index');
$this->load->view('templates/footer');
}
2
Answers
// Get the logged-in user’s email from session data
$user_email = $this->session->userdata(’email’);
}
“
in variable $array field id_akun not declared to store in diklat table
so in $array change to
];
or
];
for get the name of user in view_diklat