skip to Main Content

I have an account and diklat table, how do I want to insert data into diklat table that matches the login username?

Tabel Akun

enter image description here

Tabel diklat

enter image description here

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


  1. public function tambah() {
    

    // Get the logged-in user’s email from session data
    $user_email = $this->session->userdata(’email’);

    // Query the 'akun' table to get the user's data
    $user = $this->db->get_where('akun', ['email' => $user_email])->row_array();
    
    // Check if the user's data was found
    if ($user) {
        // Set up validation rules for the form fields
        $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'] = $user;
    
        if ($this->form_validation->run() == false) {
            // Load the form view with validation errors
            $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 {
            // Create an array with the form input data and the user's email
            $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'),
                'email'         => $user_email,
            ];
    
            // Insert the data into the 'diklat' table
            $this->db->insert('diklat', $array);
    
            // Set a success message in the session and redirect to the 'index' page
            $this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">Data Berhasil ditambah</div>');
            redirect('diklat/index');
        }
    } else {
        // If the user's data was not found, set an error message in the session and redirect to the 'login' page
        $this->session->set_flashdata('message', '<div class="alert alert-danger" role="alert">User tidak ditemukan</div>');
        redirect('auth/login');
    }
    

    }

    Login or Signup to reply.
  2. in variable $array field id_akun not declared to store in diklat table

    so in $array change to

    $array = [
        'id_akun'   => $data['akun']['id'],
        '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'),
    

    ];

    or

    $array = [
         'id_akun'   => $data['akun']->id,
         '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'),
    

    ];

    for get the name of user in view_diklat

     function view_diklat()
    {
        $email = $this->session->userdata('email'); 
        $query = "SELECT * FROM diklat JOIN akun ON diklat.id_akun = akun.id where akun.email = '$email' ";
        return $this->db->query($query)->result_array();
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search