i have form dinamic in like this
i am creating a dynamic form, i have an add button, when i click it adds one line to the form.
but when I echo , why is it like this ?
this my code
public function inputData(){
$provinsi = $this->input->post('provinsi');
$res = array();
for ($i=0; $i < count($provinsi); $i ) {
$res[] = array(
'id_finance' => '1',
'provinsi' => $provinsi[$i],
);
$this->db->insert_batch('lokasi', $res);
echo "<pre>", print_r($res);
}
}
this output
Array
(
[0] => Array
(
[id_finance] => 1
[provinsi] => TES 1
)
)
1
Array
(
[0] => Array
(
[id_finance] => 1
[provinsi] => TES 1
)
[1] => Array
(
[id_finance] => 1
[provinsi] => TES 2
)
)
1
Array
(
[0] => Array
(
[id_finance] => 1
[provinsi] => TES 1
)
[1] => Array
(
[id_finance] => 1
[provinsi] => TES 2
)
[2] => Array
(
[id_finance] => 1
[provinsi] => TES 3
)
)
1
i want it like this for output , how to ?
Array
(
[0] => Array
(
[id_finance] => 1
[provinsi] => TES 1
)
[1] => Array
(
[id_finance] => 1
[provinsi] => TES 2
)
[2] => Array
(
[id_finance] => 1
[provinsi] => TES 3
)
)
this my form
<form method="post" action="<?php echo base_url() . 'testing/inputData'; ?>">
<div>
<input type="text" name="provinsi[]"class="form-control name_list"/>
<button type="button" name="add">Add More</button>
</div>
<button type="submit" class="btn btn-info">submit</button>
</form>
to add a row I use jquery help.
how to make output like above? where is the error code
CodePudding user response:
You are printing the array for each loop, try this:
public function inputData(){
$provinsi = $this->input->post('provinsi');
$res = array();
for ($i=0; $i < count($provinsi); $i ) {
$res[] = array(
'id_finance' => '1',
'provinsi' => $provinsi[$i],
);
$this->db->insert_batch('lokasi', $res);
}
echo "<pre>", print_r($res);
}
CodePudding user response:
View:-
<form method="post" action="<?php echo base_url() . 'testing/inputData'; ?>">
<div>
<input type="text" name="provinsi[]" class="form-control name_list"/>
<button type="button" name="add">Add More</button>
</div>
<button type="submit" class="btn btn-info">submit</button>
</form>
Controller Code:-
public function inputData(){
$mainarray = array();
$data= $this->input->post();
for ($i = 0; $i < sizeof($data['provinsi']); $i ) {
$arr = array(
'id_finance'=>'1',
'provinsi'=>$data['provinsi'][$i],
$mainarray[] = $arr;
}
$table="table_name";
$result=$this->db->insert_batch($table,$mainarray);
}