Search code examples
phpdatabasecodeigniterinsertmultiple-select

How to insert multiple selects dropdown values in database, php codeigniter


I am trying to select multiple staffs and insert all in same row and same column in projects table, in database. But right now i can only insert one value in database.Can you help me how can i insert all the selected data in the database? Here is my Project.php controller

public function index ()
{
    // print_r($_REQUEST);
    //   die;
  $data['company_name'] = $this->project_model->getAllCompanyName();
  $data['project'] = $this->project_model->getProjectDetails();
  //echo "<pre>";
  //print_r($data);die;
  $this->load->view('admin/project/index',$data);
}
function add()
{
  $this->form_validation->set_rules('Pname', 'Project Name', 'required');
  $this->form_validation->set_rules('Cname', 'Client Name' , 'required');
  $this->form_validation->set_rules('PassignTo', 'Company', 'required');
  $this->form_validation->set_rules('manager', 'Manager' , 'required');
  $this->form_validation->set_rules('staff', 'Support Staff', 'required');
  
  $data['company_name'] = $this->project_model->getAllCompanyName();
  $data['project'] = $this->project_model->getProjectDetails();

  if ($this->form_validation->run() ==true)
  {
    $this->project_model->add(); 
    $this->session->set_flashdata ('success','Project Added Sucessfully');

    //   print_r($_REQUEST);
    //  die;
    //   echo "<pre>";
    redirect('admin/project/index',$data);
  }
  else{
    $this->load->view('admin/project/add',$data);
  }
  public function getManager()
{
  //echo json_encode ("sdf"); die; 
   //print_r($_REQUEST);
    //die;
  $company_name = $this->input->post('company_name');
  $getallmanager = $this->project_model->get_manager_query($company_name);
  $getallstaff = $this->project_model->get_all_staff($company_name);
  $all_the_mangers = '';
  $all_the_staffs = '';
  if(count($getallmanager)>0)
  {        
    foreach ($getallmanager as $manager){
      $all_the_mangers .='<option value="' .$manager->first_name.'">'.$manager->first_name.'</option>';
      
    }
    
  }
  if(count($getallstaff)>0)
  {        
    foreach ($getallstaff as $staff){
      $all_the_staffs .='<option value="' .$staff->id.'">'.$staff->first_name.'</option>';
    }
    
  }
  $result = array('manager'=>$all_the_mangers,'staffs'=>$all_the_staffs);
  echo json_encode($result);die;
  
}

Here is Project_model.php model

 function add()
 {
    $arr['project_name'] = $this->input->post('Pname');
    $arr['client_name'] = $this->input->post('Cname');
    $arr['company'] = $this->input->post('PassignTo');
    $arr['project_manager'] = $this->input->post('manager');
    $arr['support_staff'] = $this->input->post('staff');
    $this->db->insert('projects',$arr);

  }
 public function get_all_staff($company_name)
{
    $query = $this->db->get_where('user_login', array('company_name' => $company_name,'role !='=>'manager'));
    return $query->result();
}

And here is the view

 <div class="form-group col-md-4">
              <label for="pwd">Add Support Staff</label>
              <select id="addStaffMulti" placeholder="Selecct" multiple="multiple" name="staff" value="<?php echo set_value('staff'); ?>">
              <div class="alert-danger"><?php echo form_error('staff'); ?></div>
                <option value="">Select Staff</option>
            </select>
            </div>

And here is the script

<script type="text/javascript">
$(document).ready(function(){
$('#company').on('change' , function() {
var company_name = $(this).val();
if(company_name == '')
{
  $('#manager').prop('disabled', true);
  $('#addStaffMulti').prop('disabled', true);

}
else
{
   $('#manager' ).prop('disabled', false);
   $('#addStaffMulti').prop('disabled', false);
  var url = "<?php echo base_url()?>getManager";
  //alert(url);
  //return false;
   $.ajax({
     url:"<?php echo base_url()?>getManager",
     type: "POST",
     data: { 'company_name' : company_name},
     dataType:'json',
     success: function(data){
       //alert('ok');
       console.log(data);
       $('#manager').html(data.manager);
       $('#addStaffMulti').html(data.staffs);
       $('#addStaffMulti').multiselect('rebuild');
     },
     error: function(event){
       console.log(event);
       alert('Error occur...');
     }
   });
   }
  });
 });
</script>

Solution

  • You should change your db field type to text and when inserting that multi field you'll need to json_encode( $your_array ) it. That will stringify the array. And after when you need to read it, you should use the reverse function - json_decode( $your_array, true ). The second argument is for whether to be array or object.

    So that line would become like:

    $arr['support_staff'] = json_encode($this->input->post('staff'));
    

    And after you read it:

    $support_staff = json_decode($this->db->support_staff, true );