1

I have two arrays of values:

First array contain user id's:

Array ([0]=>1 [1]=>2 [2]=>3 [3]=>4 [4]=>5 [5]=>6) 

Second array contain attendance status:

Array([0]=>Present [1]=>Absent [2]=>Absent [3]=>Present [4]=>Absent [5]=>Present) 

I want to insert these values in the database in separate rows like this:

U_id Status 1 Present 2 Absent 3 Absent 4 Present 5 Absent 6 Present 

Currently, I am using this code to insert values in database.

My Controller Code:

public function usr_att(){ if(isset($_POST['submit'])){ $uid = $_POST['uid']; $status= $_POST['stat']; $id = implode("," , $uid ); $status = implode("," , $status ); $data = array ( 'u_id' => $id, 'status' => $status ); $this->db->insert('attendence' , $data); redirect("usr/usr_list", "refresh"); } } 

But this code inserts data like this:

U_id Status 1 Present,Absent,Absent,Present,Absent,Present 

How can I insert these values in separate rows using CodeIgniter?

1
  • What version of PHP are you using? Commented Oct 15, 2017 at 16:53

2 Answers 2

1

Simply you can do like this

public function usr_att() { if(isset($_POST['submit'])) { $uid = $_POST['uid']; $status = $_POST['stat']; foreach ($uid as $key => $item) { $data = array ( 'u_id' => $item, 'status' => $status[$key] ); $this->db->insert('attendence' , $data); } redirect("usr/usr_list", "refresh"); } } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you sir for your help its working according to my requirement.
0

For your intended purpose, once you have the values for $uid and $stat in an array, then you could do this:

<?php // You have your u_id values in an array $uid = array('1','2','3','4','5','6'); // You have your status values in an array $stat = array('Present', 'Absent', 'Absent', 'Present', 'Absent', 'Present'); // As long as the number of u_id values and status values are the same if( count($uid) == count($stat) ) { // Use the count of uid values and loop through for( $x = 0; $x <= count($uid) -1; $x++ ) { // Entering each on in the database $this->db->insert('attendence', array( 'u_id' => $uid[$x], 'status' => $stat[$x] )); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.