0

Following code is develop to save 4 dynamic input data in database with codeigniter framework.But due to a bug only value of the first input box is saved 4 times.I have included the code and screen shots. Can someone help me to correct this issue? This is the way how I entered values to the database enter image description here

enter image description here But this is how it saves in the database

This is my controller

function error(){
    //if ($this->input->post('mytext')) {

       $attain = $this->input->post('mytext', true);
        $data2=array(); //<-initialize
            foreach ($attain as $i => $a) { // need index to match other properties
                //append array
                $data2[] = array(
                'mytext' => $a,
                'mytext1' => $a,
                'mytext2' => $a,
                'mytext3' => $a,
                'projectname'=> $this->input->post('projectname'),
            );
            //for multiple entry in same table


        //}
    }
    $this->db->insert_batch('projectem', $data2); 
    redirect('Select_ctrl2/ModalAddEmployeesProject');
  } 

This is my view

 <link rel = "stylesheet" type = "text/css" 
   href = "<?php echo base_url(); ?>jquery.multiselect.js-master/css/jquery.multiselect.css">

<link rel = "stylesheet" type = "text/javascript" 
   href = "<?php echo base_url(); ?>jquery.multiselect.js-master/js/jquery-1.5.min.js">

 <link rel = "stylesheet" type = "text/javascript" 
   href = "<?php echo base_url(); ?>jquery.multiselect.js-master/js/jquery.multiselect.js">



<head>
<script>
</script>
<script type="text/javascript">
$(document).ready(function() {
    var max_fields      = 10; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID

    var x = 1; //initlal text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            $(wrapper).append('</br><div class="col-lg-12"><div class="col-lg-3"><input class="input form-control" placeholder="Task Name" name="mytext[]"/></div><div class="col-lg-3"><input class="input form-control" placeholder="Description" name="mytext1[]"/></div><div class="col-lg-3"><input class="input form-control" placeholder="Task Cost" name="mytext2[]"/></div><div class="col-lg-3"><input class="input form-control" placeholder="Employee" name="mytext3[]"/></div><a href="#" class="remove_field">Remove</a></div>'); //add input box
        }
    });

    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })
});
</script> 
</head>


    <body>

<div class="col-lg-10">
    <div class="panel panel-danger">
        <div id="page-wrapper " style="background-color: #f8f8f8">

            <div class="container-fluid shadow " >


                <div class="row" style="background-color:#46bc99;">
                    <div class="col-lg-12" >
                        <h3 class="text-center formheading">
                           <li class="glyphicon glyphicon-user" style="font-size: 40px; padding-top: 2px;"></li>
                           ADD EMPLOYEES TO PROJECT
                        </h3>

                    </div>

                </div>

                <br>
                <div class="panel-body" align="center" style="width: 960px; color: navy;  border: 2px black; padding: 5px;">

            <div class="col-lg-12" >

                <?php echo form_open('Select_ctrl2/error'); ?>



                <div class="form-group">
                <select name="projectname" class="input form-control">
                    <option value="none" selected="selected">Select Project</option>


                    <?php foreach($projects as $s):?> 
                    <option value="<?php echo $s->projectname?>"><?php echo $s->projectname?></option>
                    <?php endforeach;?>  
                </select>
                </div>

                <div class="input_fields_wrap">
                    <div class="form-group">
                    <button type="button" class="btn btn-success add_field_button">Add More Fields</button> 
                    </div>

                </div>
                <div>

                 <button name="submit" type="submit" class="btn btn-info">Submit</button>
                </div>

                <?php echo form_close(); ?>


            </div>





            </div>             

</div>


    </body>
</html>

1 Answer 1

1

You are overwriting values in every iteration on that foreach, remember that you receive mytext, mytext1, mytext2 and mytext3 as arrays, so you need to access every value by its index:

function error(){
       $attain = $this->input->post();
       $data2=array(); //<-initialize
       for ($i = 0; $i < count($attain['mytext']); $i++) {
                //append array
                $data2[] = array(
                    'mytext' => $attain['mytext'][$i],
                    'mytext1' => $attain['mytext1'][$i],
                    'mytext2' => $attain['mytext2'][$i],
                    'mytext3' => $attain['mytext3'][$i],
                    'projectname'=> $attain['projectname'],
                );
                //for multiple entry in same table
       }
    $this->db->insert_batch('projectem', $data2); 
    redirect('Select_ctrl2/ModalAddEmployeesProject');
  }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Mr.Triby It work so nice and perfect.Thank you very much

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.