如何使用codeigniter在数据库中保存多个数据数组

时间:2014-03-21 03:33:41

标签: javascript php codeigniter

我创建了一个创建多个数组字段的地址数组按钮我的问题是我无法保存插入字段的数据。

* 强文 * JAVASCRIPT ON ADDING LINES

  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; 
}}

* 强文 *查看

<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>

* 强文 *控制器

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');

    }

* 强文 *模型

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);}

1 个答案:

答案 0 :(得分:0)

我认为你需要使用php的json_encode()或serialize()函数来存储每个字段的这些多个值。用以下函数替换saveovertime()函数:

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);}