Get data from the same form registration in Codeigniter

  codeigniter, forms, html, mysql, php

enter image description here

Based on the picture, this is for student registration form. Father and mother information would be in student table. But for guardian information would be at guardians table. In the form registration. I want to carry all information in "Father Information" when I choose "Father" at selection in "Guardian Information".

Controller
”’
public function add($id = null) {

    check_permission(ADD);
 
    if ($_POST) {
        
        $school_id = $this->input->post('school_id');
        $class_id  = $this->input->post('class_id'); 
       
        $HQ  = $this->input->post('HQ');   


        $this->_prepare_student_validation();
        if ($this->form_validation->run() === TRUE) {
            $data = $this->_get_posted_student_data();
          

            if($this->input->post('lead_id') && $this->input->post('lead_id') != ''){  
                $condition_lead = array();
                $condition_lead['id'] = $this->input->post('lead_id');
                $data_lead['is_converted'] = 1;
                $this->db->update('student_leads', $data_lead, $condition_lead);
            }

            $insert_id = $this->student->insert('students', $data);
            
            if ($insert_id) {
                $this->__insert_enrollment($insert_id);
                // create_log('Has been added a student : '. $data['name']);    
                $this->session->set_flashdata('filter_HQ','schools_mode');   
                success($this->lang->line('insert_success'));
                redirect('student/index/'.$this->input->post('class_id'));
            } else {
                error($this->lang->line('insert_failed'));
                redirect('student/add');
            }
        } else {

            error($this->lang->line('insert_failed'));
            $this->data['post'] = $_POST;
            $this->data['school_id'] = $_POST['school_id'];
        }
    }

    
    if($id){
        $this->data['lead'] = $this->student->get_list('student_leads', array('id' => $id,'is_converted' => 0), '', '', '', 'id', 'ASC');
        $this->data['lead_school'] = $this->student->get_list('schools', array('school_name' => $this->data['lead'][0]->preferred_school), 'id', '', '', 'id', 'ASC');
        if(count($this->data['lead']) == 0){
            error('No data for this lead or lead already converted to student');
            redirect('student/add');
        }
    }
    
    
    $class_id = $this->uri->segment(4);
    if(!$class_id){
      $class_id = $this->input->post('class_id');
    }

    $this->data['lead_id'] = $id;
    $this->data['class_id'] = $class_id;



    if($this->session->userdata('role_id') != SUPER_ADMIN && $this->session->userdata('role_id') != ADMIN_HQ && $this->session->userdata('role_id') != HUMAN_RESOURCE_HQ && 
    $this->session->userdata('role_id') != ACCOUNTING_HQ && $this->session->userdata('role_id') != FINANCE_HQ && $this->session->userdata('role_id') != MARKETING_HQ && 
    $this->session->userdata('role_id') != STUDENT_MANAGEMENT_HQ && $this->session->userdata('role_id') != CHIEF && $this->session->userdata('role_id') != EDUCATION_HQ){
        $this->data['students'] = $this->student->get_student_list($class_id, $school_id, $school->academic_year_id,$HQ);
    }else{
        if($this->input->post('HQ') != "" ){
            $this->data['students'] = $this->student->get_student_list($class_id, $school_id, $school->academic_year_id,$HQ);
        }
    }
   

    // $this->data['students'] = $this->student->get_student_list($class_id);
    $this->data['roles'] = $this->student->get_list('roles', array('status' => 1), '', '', '', 'id', 'ASC');
    
     
    $condition = array();
    $condition['status'] = 1;        
    if($this->session->userdata('role_id') != SUPER_ADMIN && $this->session->userdata('role_id') != ADMIN_HQ && $this->session->userdata('role_id') != HUMAN_RESOURCE_HQ && 
    $this->session->userdata('role_id') != ACCOUNTING_HQ && $this->session->userdata('role_id') != FINANCE_HQ && $this->session->userdata('role_id') != MARKETING_HQ && 
    $this->session->userdata('role_id') != STUDENT_MANAGEMENT_HQ && $this->session->userdata('role_id') != CHIEF && $this->session->userdata('role_id') != EDUCATION_HQ ){  
        
        $condition['school_id'] = $this->session->userdata('school_id');
        $this->data['classes'] = $this->student->get_list('classes', $condition, '','', '', 'id', 'ASC');
        $this->data['discounts'] = $this->student->get_list('discounts', $condition, '','', '', 'id', 'ASC');
        $this->data['guardians'] = $this->student->get_list('guardians', $condition, '','', '', 'id', 'ASC');
        $this->data['class_list'] = $this->student->get_list('classes', $condition, '','', '', 'id', 'ASC');
        $this->data['types']      = $this->student->get_list('student_types', $condition, '','', '', 'id', 'ASC');
    }
    
    $this->data['schools'] = $this->schools;
    $this->data['add'] = TRUE;
    $this->layout->title($this->lang->line('add'). ' | ' . SMS);
    $this->layout->view('student/index', $this->data);
}

”’

Model

”’
public function get_student_list($class_id = null, $school_id = null, $academic_year_id = null, $HQ = null ){

    $this->db->select('S.*, SC.school_name, E.roll_no, E.class_id, U.username, U.role_id,  C.name AS class_name, SE.name AS section');
    $this->db->from('enrollments AS E');
    $this->db->join('students AS S', 'S.id = E.student_id', 'left');
    $this->db->join('users AS U', 'U.id = S.user_id', 'left');
    $this->db->join('classes AS C', 'C.id = E.class_id', 'left');
    $this->db->join('sections AS SE', 'SE.id = E.section_id', 'left');
    $this->db->join('schools AS SC', 'SC.id = S.school_id', 'left');
    
    if($academic_year_id){
        $this->db->where('E.academic_year_id', $academic_year_id); 
    }
    if($class_id && $class_id != 'all'){
        $this->db->where('E.class_id', $class_id);
    }

    if($HQ != 'all' && $HQ != 'schools_mode' && $HQ != ''){
        $this->db->where('SC.group_by', $HQ);
    }
            
    if($this->session->userdata('role_id') == GUARDIAN){
       $this->db->where('S.guardian_id', $this->session->userdata('profile_id'));
    }
    
    if($this->session->userdata('role_id') == STUDENT){
       $this->db->where('S.id', $this->session->userdata('profile_id'));
    }
    
    if($this->session->userdata('role_id') != SUPER_ADMIN && $this->session->userdata('role_id') != ADMIN_HQ && $this->session->userdata('role_id') != HUMAN_RESOURCE_HQ && 
    $this->session->userdata('role_id') != ACCOUNTING_HQ && $this->session->userdata('role_id') != FINANCE_HQ && $this->session->userdata('role_id') != MARKETING_HQ && 
    $this->session->userdata('role_id') != STUDENT_MANAGEMENT_HQ && $this->session->userdata('role_id') != CHIEF && $this->session->userdata('role_id') != EDUCATION_HQ ){
        $this->db->where('S.school_id', $this->session->userdata('school_id'));
    }
    
    if($school_id){
        $this->db->where('S.school_id', $school_id); 
    } 
    $this->db->where('SC.status', 1);
    $this->db->order_by('E.roll_no', 'ASC');
   
    return $this->db->get()->result();
    
}

public function get_single_student($id,  $academic_year_id){
    
    $this->db->select('S.*,  SC.school_name, T.type, D.amount, D.title AS discount_title, SC.school_name, G.name as guardian, E.academic_year_id, E.roll_no, E.class_id, E.section_id, U.username, U.role_id, R.name AS role,  C.name AS class_name, SE.name AS section');
    $this->db->from('enrollments AS E');
    $this->db->join('students AS S', 'S.id = E.student_id', 'left');
    $this->db->join('users AS U', 'U.id = S.user_id', 'left');
    $this->db->join('roles AS R', 'R.id = U.role_id', 'left');
    $this->db->join('classes AS C', 'C.id = E.class_id', 'left');
    $this->db->join('sections AS SE', 'SE.id = E.section_id', 'left');
    $this->db->join('guardians AS G', 'G.id = S.guardian_id', 'left');
    $this->db->join('schools AS SC', 'SC.id = S.school_id', 'left');
    $this->db->join('discounts AS D', 'D.id = S.discount_id', 'left');
    $this->db->join('student_types AS T', 'T.id = S.type_id', 'left');
    $this->db->where('S.id', $id);
    $this->db->where('E.academic_year_id', $academic_year_id);

View
”’

lang->line(‘guardian_information’); ?>:

lang->line(‘is_guardian’); ?> *

–lang->line(‘select’); ?>–
>lang->line(‘father’); ?>
>lang->line(‘mother’); ?>
>lang->line(‘other’); ?>
>lang->line(‘guardian_exist’); ?>

“>

lang->line(‘guardian’); ?> *

–lang->line(‘select’); ?>–

id; ?>” id ? ‘selected=”selected”‘ : ”; ?>>name; ?>

lang->line(‘relation_with_guardian’); ?>
” placeholder=”lang->line(‘relation_with_guardian’); ?>” type=”text”>

fn_except_exist”>

                                    <div class="col-md-3 col-sm-3 col-xs-12">
                                        <div class="item form-group">
                                            <label for="gud_name"><?php echo $this->lang->line('name'); ?> <span class="required">*</span></label>
                                            <input  class="form-control col-md-7 col-xs-12"  name="gud_name"  id="add_gud_name" value="<?php echo isset($post['gud_name']) ?  $post['gud_name'] : ''; ?>" placeholder="<?php echo $this->lang->line('name'); ?>" required="required" type="text">
                                            <div class="help-block"><?php echo form_error('gud_name'); ?></div>
                                        </div>
                                    </div>
                                    <div class="col-md-3 col-sm-3 col-xs-12">
                                        <div class="item form-group">
                                            <label for="gud_phone"><?php echo $this->lang->line('phone'); ?> <span class="required">*</span></label>
                                            <input  class="form-control col-md-7 col-xs-12"  name="gud_phone"  id="add_gud_phone" value="<?php echo isset($post['gud_phone']) ?  $post['gud_phone'] : ''; ?>" placeholder="<?php echo $this->lang->line('phone'); ?>" required="required" type="text">
                                            <div class="help-block"><?php echo form_error('phone'); ?></div>
                                        </div>
                                    </div>
                                    <div class="col-md-3 col-sm-3 col-xs-12">
                                        <div class="item form-group">
                                            <label for="gud_email"><?php echo $this->lang->line('email'); ?><span class="required">*</span> </label>
                                            <input  class="form-control col-md-7 col-xs-12"  name="gud_email"  id="add_gud_email" value="<?php echo isset($post['gud_email']) ?  $post['gud_email'] : ''; ?>" placeholder="<?php echo $this->lang->line('email'); ?>" required="email" type="email">
                                            <div class="help-block"><?php echo form_error('gud_email'); ?></div>
                                        </div>
                                    </div>
                                    <div class="col-md-3 col-sm-3 col-xs-12">
                                        <div class="item form-group">
                                            <label for="gud_profession"><?php echo $this->lang->line('profession'); ?></label>
                                            <input  class="form-control col-md-7 col-xs-12"  name="gud_profession"  id="add_gud_profession" value="<?php echo isset($post['gud_profession']) ?  $post['gud_profession'] : ''; ?>" placeholder="<?php echo $this->lang->line('profession'); ?>"  type="text">
                                            <div class="help-block"><?php echo form_error('gud_profession'); ?></div>
                                        </div>
                                    </div>                                   
                                    <div class="col-md-3 col-sm-3 col-xs-12">
                                        <div class="item form-group">
                                            <label for="gud_religion"><?php echo $this->lang->line('religion'); ?> </label>
                                            <input  class="form-control col-md-7 col-xs-12"  name="gud_religion"  id="add_gud_religion" value="<?php echo isset($post['gud_religion']) ?  $post['gud_religion'] : ''; ?>" placeholder="<?php echo $this->lang->line('religion'); ?>" type="text">
                                            <div class="help-block"><?php echo form_error('gud_religion'); ?></div>
                                        </div>
                                    </div>
                                    <div class="col-md-3 col-sm-3 col-xs-12">
                                        <div class="item form-group">
                                            <label for="gud_national_id"><?php echo $this->lang->line('national_id'); ?></label>
                                            <input  class="form-control col-md-7 col-xs-12"  name="gud_national_id"  id="add_gud_national_id" value="<?php echo isset($post['gud_national_id']) ?  $post['gud_national_id'] : ''; ?>" placeholder="<?php echo $this->lang->line('national_id'); ?>"  type="text">
                                            <div class="help-block"><?php echo form_error('gud_national_id'); ?></div>
                                        </div>
                                    </div>
                                    <div class="col-md-3 col-sm-3 col-xs-12">
                                        <div class="item form-group">
                                            <label for="gud_username"><?php echo $this->lang->line('username'); ?><span class="required">*</span></label>
                                            <input  class="form-control col-md-7 col-xs-12"  name="gud_username"  id="add_gud_username" value="<?php echo isset($post['gud_username']) ?  $post['gud_username'] : ''; ?>" placeholder="<?php echo $this->lang->line('username'); ?>"  type="text" required="required">
                                            <div class="help-block"><?php echo form_error('gud_username'); ?></div>
                                        </div>
                                    </div>                                        

                                </div>
                                
                                <div class="row">    
                                    <div class="col-md-6 col-sm-6 col-xs-12">
                                        <div class="item form-group">
                                            <label for="gud_present_address"><?php echo $this->lang->line('present'); ?> <?php echo $this->lang->line('address'); ?></label>
                                            <textarea  class="form-control col-md-7 col-xs-12 textarea-4column"  name="gud_present_address"  id="add_gud_present_address" placeholder="<?php echo $this->lang->line('present'); ?> <?php echo $this->lang->line('address'); ?>"><?php echo isset($post['gud_present_address']) ?  $post['gud_present_address'] : ''; ?></textarea>
                                            <div class="help-block"><?php echo form_error('gud_present_address'); ?></div>
                                        </div>
                                    </div>

                                    <div class="col-md-6 col-sm-6 col-xs-12">
                                        <div class="item form-group">
                                            <label for="gud_permanent_address"><?php echo $this->lang->line('permanent'); ?> <?php echo $this->lang->line('address'); ?></label>
                                            <textarea  class="form-control col-md-7 col-xs-12 textarea-4column"  name="gud_permanent_address"  id="add_gud_permanent_address" placeholder="<?php echo $this->lang->line('permanent'); ?> <?php echo $this->lang->line('address'); ?>"><?php echo isset($post['gud_permanent_address']) ?  $post['gud_permanent_address'] : ''; ?></textarea>
                                            <div class="help-block"><?php echo form_error('gud_permanent_address'); ?></div>
                                        </div>
                                    </div>  
                                </div>
                                
                                <div class="row">
                                    <div class="col-md-6 col-sm-6 col-xs-12">
                                        <div class="item form-group">
                                            <label for="other_info"><?php echo $this->lang->line('other_info'); ?> </label>
                                            <textarea  class="form-control col-md-7 col-xs-12 textarea-4column"  name="gud_other_info"  id="add_gud_other_info" placeholder="<?php echo $this->lang->line('other_info'); ?>"><?php echo isset($post['gud_other_info']) ?  $post['gud_other_info'] : ''; ?></textarea>
                                            <div class="help-block"><?php echo form_error('gud_other_info'); ?></div>
                                        </div>
                                    </div>                                        
                                </div>
                                
                            </div>

”’

Source: Ask PHP

LEAVE A COMMENT