2017-11-23 5 views
0

この質問は以前に聞いたことがある場合は申し訳ありません。私は以下のスクリプトをチェックするのに助けを求めます。なぜなら、複数の入力フィールドを持つフォームを送信しようとすると、1つのデータしか得られないのに対して、データベースには2つのデータが入力されるはずです。複数のデータコードを入力する

私のスクリプトのどの部分が間違っていますか?

コントローラ

public function add() { 
    // ... some script before 'else' ... 
    } else { 

     $post = $this->input->post(); 
     $result = array(); 
     $total_input = count($post['input_acc_code']); 
     foreach ($post['input_acc_code'] as $key => $value) { 
      $result[] = array(
       'trans_type' => 'journal', 
       'form_type' => NULL, 
       'acc_code' => $post['input_acc_code'][$key], 
       'acc_type_id' => $post['input_acc_type_id'][$key], 
       'refference' => '', 
       'customer_ID' => NULL, 
       'acc_side' => '', 
       'debet' => $post['input_debet'][$key], 
       'credit' => $post['input_credit'][$key], 
       'summary' => $post['input_note'][$key], 
       'files' => NULL, 
       'create_at' => date("Y-m-d H:i:s", strtotime("now")) 
       ); 
      if($this->model_transaction->savedata('fi_acc_journal', $result) == TRUE) { 

       $this->session->set_flashdata('alert', 'Success'); 
       redirect(base_url().'admin/transaction'); 
      } else { 
       $this->session->set_flashdata('alert', 'Failed'); 
       redirect(base_url().'admin/transaction'); 
      } 
     } 
    } 
} 

モデル

function savedata($table, $data = array()) { 
    $this->db->insert_batch($table, $data); 
    if($this->db->affected_rows() > 0) { 
     return TRUE; 
    } 
    return FALSE; 
} 

ビュー

<?php $attributes = array('class' => 'form-horizontal', 'id' => ''); 
echo form_open_multipart(base_url().$this->session->userdata('user_status').'/transaction/add', $attributes);?> 
<div class="row"> 
    <div class="col-sm-12 col-md-12 panel-form-input"> 
     <div class="form-group form-group-sm"> 
      <label for="input_datetime" class="col-sm-2 control-label">Tanggal Transaksi</label> 
      <div class="col-sm-10"> 
       <input type="text" class="input-date form-control" name="input_datetime[]" id="input-date"> 
       <?php echo form_error('input_datetime');?> 
      </div> 
     </div> 
    </div> 
</div> 
<div class="row"> 
    <div class="col-sm-12 col-md-12 panel-form-input"> 
     <div class="panel panel-default"> 
      <div class="table-responsive"> 
       <table class="table table-unbordered"> 
        <thead> 
         <th class="col-25">Account</th> 
         <th class="col-5">Account Type</th> 
         <th class="col-35">Notes</th> 
         <th class="col-15">Debet</th> 
         <th class="col-15">Credit</th> 
         <th class="col-5"></th> 
        </thead> 
        <tbody> 

         // First Input Field Form Table 

         <tr> 
          <td class="col-25"> 
           <div class="form-group form-group-sm"> 
            <div class="col-sm-12"> 
             <select class="select-transaction input-group-sm form-control" name="input_acc_code[]" id="acc_code_1"> 
             <?php if ($account_list != NULL): ?> 
              <option>— Choose Account Number —</option> 
              <?php foreach ($account_list as $value): ?> 
              <option value="<?php echo $value->acc_code;?>"><?php echo $value->acc_name;?></option> 
              <?php endforeach;?> 
              <?php else:?> 
              <option>— No Data —</option> 
             <?php endif;?> 
             </select> 
             <?php echo form_error('input_acc_code[]');?> 
            </div> 
           </div> 
          </td> 
          <td class="col-5"> 
           <div class="form-group form-group-sm"> 
            <div class="col-sm-12"> 
             <input type="text" class="form-control" name="input_acc_type_id[]" id="acc_type_id_1"> 
             <?php echo form_error('input_acc_type_id[]');?> 
            </div> 
           </div> 
          </td> 
          <td class="col-35"> 
           <div class="form-group form-group-sm"> 
            <div class="col-sm-12"> 
             <input type="text" class="form-control" name="input_note[]"> 
             <?php echo form_error('input_note[]');?> 
            </div> 
           </div> 
          </td> 
          <td class="col-15"> 
           <div class="form-group form-group-sm"> 
            <div class="col-sm-12"> 
             <input type="text" class="form-control" name="input_debet[]"> 
            </div> 
           </div> 
          </td> 
          <td class="col-15"> 
           <div class="form-group form-group-sm"> 
            <div class="col-sm-12"> 
             <input type="text" class="form-control" name="input_credit[]"> 
            </div> 
           </div> 
          </td> 
          <td class="col-5"></td> 
         </tr> 

         // Second Input Field Form Table 

         <tr> 
          <td class="col-25"> 
           <div class="form-group form-group-sm"> 
            <div class="col-sm-12"> 
             <select class="select-transaction input-group-sm form-control" name="input_acc_code[]" id="acc_code_1"> 
             <?php if ($account_list != NULL): ?> 
              <option>— Choose Account Number —</option> 
              <?php foreach ($account_list as $value): ?> 
              <option value="<?php echo $value->acc_code;?>"><?php echo $value->acc_name;?></option> 
              <?php endforeach;?> 
              <?php else:?> 
              <option>— No Data —</option> 
             <?php endif;?> 
             </select> 
             <?php echo form_error('input_acc_code[]');?> 
            </div> 
           </div> 
          </td> 
          <td class="col-5"> 
           <div class="form-group form-group-sm"> 
            <div class="col-sm-12"> 
             <input type="text" class="form-control" name="input_acc_type_id[]" id="acc_type_id_1"> 
             <?php echo form_error('input_acc_type_id[]');?> 
            </div> 
           </div> 
          </td> 
          <td class="col-35"> 
           <div class="form-group form-group-sm"> 
            <div class="col-sm-12"> 
             <input type="text" class="form-control" name="input_note[]"> 
             <?php echo form_error('input_note[]');?> 
            </div> 
           </div> 
          </td> 
          <td class="col-15"> 
           <div class="form-group form-group-sm"> 
            <div class="col-sm-12"> 
             <input type="text" class="form-control" name="input_debet[]"> 
            </div> 
           </div> 
          </td> 
          <td class="col-15"> 
           <div class="form-group form-group-sm"> 
            <div class="col-sm-12"> 
             <input type="text" class="form-control" name="input_credit[]"> 
            </div> 
           </div> 
          </td> 
          <td class="col-5"></td> 
         </tr> 
        </tbody> 
       </table> 
      </div> 
     </div> 
    </div> 
</div> 
<div class="row"> 
    <div class="col-sm-12 col-md-12"> 
     <div class="menu-bar"> 
      <button class="btn btn-md btn-primary" type="submit">Save</input> 
     </div> 
    </div> 
</div> 
<?php echo form_close();?> 

はあなたの助けをありがとう....

答えて

0

insert_batch関連するモデル呼び出しをforeachから移動してみてください。それ以外の場合は、まっすぐな挿入を実行するだけです。まず、挿入するクエリを構築していて、foreachの後に多次元配列を挿入します。

public function add() 
{ 
    $post = $this->input->post(); 
    $result = array(); 
    $total_input = count($post['input_acc_code']); 
    foreach ($post['input_acc_code'] as $key => $value) { 
     $result[] = array(
      'trans_type' => 'journal', 
      'form_type' => NULL, 
      'acc_code' => $post['input_acc_code'][$key], 
      'acc_type_id' => $post['input_acc_type_id'][$key], 
      'refference' => '', 
      'customer_ID' => NULL, 
      'acc_side' => '', 
      'debet'  => $post['input_debet'][$key], 
      'credit'  => $post['input_credit'][$key], 
      'summary'  => $post['input_note'][$key], 
      'files'  => NULL, 
      'create_at' => date("Y-m-d H:i:s", strtotime("now")) 
     ); 
    }   
    if ($this->model_transaction->savedata('fi_acc_journal', $result) == TRUE) { 
     $this->session->set_flashdata('alert', 'Success'); 
     redirect(base_url() . 'admin/transaction'); 
    } else { 
     $this->session->set_flashdata('alert', 'Failed'); 
     redirect(base_url() . 'admin/transaction'); 
    } 
} 
+0

アレックスあなたの助けをありがとう。今は仕事です。申し訳ありませんが、foreach =の部分をチェックしないでください) –

+0

これがあなたの質問に答えるなら、それを受け入れてマークしてください:) – Alex

+0

また、日付機能でstrtotimeを行う必要はありません。 2番目の日付パラメータが空白のままであれば自動的にtime()を使用します。 – Alex

関連する問題