简体   繁体   中英

How to Save multiple data array in database

I create an addline array button that creates multiple array fields my problems is that I can't save the data that inserted to the fields.

JavaScript

  var MaxInputs       = 3; //maximum input boxes allowed
  var InputsWrapper = $("");
  var addlines = 1;
  var FieldCount = 1; //to keep track of text box added

function AddORDeleteLines(obj,type){
type=type+'';
var objtype=obj.id+'';
if(type==="add"){
     if(objtype==='AddMoreLines'){
    

        if(addlines<=MaxInputs){
            FieldCount++;
            InputsWrapper = $("#ADD_LINE-txtEmployee-div");
            $(InputsWrapper).append('<div class="class-div_'+FieldCount+'">'+
            '<input type="text" name="txtEmployee" size="30"/>'+
            '</div>');
            
            
        InputsWrapper = $("#ADD_LINE-txtDate-div");
            $(InputsWrapper).append('<div class="class-div_'+FieldCount+'">'+
            '<input type="date" name="txtDate"/>'+
            '</div>');
            
        InputsWrapper = $("#ADD_LINE-txtTimeFrom-div");
            $(InputsWrapper).append('<div class="class-div_'+FieldCount+'">'+
            '<input type="time" name="txtTimeFrom" />'+
            '</div>');
        InputsWrapper = $("#ADD_LINE-txtTimeTo-div");
            $(InputsWrapper).append('<div class="class-div_'+FieldCount+'">'+
            '<input type="time" name="txtTimeTo" />'+
            '<input type="button" onClick="AddORDeleteLines(this,'+"'delete'"+')" id="removeButton_'+FieldCount+'" value="x" class="removeLines"/>'+
            '</div>');      
            addlines++;
        }
    }
}
else if(type==="delete"){
        var parentobj = $("#"+obj.id).parent('div');
        //$(parentobj).parent('div').remove();
        var objclass = $("#"+obj.id).parent('div').attr("class")+'';
        objtype = $("#"+obj.id).attr("class")+'';
        if(objtype==='removeLines'){
            addlines--;
         }
        $("div").remove('.'+objclass);
        
    return false; 
}}

View

<table>
   <tr> 
       <td>
       <div id="ADD_LINE-txtEmployee-div">
       <input type="text" id="txtEmployee" name="txtEmployee" size="30"/>
    </div>
   </td>
   <td>
       <div id="ADD_LINE-txtDate-div">
      <div>
          <input type="date" id="txtDate" name="txtDate" />
      </div>    
       </div>
   </td>
   <td>
    <div id="ADD_LINE-txtTimeFrom-div">
         <div>
        <input type="time" id="txtTimeFrom" name="txtTimeFrom" />
         </div>
    </div>
   </td>
   <td>
    <div id="ADD_LINE-txtTimeTo-div">
         <div>
              <input type="time" id="txtTimeTo" name="txtTimeTo" />
                        <input type="button" id="AddMoreLines" onclick="AddORDeleteLines(this,'add')" value="ADD LINE" />
         </div>
    </div>
  </td>
    </tr>

Controller

public function SaveOvertime(){
$this->load->library('form_validation');
$this->form_validation->set_rules('txtEmployee[]', 'Employee', 'required');
$this->form_validation->set_rules('txtTimeFrom[]', 'Time From', 'required');
$this->form_validation->set_rules('txtTimeTo[]', 'Time to', 'required');
$this->form_validation->set_rules('txtDate[]', 'Date', 'required');

    if ($this->form_validation->run() == FALSE)
    {
          $this->load->view('error_overtime');
    }
    else
    {   
    $this->load->model('dtr_model');
    $this->dtr_model->saveovertime();
    $this->load->view('success_overtime');

    }

MODEL

function saveovertime(){
    $value = array(
        'EMPLOYEE'=>$this->input->post('txtEmployee[]'),
        'TIME_FROM'=>$this->input->post('txtTimeFrom[]'),
        'TIME_TO'=>$this->input->post('txtTimeTo[]'),
        'DATE'=>$this->input->post('txtDate[]'));   
$query = $this->db->insert('dtr_timerecord_overtime_line',$value);}

I think you need to store these multiple values of each field by using json_encode() or serialize() functions of php. Replace your saveovertime() function with below function:

function saveovertime(){
$value = array(
    'EMPLOYEE'=>json_encode($this->input->post('txtEmployee')),
    'TIME_FROM'=>json_encode($this->input->post('txtTimeFrom')),
    'TIME_TO'=>json_encode($this->input->post('txtTimeTo')),
    'DATE'=>json_encode($this->input->post('txtDate')));
$query = $this->db->insert('dtr_timerecord_overtime_line',$value);}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM