I have a problem with posting dynamic text box data with code-igniter. I am going to do this using insert_batch() method.
view
<input name="qty[]" type="text" id="qty[]" size="5" maxlength="5" />
<input type="hidden" name="id[]" id="id[]" value="<?php echo $v_items->product_id ; ?>" />
In controller I have get array result like this;
public function submit_in()
{
$data = array(
'id' => $this->input->post('qty'),
'qty' => $this->input->post('id')
);
$data['title'] = 'Damage Product';
$data['subview'] = $this->load->view('admin/cwh/stock_in', $data, true);
$this->load->view('admin/_layout_main', $data);
}
array (size=3)
'id' =>
array (size=13)
0 => string 'qw' (length=2)
1 => string 'qw' (length=2)
2 => string '' (length=0)
3 => string 'qwqw' (length=4)
4 => string '' (length=0)
5 => string '' (length=0)
6 => string 'q' (length=1)
7 => string '' (length=0)
8 => string 'wwq' (length=3)
9 => string '' (length=0)
10 => string '' (length=0)
11 => string '' (length=0)
12 => string 'qw' (length=2)
'qty' =>
array (size=13)
0 => string '74' (length=2)
1 => string '75' (length=2)
2 => string '76' (length=2)
3 => string '77' (length=2)
4 => string '78' (length=2)
5 => string '79' (length=2)
6 => string '80' (length=2)
7 => string '81' (length=2)
8 => string '82' (length=2)
9 => string '83' (length=2)
10 => string '84' (length=2)
11 => string '85' (length=2)
12 => string '86' (length=2)
'title' => string 'Damage Product' (length=14)
So i cant perform batch_insert() method using this array result. (I think my array result format is wrong)
My table structure is
product_data(**pid**,id,qty);
asked Oct 22, 2015 at 4:17
Tharindu ucsc
6411 gold badge8 silver badges19 bronze badges
3 Answers 3
Manipulate your data before use
$this->db->insert_batch('my_table',$data);
This is my suggestion:
$arr_qty = $this->input->post('qty');
$arr_id = $this->input->post('id');
$i=-1;
foreach($arr_qty as $qty):
$i++;
$data[] = array(
'qty' => $qty,
'id' => $arr_id[$i]
);
endforeach;
// Then
$this->db->insert_batch('my_table',$data);
Tell me if it works.
answered Oct 22, 2015 at 4:28
Nere
4,0956 gold badges37 silver badges75 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Nere
you may use
if(!empty($arr_qty))bulk insert you must loop in order to insert the bulk data
for($i = 0; $i < sizeof($_POST['name']);$i++)
{
$name = $_POST['name'];
$data = array(
$name[$i];
);
// insert the query here for insert
}
Comments
Actually you assign wrong data
'id' => $this->input->post('qty'),//Wrong 'qty'
'qty' => $this->input->post('id') //wrong 'id'
ID ==QTY and QTY == ID
That is wrong in your code, It should be
'id' => $this->input->post('id'), //correct
'qty' => $this->input->post('qty') //correct
answered Oct 22, 2015 at 4:36
Abdulla Nilam
39.9k18 gold badges85 silver badges115 bronze badges
Comments
default