0

i have two arrays $getusersdetails

Array
(
    [0] => Array
        (
            [users_id] => 2
        )

    [1] => Array
        (
            [users_id] => 3
        )

    [2] => Array
        (
            [users_id] => 4
        )

)

and $data['checkbox']

Array
(
    [1] => 1
    [2] => 3
)

I want to insert this values to mysql database. so itried

 $j=1;
        foreach ($getusersdetails as $userstatus):

            $val1['fk_c_id'] = $data['checkbox'][$j];
            $val1['fk_user_id'] = $userstatus['users_id'];
            $val1['status'] = 1;
            $result1 = $this->db->insert('admin_course_details', $val1);
              $j++;
        endforeach;

here i din't get expected output. Expected output

status | fk_c_id | fk_user_id
1             1     2
1             1     3
1             1     4
1             3      2
1             3      3
1             3      4

my output

status | fk_c_id | fk_user_id
    1             1     2
    1             1     3
    1             1     4

I want 1 and 3 (checkbox values) for each users,so here i have 3 users.but here only 1 check box value is inserting.Any help is appreciated.thanks

0

1 Answer 1

4

You need to apply two foreach()

foreach ($data['checkbox'] as $data):
  foreach ($getusersdetails as $userstatus):
      $val1['fk_c_id'] = $data;
      $val1['fk_user_id'] = $userstatus['users_id'];
      $val1['status'] = 1;
      $result1 = $this->db->insert('admin_course_details', $val1);
  endforeach;
endforeach;
Sign up to request clarification or add additional context in comments.

1 Comment

@Ezra glad to help you :):)

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.