Search code examples
codeigniter-3

Codeigniter Insert Dynamic multiple input


can someone help me please. im stock. Can you help me how to save multiple value from multiple dynamic textbox?

My View:

 <?php
  echo form_open(admin/add_transaction); 
  for ($i=0; $i < $qty ; $i++) { 
  $count = $i+1; 
  $newinput=array(
     'class'=>"form-control",
     'name'=>"$property_no-$count",
     'placeholder'=>"Serial No. - $property_no-$count",
                 ); ?>
                               <div class="input-group">
                               <span class="input-group-addon">
                               <i class="glyphicon glyphicon-barcode"></i></span>
                              <?php echo form_input($newinput); ?></div>
                               <br>
                               <?php }
                               echo form_close();
                                ?>

My Controller:

        function add_transaction(){
        $this->masterrecords->save_serial();
        redirect('admin/transaction');
    }

My Model: How to insert multiple value of input text?

        function save_serial(){

        $s=$this->input->post('new');

        $data = array();
    for ($i = 0; $i < count($this->input->post('new')); $i++)
    {
        $data = array(

            'serial_no' => $s[$i],


        );
    }
        $this->db->insert('tblserial_no' ,$data);
        return true;



    }

Solution

  • You have $qty and $property_no variable with you. What you can do is create two hidden fields for both the variables:

     <input type='hidden' name='qty' value='<?= $qty ?>'/>
     <input type='hidden' name='property_no' value='<?= $property_no ?>'/>
      
     //then in your model:
     $data = array();
     for ($i=0; $i < $this->input->post('qty') ; $i++) { 
    
           $count = $i+1;
    
           //check fields are set and not empty...
           if($this->input->post($this->input->post('property_no').'-'.$count) && 
              $this->input->post($this->input->post('property_no').'-'.$count) != ""){
    
              //prepare your $data array.. 
              $data[$count] = $this->input->post($this->input->post('property_no').'-'.$count);
    
           }
      }