0

i have search alot and i have post somany times but i didnt get any proper answer.

This is my view page - View

here i have write a query for add fields dynamically for particular ref_no so help me for model and controller

<div id="login_form">
        <?php echo form_open(base_url().'sample/invoice'); ?>
        <label for="type" class="control-label">Type</label>
        <div><?php echo form_input(array('id'=>'type','name'=>'type'));?></div>

        <label for="ref" class="control-label">REF</label>
        <div><?php echo form_input(array('id'=>'ref','name'=>'ref'));?></div>

        <label for="title" class="control-label">TITLE</label>
        <div><?php echo form_input(array('id'=>'title','name'=>'title'));?></div>

        <div  id="description"><p id="add_field">ADD DESCRIPTION</p></div>

        <label for="doc" class="control-label">Support Doc</label>
        <div><?php echo form_input(array('id'=>'doc','name'=>'attach','type'=>"file"));?></div>

        <input id="btn_add" name="btn_add" type="submit" class="btn btn-primary" value="Save" />
    </div>
<script>
            var count = 0;
    $(document).ready(function() {
        $('p#add_field').click(function(){
           count += 1;
            var html='<strong>Description  '+ count +'</strong>'+'<input id="description'+ count +'"name="description[]'+'" type="text" />'+'<input id="description'+ count +'"name="voucher_no[]'+'" type="text" />'+'<input id="description'+ count +'"name="price[]'+'" type="text" /><br />';
            $('#description').append(html);

    });
    });

        </script>

This is My Controller :

$data1 = array(
    'invoice_type' => $this->input->post('type'),
    'reference_no' => $this->input->post('ref'),
    'des_title' => $this->input->post('title'),


     );

    $data2 = array(
        'reference_no' => $this->input->post('ref'),
        'description' => $this->input->post('des'),
        ); 
 $this->sample_model->insert_entry($data1, $data2);

This My MOdel :

function insert_entry($data1, $data2) {

    $this->db->insert('myinvoice', $data1);
    $this->db->insert('invoice_description', $data2);
}

What i want is insert mutiple descriptions for one reference number. myinvoice is parent table and invoice_description is child table when i insert single data its works perfectly but i want insert multiple descriptions

8
  • you want model and controller code ? Commented Aug 4, 2015 at 10:46
  • i want insert data into mysql database Commented Aug 4, 2015 at 10:50
  • post some model and controller code also which you have tried. Commented Aug 4, 2015 at 10:51
  • @user1048123 please i have edit my question please help me Commented Aug 4, 2015 at 11:00
  • from where you get $data1 and $data2 value. Its not in your form Commented Aug 4, 2015 at 11:12

4 Answers 4

1

first of all we cannot use same id more than once in the same page. And in controller

function abc(){
    $description = $this->input->post("description");
    $voucher_no = $this->input->post("voucher_no");
    $price = $this->input->post("price");
    $i = 0;
    foreach($description as $row){
        $data['description'] = $description[$i];
        $data['voucher_no'] = $voucher_no[$i];
        $data['price'] = $price[$i];
        $this->db->insert("your_table",$data);
        $i++;
    }
}

I hope the above code helps you. You can directly insert in controller or you can call a model function to do that insertion.

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

2 Comments

it wont work :( no errors but when i submit i coudnt get any data in my database
bro for this i mixed 3 of your codes i will update my answer ok va :)
0

try this Your controller will be

$data1 = array(
                  'invoice_type' => $this->input->post('type'),
                  'reference_no' => $this->input->post('ref'),
                  'des_title' => $this->input->post('title'),
                  );
    /*
        Considering description ,voucher_no,price compulsary if user add discription
    */  
    $description = $this->input->post("description");
    $voucher_no = $this->input->post("voucher_no");
    $price = $this->input->post("price");
    $data2 ='';
    if(isset($description) && !empty($description)){
        $i = 0;
        foreach($description as $row){
            $data2[] =array(
                'description'=>$description[$i],
                'voucher_no' => $voucher_no[$i],
                'price'=>$price[$i],        
            );
            $i++;
        }
    }

    $this->sample_model->insert_entry($data1, $data2);

Your model will be like this

function insert_entry($data1, $data2) {
        $this->db->insert('myinvoice',$data1);
        if($data2){
            $this->db->insert_batch('invoice_description',$data2);
        }
    }

5 Comments

there is no error but data wont insert my database just cleaning fileds when i submit
can you print $data2 and $data1 value in controller before insert_entry and check whats value you are getting
update your question accordingly .so that we can help
0

Hope my question and answer will help to others :) My View Page :

<div id="login_form">
            <?php echo form_open(base_url().'sample/invoice'); ?>
            <label for="type" class="control-label">Type</label>
            <div><?php echo form_input(array('id'=>'type','name'=>'type'));?></div>

            <label for="ref" class="control-label">REF</label>
            <div><?php echo form_input(array('id'=>'ref','name'=>'ref'));?></div>

            <label for="title" class="control-label">TITLE</label>
            <div><?php echo form_input(array('id'=>'title','name'=>'title'));?></div>

            <div  id="description"><p id="add_field">ADD DESCRIPTION</p></div>

            <div  id="doc"><p id="add">ADD Support Doc</p></div>

            <input id="btn_add" name="btn_add" type="submit" class="btn btn-primary" value="Save" />
        </div>

        <script>
         var count = 0;
        $(document).ready(function() {
        $('p#add_field').click(function(){
           count += 1;
           var html='<strong>Description  '+ count +'</strong>'+'<input id="description'+ count +'"name="description[]'+'" type="text" />'+'<input id="description'+ count +'"name="voucher_no[]'+'" type="text" />'+'<input id="description'+ count +'"name="price[]'+'" type="text" /><br />';
           $('#description').append(html); });
           });
        </script>
        <script>
            var coun = 0;
    $(document).ready(function() {
        $('p#add').click(function(){
           coun += 1;
            var div='<strong>Document  '+ coun +'</strong>'+'<input id="doc'+ coun +'"name="doc[]'+'" type="file" /><br />';
            $('#doc').append(div);

    });
    });

        </script>

This is My Controller :

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Sample extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
        $this->load->model('sample_model');
    }

    public function index()
    {
        $this->load->view('sample_view');
    }

    function invoice()
    {
        $this->load->library('form_validation');

        $this->form_validation->set_rules('type', 'INVOICE TYPE', 'trim');
        $this->form_validation->set_rules('ref', 'REFERENCE NO', 'trim|required|is_unique[myinvoice.reference_no]');
        $this->form_validation->set_rules('title', 'DESCRIPTION TITLE', 'trim|required'); 
        $this->form_validation->set_rules('description[]', 'DESCRIPTIONS', 'trim'); 
        $this->form_validation->set_rules('voucher_no[]', 'VOUCHER', 'trim'); 
        $this->form_validation->set_rules('price[]', 'PRICE', 'trim'); 
        $this->form_validation->set_rules('doc[]', 'DOCUMENT', 'trim');


        if($this->form_validation->run())
        {
            $this->load->database();
            $this->load->model('sample_model');


            $data1 = array(
                  'invoice_type' => $this->input->post('type'),
                  'reference_no' => $this->input->post('ref'),
                  'des_title' => $this->input->post('title'),
                  );
                  $this->db->insert("myinvoice",$data1);

            $ref =  $this->input->post('ref');
            $description = $this->input->post('description');
            $voucher_no = $this->input->post('voucher_no');
            $price = $this->input->post('price');
             $i = 0;
             if($description){
            foreach($description as $row)
            {
            $data2['ref_no'] = $ref;
            $data2['descriptions'] = $description[$i];
            $data2['voucher'] = $voucher_no[$i];
            $data2['value'] = $price[$i];
              $this->db->insert("invoice_description",$data2);
            $i++;

             }}
                $doc = $this->input->post('doc');
                $ref =  $this->input->post('ref');
            $i = 0;
             if($description){
            foreach($doc as $row)
            {
                $data3['ref_no'] = $ref;
                $data3['attached'] = $doc[$i];
                 $this->db->insert("invoice_attached",$data3);
                $i++;
             }}             
            }else{

            $this->load->view('sample_view');
        }
    }

Comments

0

You can try this

$data = array(
      array( 
          'title'       => $this->input->post('title1'), 
          'category'    => $this->input->post('details1'), 
          'description' => $this->input->post('address1')
      ),
      array( 
          'title'       => $this->input->post('title2'), 
          'category'    => $this->input->post('details12'), 
          'description' => $this->input->post('address2')
      )
);
$this->db->insert('tbl_name', $data);

Comments

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.