2015-10-09 58 views
0

我有一個問題,當我想插入其中使用foreach循環(獲得價值形態數據庫)多個字段,這裏是我的表格:笨插入多個字段中使用的foreach循環

<form class="stdform" action="<?php echo $action_url; ?>" method="post"> 

    <label>Check Date</label> <input class="span3" type="text" 
     id="datepicker" name="check_date" value="" required="required" 
     placeholder="Tanggal cek" /> 

    <table class="table table-condensed table-hover table-bordered" 
     style="font-size: 12;"> 
     <thead> 
      <tr> 
       <th class="head0">#</th> 
       <th class="head0">Device</th> 
       <th class="head0">IP Address</th> 
       <th class="head0">NAT IP Address</th> 
       <th class="head0">Check OS</th> 
       <th class="head0">Application/Server</th> 
       <th class="head0">Shift A</th> 
       <th class="head0">Shift B</th> 
       <th class="head0">Remark</th> 
       <th class="head0">Report</th> 
      </tr> 
     </thead> 
     <tbody> 
      <!-- Row Template --> 
      <?php 
         $num=1; 
         foreach($get_server_assets as $row) { 

         ?> 
      <tr> 
       <td> 
        <?php echo $num; ?> 
       </td> 
       <td><input class="span12" type="text" name="device[]" 
        value="<?php echo $row->s_device; ?>" /></td> 
       <td><input class="span9" type="text" name="ip_address[]" 
        value="<?php echo $row->s_ip; ?>" /></td> 
       <td><input class="span9" type="text" name="nat_ip[]" 
        value="<?php echo $row->s_nat_ip; ?>" /></td> 
       <td><select class="span12" name="check_os[]"> 
         <option value=""></option> 
         <option value="Good">Good</option> 
         <option value="Not Good">Not Good</option> 
       </select></td> 
       <td><input class="span10" type="text" name="app_name[]" 
        value="<?php echo $row->app_server; ?>" /></td> 
       <td><select class="span12" name="check_app_a[]"> 
         <option value=""></option> 
         <option value="Good">Good</option> 
         <option value="Not Good">Not Good</option> 
       </select></td> 
       <td><select class="span12" name="check_app_b[]"> 
         <option value=""></option> 
         <option value="Good">Good</option> 
         <option value="Not Good">Not Good</option> 
       </select></td> 
       <td><input class="span12" type="text" name="remark[]" value="" /></td> 
       <td><input class="span12" type="text" name="report[]" value="" /></td> 
      </tr> 

      <?php $num++; } ?> 

     </tbody> 
    </table> 

    <p class="stdformbutton"> 

     <button class="btn btn-primary btn-large" type="submit" name="submit" 
      value="true">Submit</button> 
     &nbsp; <a class="btn btn-large" onclick="window.history.back()">Cancel</a> 

    </p> 

</form> 

這裏是我的控制器:

$data = array(); 

     $count = count($this->input->post('device')); 

     for($i=0; $i<$count; $i++) { 

      $data[] = array(
       'check_date'=>$this->input->post('check_date'), 
       'device'=>$this->input->post('device' . $i), 
       'ip_address'=>$this->input->post('ip_address' . $i), 
       'nat_ip'=>$this->input->post('nat_ip' . $i), 
       'check_os'=>$this->input->post('check_os' . $i), 
       'app_name'=>$this->input->post('app_name' . $i), 
       'check_app_a'=>$this->input->post('check_app_a' . $i), 
       'check_app_b'=>$this->input->post('check_app_b' . $i), 
       'remark'=>$this->input->post('remark' . $i), 
       'report'=>$this->input->post('report' . $i), 
      ); 

     } 

     $this->db->insert_batch('server_checklist',$data); 

     /*foreach($_POST['data'] as $d) { 

      $this->db->insert('server_checklist',$d); 

     } */ 

     redirect('admin/viewServerChecklist'); 

當我提交沒有數據插入時,只有check_date存儲。而其餘的給0值

Click here for image

有人可以幫我嗎?

+0

你爲什麼要加入$ i到每個變量的名字嗎?它不是一個數組,所以你會像$ this-> input-> post('check_os')[$ i]那樣訪問它嗎? – Silwerclaw

+0

啊謝謝@Silwerclaw可憐的我,你可以說這是我第一次使用數組的東西。非常感謝您的幫助。 – codekiddies

+0

只有一種形式,多行,你必須插入每行到db? –

回答

0

試試這個:

$data_table = $this->input->post(NULL, TRUE); 

     if (empty($data_table['device'])) 
     { 
      redirect('admin/viewServerChecklist?message=error'); 
     } 

     $required = array('device', 'ip_address', 'nat_ip', 'check_os', 'app_name', 'check_app_a', 'check_app_b', 'remark', 'report'); 
     $data = array(); 

     foreach ($data_table['device'] as $key => $value) 
     { 
      // check list table. if value required not exist. continue to next index 
      $row = array(); 
      foreach ($required as $key2 => $value2) 
      { 
       if (!isset($data_table[$value2][$key])) continue; 
       $row[$value2] = $data_table[$value2][$key]; 
      } 
      // since check_date is not array. we need to parse to row data 
      $row['check_date'] = $data_table['check_date']; 
      $data[] = $row; 
     } 

     $this->db->insert_batch('server_checklist', $data); 

     /*foreach($_POST['data'] as $d) { 

      $this->db->insert('server_checklist',$d); 

     } */ 

     redirect('admin/viewServerChecklist'); 
+0

謝謝,我會試試看。對不起,我剛剛閱讀你的評論。 – codekiddies

+0

非常感謝@aridjemana。問題解決:D – codekiddies

+0

歡迎光臨:) –

0

試試這個,而不是追加,嘗試訪問數組的索引。

$data[] = array(
      'check_date'=>$this->input->post('check_date'), 
      'device'=>$this->input->post('device')[$i], 
      'ip_address'=>$this->input->post('ip_address')[$i], 
      'nat_ip'=>$this->input->post('nat_ip')[$i], 
      'check_os'=>$this->input->post('check_os')[$i], 
      'app_name'=>$this->input->post('app_name')[$i], 
      'check_app_a'=>$this->input->post('check_app_a')[$i], 
      'check_app_b'=>$this->input->post('check_app_b')[$i], 
      'remark'=>$this->input->post('remark')[$i], 
      'report'=>$this->input->post('report')[$i], 
     ); 
+0

我在我的開發服務器(本地)中做到了,它工作正常!但它在我的生產服務器(互聯網)中不起作用。它給出了錯誤,我的瀏覽器全部變成白色,並且沒有返回任何錯誤消息。有什麼建議?破解代碼後的 – codekiddies

+0

? –

+0

是的,在我的生產服務器上。 – codekiddies