Insert batch dataset?

I want to insert the following insert_batch data, as in the following example in a database table (mysql):

HTML:

 <input name="u_id[0][0]" value="76"> <input name="un[0][0]" value="1"> <input type="text" name="ue[0][0]" value="11"> <input type="text" name="up[0][0]" value="111"> <input name="u_id[1][0]" value="77"> <input name="un[1][1]" value="2"> <input type="text" name="ue[1][1]" value="22"> <input type="text" name="up[1][1]" value="222"> <input name="un[1][2]" value="3"> <input type="text" name="ue[1][2]" value="33"> <input type="text" name="up[1][2]" value="333"> 

PHP:

 $u_id = $this->input->post('u_id'); $un = $this->input->post('un'); $up = $this->input->post('up'); $ue = $this->input->post('ue'); $data = array(); foreach ($un as $idx => $name) { $data[] = array( 'u_id' => $u_id[$idx], 'un' => $un[$idx], 'up' => $up[$idx], 'ue' => $ue[$idx], ); }; $this -> db -> insert_batch('units', $data); 

I want to insert them like this:

enter image description here

How to change php code and html code? What am I doing?

+4
source share
2 answers

I assume that you are using CodeIgniter and that the name of the database table you want to insert is called "ones" and that its column "id" is auto-incrementing.

I proceed from my solution from CodeIgniter User Guide Version 2.0.3 using a call to the auxiliary ($this->db->insert_string()) database class.

 foreach ($data as $row) { $error_code = $this->db->insert_string('units', $row); } 

See http://codeigniter.com/user_guide/database/helpers.html

The insert_string function that the Database class provides accepts an associative array, builds an INSERT from internal elements, executes it, and then returns a numerical error code.

+1
source

LOL, this is not very, but sometimes it may work:

 <input name="u_id[]" value="76"> <input name="un[]" value="1"> <input type="text" name="ue[]" value="11"> <input type="text" name="up[]" value="111"> <input name="u_id[]" value="77"> <input name="un[]" value="2"> <input type="text" name="ue[]" value="22"> <input type="text" name="up[]" value="222"> <input name="un[]" value="3"> <input type="text" name="ue[]" value="33"> <input type="text" name="up[]" value="333"> 

 $u_id=$this->input->post('u_id'); $un=$this->input->post('un'); $up=$this->input->post('up'); $ue=$this->input->post('ue'); for($i=0;$i<count($u_id);$i++){ for($ii=0;$ii<count($un[$i]);$ii++){ (count($un[$i])>1)?$unn=$un[$i][$ii+1]:$unn=$un[$i][$ii]; (count($ue[$i])>1)?$uen=$ue[$i][$ii+1]:$uen=$ue[$i][$ii]; (count($up[$i])>1)?$upn=$up[$i][$ii+1]:$upn=$up[$i][$ii]; $this->db->insert('units', array(//use db insert here 'u_id'=>$u_id[$i][0], 'un'=>$unn, 'ue'=>$uen, 'up'=>$upn, )); } } 

I would go so far as to suggest you not use it. But perhaps this may inspire someone to suggest a better solution.

Greetings.

0
source

All Articles