1

I'm new with codeigniter. I have one form in table format. Having records in table format I want to change only one record which is in select box after selecting it, I want to click on multiple checkboxes for multiple records & when click on submit button, records should be need to add in database table.

table : there is payment mod column in which I am changing there value.before billId I have one column for checkboxes. By clicking on it i want to submit the data.

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

model:

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

controller :

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

view:

<?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>
2
  • 1
    use batch insert of codeigniter... Commented Sep 22, 2017 at 8:04
  • btw, your HTML is wrong, some closing tag is missing, others are exchanged... Commented Sep 22, 2017 at 8:47

1 Answer 1

0

Codeigniter Query Builder has a function 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); 

// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date')

Sign up to request clarification or add additional context in comments.

2 Comments

You can make it in your controller or Model where ever you want. I will suggest you do it in a model. Just pass the data to model from your Controller. Follow MVC structure.
Please don't add database related queries to the controller. Always use model for database related query. @PoonamGaikwad

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.