0

I want to insert multiple rows into the db using submit button, This is my view code

    <?php   
        foreach($role_array as $k =>$v)
        {
            //var_dump($k);
            if($k != "bookin_operator" && $k!= "production_manager" && $k != "client" && $k != "admin" )
            {

        ?>
            <div class="row-fluid"> 
                <div class="span6">      
                    <div class='control-group'>


                        <label for='<?php echo $k;  ?>' class='control-label'><?php  echo $v ;?></label>
                        <div class='controls'>
                            <select class='form-control' name='<?php echo $k;  ?>[]' id='<?php echo $k;  ?>' multiple='multiple' size='5'>
                                <?php

                                   foreach($all_datas[$k] as $datas ) 
                                   {

                                        $username = "{$datas->nickname}";

                                        echo "<option value='{$datas->id}'>{$username}</option>";
                                   }
                                ?>
                            </select>
                            <label for="<?php echo $k;  ?>" class="error" style="display:none"></label>
                        </div>
                    </div>                         
                </div>    

            </div>  
            <?php }} ?> 

This is my controller code

            $roles = $this->utility->roles;
            foreach($roles as $k => $v)
            {
                if($k != "bookin_operator" && $k!= "production_manager" && $k != "client" && $k != "admin" && isset($post_data[$k]))
                {
                    $userrole = $k;
                    $post_user=isset($post_data[$k])?$post_data[$k]:'';

                    if (count($post_user) > 0)
                    {
                        for ($i = 0; $i < count($post_user); $i++)
                        {
                            $data = array("journal_id" => $new_journal_id,"user_role" => $userrole,"user_id" =>$post_user[$i]);
                           // var_dump($data);
                            $flag = true;



                        }
                    }
                }
            }

            if(isset($flag))
            {

                $v=$this->JournalUsers->addJournalUser($data);
            }

$data output is

    array(3) {
    ["journal_id"]=>int(83)
    ["user_role"]=>
    string(15) "quality_checker"
    ["user_id"]=>
    string(3) "136"
    }
    array(3) {
    ["journal_id"]=>
    int(83)
    ["user_role"]=>
    string(5) "texer"
    ["user_id"]=>
    string(3) "103"
    }
    array(3) {
    ["journal_id"]=>
    int(83)
    ["user_role"]=>
    string(5) "texer"
    ["user_id"]=>
    string(3) "133"
    }
     array(3) {
    ["journal_id"]=>
    int(83)
    ["user_role"]=>
    string(5) "texer"
    ["user_id"]=>
    string(3) "132"
    }
    array(3) {
   ["journal_id"]=>
   int(83)
   ["user_role"]=>
   string(10) "typesetter"
   ["user_id"]=>
   string(3) "109"
   }

   array(3) {
   ["journal_id"]=>
   int(83)
   ["user_role"]=>
   string(10) "typesetter"
   ["user_id"]=>
   string(3) "153"
   }

But,when insert the data only one row will be inserted on the table.

   array(3) {
   ["journal_id"]=>
   int(83)
   ["user_role"]=>`enter code here`
    string(10) "typesetter"
    ["user_id"]=>
    string(3) "153"
    }  

I want to insert all datas in db

1
  • use print_r and post the array Commented Nov 16, 2015 at 6:29

2 Answers 2

4

Here the $data is reinitialized everytime. Update the code like this:

$data = array();
    $roles = $this->utility->roles;
        foreach($roles as $k => $v)
        {
            if($k != "bookin_operator" && $k!= "production_manager" && $k != "client" && $k != "admin" && isset($post_data[$k]))
            {
                $userrole = $k;
                $post_user=isset($post_data[$k])?$post_data[$k]:'';

                if (count($post_user) > 0)
                {
                    for ($i = 0; $i < count($post_user); $i++)
                    {
                        $data[] = array("journal_id" => $new_journal_id,"user_role" => $userrole,"user_id" =>$post_user[$i]);
                       // var_dump($data);
                        $flag = true;



                    }
                }
            }
        }

        if(isset($flag) && !empty($data))
        {

            $v=$this->JournalUsers->addJournalUser($data);
        }

And in the model loop through like this:

public function addJournalUser($data) {
    foreach ($data AS $row) {
      // execute your insertion query with variable row   
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Correct only one thing missing just define $data above your code!!
@SanjayKumarNS I have a doubt public function addJournalUser( $data ) { $this->db->insert( "rsgp_journal_users", $data ); } this is my insert function. without change this ,is it possible??,becoz this function is used for different locations
0

You can also use insert_batch in the model,no need of foreach

$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); 

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.