php - Inserting multiple rows mysql in codeigniter -
i trying insert multiple rows @ once using codeigniter
my html code codeigniter view
<?= form_open(base_url() . 'home/create') ?> <tbody> <tr> <td class="col-md-1 center-block"> <input type="text" name="id[]" value="1" class="form-control" /> </td> <td> <input type="text" placeholder="enter descreption" class="form-control" name="desc[]" /> </td> <td class="col-md-2 center-block"> <input id="space" onkeyup="sumprice()" type="number" class="form-control" name="space[]" /> </td> <td> <select onchange="sumprice()" class="selectpicker price" data-live-search="true" data-width="100%" name="price[]" data-header="select cost"> <option value="5">5</option> <option value="10">10</option> <option value="20">20</option> </select> </td> <td class="total_price col-md-1"> <input type="number" class="form-control" name="total[]" value="1000" /> </td> <td> <button class="btn btn-danger center-block remove-row"><span class="glyphicon glyphicon-remove"></span> remove</button> </td> </tr> </tbody> </table> <input type="submit" value="insert" class="btn btn-success" /> </form> <button class="btn btn-success append-row"><span class="glyphicon glyphicon-plus-sign"></span> add new row</button>
and using jquery insert new row in table here js
code
$('.append-row').click(function() { $("table tbody").append ('<tr><td class="col-md-1 center-block"><input type="text" name="id[]" value="' + row_id_val++ + '" class="form-control" /></td> <td><input type="text" placeholder="enter descreption" class="form-control" name="desc[]" /></td> <td class="col-md-2 center-block"><input id="space" onkeyup="sumprice()" type="number" class="form-control" name="space[]" /></td> <td> <select onchange="sumprice()" class="selectpicker price" data-live-search="true" data-width="100%" name="price[]" data-header="select sub account" onselect="sumprice()" > <option onselect="sumprice()" value="5">5</option> <option onselect="sumprice()" value="10">10</option> <option onselect="sumprice()" value="20">20</option> </select> </td> <td class="total_price col-md-1"><input type="number" class="form-control" value="" name="total[]" /></td> <td><button class="btn btn-danger center-block remove-row"><span class="glyphicon glyphicon-remove"></span> remove</button></td></tr>'); $('.selectpicker').selectpicker('refresh'); });
my controller
if ($_post) { $data = array(); ($i = 0; $i < count($this->input->post('id')); $i++) { $data[$i] = array( 'row_id' => $this->input->post('id')[$i], 'desc' => $this->input->post('desc')[$i], 'space' => $this->input->post('space')[$i], 'price' => $this->input->post('price')[$i], 'total' => $this->input->post('total')[$i], 'code' => time() ); } $this->home_model->create($data); }
model
function create($data) { $this->db->insert_batch('quotation', $data); }
i tried many solutions stackoverflow none of them worked
if php version>=5.4 should work.
may try this
if ($_post) { $row_ids=$this->input->post('id'); $desc=$this->input->post('desc'); $spaces=$this->input->post('space'); $prices=$this->input->post('price'); $totals=$this->input->post('total'); $data = array(); ($i = 0; $i < count($this->input->post('id')); $i++) { $data[$i] = array( 'row_id' => $row_ids[$i], 'desc' => $desc[$i], 'space' => $spaces[$i], 'price' => $prices[$i], 'total' => $totals[$i], 'code' => time() ); } $this->home_model->create($data); }
Comments
Post a Comment