2017-09-22 86 views
1

我是新的codeigniter。我有一種表格格式。具有表格格式的記錄我想在選擇它後只更改選擇框中的一個記錄,我想單擊多個複選框上的多個記錄&點擊提交按鈕時,記錄應該需要添加到數據庫表中。在codeigniter中的數據庫中插入多條記錄

表格:有支付模式column其中我正在更改value.before billId我有一個columncheckboxes。通過點擊它我想提交數據。

Bill Id Reciept No. Product Id Amount Volume Rate Date Payment Mode  

1 416835  PETROL  50  1  76 19/06/2017 00:05:35 cash 

2 416836 DIESEL 12000 203  59 19/06/2017 03:27:50  bank 

3 416837 PETROL 400  5 76 19/06/2017 05:25:09  credit 

模型:

function addNewsale($saleInfo, $cashInfo, $bankInfo, $creditInfo, $fleetInfo, $posInfo, $petrolInfo, $diselInfo) 
{ 
    $billId = $this->input->post('billId'); 
    $productId = $this->input->post('productId'); 
    $amount = $this->input->post('amount'); 
    $volume = $this->input->post('volume'); 
    $rate = $this->input->post('rate'); 
    $date = $this->input->post('date'); 
    $paymentmod = $this->input->post('paymentmod'); 
    $vehicalno = $this->input->post('vehicalno'); 
    $recieptno = $this->input->post('recieptno'); 
    $payId = $this->input->post('payId'); 

    //$remainCredit = $creditAmount-$debitAmount; 

    if($cashInfo){ 
     $this->db->insert('tbl_sale_detail',$saleInfo); 
     $billId=$this->db->insert_id(); 
     $cashInfo=array(
      'billId'=>$billId, 
      'amount'=>$amount, 
      'date'=>date('Y-m-d H:i:s'), 
      'createdBy'=>$this->vendorId, 
      'createdDtm'=>date('Y-m-d H:i:s')); 
     $this->db->insert('tbl_cash_detail',$cashInfo); 
    } 

    if($bankInfo){ 
     $this->db->insert('tbl_sale_detail',$saleInfo);  
     $billId = $this->db->insert_id(); 
     $bankInfo = array(
      'billId'=>$billId, 
      'amount'=>$amount, 
      'date'=>date('Y-m-d H:i:s'), 
      'createdBy'=>$this->vendorId, 
      'createdDtm'=>date('Y-m-d H:i:s')); 
     $this->db->insert('tbl_bank_detail',$bankInfo); 
    } 

    if($creditInfo){ 
     $this->db->insert('tbl_sale_detail',$saleInfo);  
     $billId=$this->db->insert_id(); 
     $creditInfo=array(
      'billId'=>$billId, 
      'customerId'=>$customerId, 
      'creditAmount'=>$amount, 
      'debitAmount'=>$debitAmount, 
      'remainCredit'=>$remainCredit, 
      'date'=>date('Y-m-d H:i:s'), 
      'createdBy'=>$this->vendorId, 
      'createdDtm'=>date('Y-m-d H:i:s')); 
     $this->db->insert('tbl_credit_detail',$creditInfo); 
    } 

    if($posInfo){ 
     $this->db->insert('tbl_sale_detail',$saleInfo);  
     $billId=$this->db->insert_id(); 
     $posInfo=array(
      'billId'=>$billId, 
      'amount'=>$amount, 
      'date'=>date('Y-m-d H:i:s'), 
      'createdBy'=>$this->vendorId, 
      'createdDtm'=>date('Y-m-d H:i:s')); 
     $this->db->insert('tbl_pos_detail',$posInfo); 
    } 

    if($fleetInfo){ 
     $this->db->insert('tbl_sale_detail',$saleInfo);  
     $billId=$this->db->insert_id(); 
     $fleetInfo=array(
      'billId'=>$billId, 
      'amount'=>$amount, 
      'date'=>date('Y-m-d H:i:s'), 
      'createdBy'=>$this->vendorId, 
      'createdDtm'=>date('Y-m-d H:i:s')); 
     $this->db->insert('tbl_fleet_detail',$fleetInfo); 
    } 

    if($diselInfo){ 
     $this->db->trans_start(); 
     $this->db->insert('tbl_disel_tank', $diselInfo); 
     $this->db->where('purchaseId', $purchaseId); 
     $insert_id = $this->db->insert_id(); 
     $this->db->trans_complete(); 
     return $insert_id; 
    } 
    if($petrolInfo){ 
     $this->db->trans_start(); 
     $this->db->insert('tbl_petrol_tank', $petrolInfo); 
     $this->db->where('purchaseId', $purchaseId); 
     $insert_id = $this->db->insert_id(); 
     $this->db->trans_complete(); 
     return $insert_id; 
    } 
} 

控制器:

function addNewSale() 
{ 
    if($this->isAdmin() == TRUE) 
    { 
     $this->loadThis(); 
    } 
    else 
    { 
     $this->load->library('form_validation'); 

     $this->form_validation->set_rules('productId','Product','trim'); 
     $this->form_validation->set_rules('amount','amount ','trim|required|numeric'); 
     $this->form_validation->set_rules('volume','volume','required'); 
     $this->form_validation->set_rules('rate','rate','trim|required'); 
     $this->form_validation->set_rules('date','date','required'); 
     $this->form_validation->set_rules('paymentmod','paymentmod ','trim|required'); 

     if($this->form_validation->run() == FALSE) 
     { 
      $this->addSale(); 
     } 
     else 
     { 
      $billId = $this->input->post('billId'); 
      $productId = $this->input->post('productId'); 
      $amount = $this->input->post('amount'); 
      $volume = $this->input->post('volume'); 
      $rate = $this->input->post('rate'); 
      $date = $this->input->post('date'); 
      $paymentmod = $this->input->post('paymentmod'); 
      $vehicalno = $this->input->post('vehicalno'); 
      $recieptno = $this->input->post('recieptno'); 
      $payId = $this->input->post('payId'); 
      $petrolInfo = ''; 
      $diselInfo = ''; 
      $saleInfo = array(
       'billId'=>$billId, 
       'recieptno'=>$recieptno, 
       'productId'=>$productId, 
       'amount'=>$amount, 
       'volume'=>$volume, 
       'rate'=>$rate, 
       'date'=>$date, 
       'paymentmod'=>$paymentmod, 
       'vehicalno'=>$vehicalno, 
       'createdBy'=>$this->vendorId, 
       'createdDtm'=>date('Y-m-d H:i:s')); 

      if($productId == 1){ 
       $last = $this->db->order_by('petrolId',"desc")->get('tbl_petrol_tank')->row('totalVolume'); 
       print_r($last); 

       $petrolInfo = ''; 
       $releaseVolume = $volume; 
       $totalVolume = $last-$releaseVolume; 
       $petrolInfo = array(
        'productId'=>'petrol', 
        'productVolume'=>$volume, 
        'totalVolume'=>$totalVolume, 
        'releaseVolume'=>$releaseVolume,); 
      } 

      if($productId == 2){ 
       $lastd = $this->db->order_by('diselId',"desc")->get('tbl_disel_tank')->row('totalVolume'); 
       print_r($lastd); 
       $diselInfo = ''; 
       $releaseVolume = $volume; 
       $totalVolume = $lastd-$releaseVolume; 
       $diselInfo = array(
        'productId'=>'disel', 
        'productVolume'=>$volume, 
        'totalVolume'=>$totalVolume, 
        'releaseVolume'=>$releaseVolume, 
        'date'=>$date, 
        'createdBy'=>$this->vendorId, 
        'createdDtm'=>date('Y-m-d H:i:s')); 
      } 
      $cashInfo = ''; 
      $bankInfo = ''; 
      $creditInfo = ''; 
      $posInfo = ''; 
      $fleetInfo = ''; 
      $creditAmount = $amount; 
      $debitAmount = $creditAmount-$amount; 
      $remainCredit = $creditAmount-$debitAmount; 

      if($payId == '1'){ 
       $cashInfo = array(
        'billId'=>$billId, 
        'amount'=>$amount, 
        'date'=>date('Y-m-d H:i:s'), 
        'createdBy'=>$this->vendorId, 
        'createdDtm'=>date('Y-m-d H:i:s')); 
      } 

      if($payId == '2'){ 
       $bankInfo = array(
        'billId'=>$billId, 
        'amount'=>$amount, 
        'date'=>date('Y-m-d H:i:s'), 
        'createdBy'=>$this->vendorId, 
        'createdDtm'=>date('Y-m-d H:i:s')); 
      } 
      if($payId == '3'){ 
       $creditInfo = array(
        'billId'=>$billId, 
        'customerId'=>$customerId, 
        'creditAmount'=>$amount, 
        'debitAmount'=>$debitAmount, 
        'remainCredit'=>$remainCredit, 
        'date'=>date('Y-m-d H:i:s'), 
        'createdBy'=>$this->vendorId, 
        'createdDtm'=>date('Y-m-d H:i:s')); 
      } 

      if($payId == '5'){ 
       $posInfo = array(
        'billId'=>$billId, 
        'amount'=>$amount, 
        'date'=>date('Y-m-d H:i:s'), 
        'createdBy'=>$this->vendorId, 
        'createdDtm'=>date('Y-m-d H:i:s')); 
      } 

      if($payId == '4'){ 
       $fleetInfo = array(
        'billId'=>$billId, 
        'amount'=>$amount, 
        'date'=>date('Y-m-d H:i:s'), 
        'createdBy'=>$this->vendorId, 
        'createdDtm'=>date('Y-m-d H:i:s')); 
      } 

      $this->load->model('sale_model'); 
      $result = $this->sale_model->addNewSale($saleInfo, $cashInfo, $bankInfo, $creditInfo, $posInfo, $fleetInfo, $petrolInfo, $diselInfo); 

      if($result > 0) 
      { 
       $this->session->set_flashdata('success', 'New Sale created successfully'); 
      } 
      else 
      { 
       $this->session->set_flashdata('error', 'Disel Tank Is Empty'); 
      } 

      redirect('index.php/addSale'); 
     } 
    } 
} 

視圖:

<?php 

$billId = ''; 
$recieptno = ''; 
$productId = ''; 
$amount = ''; 
$volume = ''; 
$rate = ''; 
$date = ''; 
$paymentmod = ''; 
$vehicalno = ''; 

if(!empty($saleInfo)) 
{ 
    foreach ($saleInfo as $uf) 
    { 
     $billId = $uf->billId; 
     $recieptno = $uf->recieptno; 
     $productId = $uf->productId; 
     $amount = $uf->amount; 
     $volume = $uf->volume; 
     $rate = $uf->rate; 
     $date = $uf->date; 
     $paymentmod = $uf->paymentmod; 
     $vehicalno = $uf->vehicalno; 
    } 
} 
?> 

<div class="content-wrapper"> 
    <!-- Content Header (Page header) --> 
    <section class="content-header"> 
    <h1> 
     <i class="fa fa-inr"></i> Sale Management<small>Add </small> 
    </h1> 
    </section> 
    <section class="content"> 
    <div class="row"> 
     <div class="col-xs-12 text-right"> 
     <div class="form-group"> 
      <!--<a class="btn btn-primary" href="<?php echo base_url();  ?>index.php/addSale"><i class="fa fa-plus"></i> Add Sale</a>--> 
     </div> 
     </div> 
    </div> 
    <div class="form-group"> 
    </div> 

    <div class="row"> 
     <div class="col-xs-12"> 
     <div class="box"> 
      <div class="box-header"> 
      <h3 class="box-title">Sale List</h3> 
      <div class="box-tools"> 
       <form role="form" action="<?php echo base_url() ?>index.php/addSale" method="post" id="addSale" role="form"> 
       <div class="input-group"> 
        <input type="text" name="searchText" value="<?php echo $searchText; ?>" class="form-control input-sm pull-right" style="width: 150px;" placeholder="Search"/> 
        <div class="input-group-btn"> 
        <button class="btn btn-sm btn-default searchList"><i class="fa fa-search"></i></button> 
        </div> 
       </div> 
       </div> 
      </div><!-- /.box-header --> 

      <div class="box-body table-responsive no-padding"> 
       <table class="table table-hover"> 
       <tr> 
        <th>Reciept No.</th> 
        <th>Product Id</th> 
        <th>Amount</th> 
        <th>Volume</th> 
        <th>Rate</th> 
        <th>Date</th> 
        <th>Payment Mode</th> 
        <th>Vehical No</th> 
       </tr> 
       <?php 
       if(!empty($saleRecords)) 
       { 
        foreach($saleRecords as $key=>$value) 
        { 
       ?> 
       <tr> 
        <td><?php echo $value->recieptno ?></td> 
        <td><?php echo $value->productId ?></td> 
        <td><?php echo $value->amount ?></td> 
        <td><?php echo $value->volume ?></td> 
        <td><?php echo $value->rate ?></td> 
        <td><?php echo $value->date ?></td> 
        <td> 
        <select class="form-control required" id="paymentmod" name="paymentmod[]"> 
         <option value="0">select</option> 
         <?php 
         if(!empty($paytypes)) 
         { 
          foreach ($paytypes as $pl) 
          { 
          ?> 
         <option value="<?php echo $pl->payId ?>"><?php echo $pl->payType ?></option> 
         <?php 
          } 
         } 
         ?> 
        </select> 
        </td> 
        <td><?php echo $value->vehicalno ?></td> 
       </tr> 
       <?php 
        } 
       } 
       ?> 
       </table> 

       <div class="box-footer"> 
       <input type="submit" class="btn btn-primary" value="Submit" /> 
       <input type="reset" class="btn btn-default" value="Reset" /> 
       </form> 
      </div><!-- /.box-body --> 
      <div class="box-footer clearfix"> 
       <?php echo $this->pagination->create_links(); ?> 
      </div> 
      </div><!-- /.box --> 
     </div> 
     </div> 
    </section> 
    </div> 
+1

使用批量插入... – Angel

+0

順便說一句,你的HTML是錯誤的,一些關閉標籤丟失,別人交換... –

回答

0

Query Builder具有如下功能:insert_batch

$data = array(
    array(
     'title' => 'My title' , 
     'name' => 'My Name' , 
     'date' => 'My date' 
    ), 
    array(
     'title' => 'Another title' , 
     'name' => 'Another Name' , 
     'date' => 'Another date' 
    ) 
); 

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

//產地:INSERT INTO MYTABLE(標題,姓名,日期)VALUES( '我 標題', '我的名字', '我的約會'),( '的另一個頭銜',「別稱」, ‘另一個日期’)笨的

+0

確定...但是這個代碼是在模型文件或控制器? –

+0

您可以在任何你想要的控制器或模型中製作它。我會建議你在模型中做。只需將數據從控制器傳遞給模型即可。遵循MVC結構。 –

+0

請不要將與數據庫相關的查詢添加到控制器。始終使用模型進行數據庫相關查詢。 @PoonamGaikwad –