Welcome Guest, Not a member yet? Register   Sign In
insert loop
#1

[eluser]snowstar[/eluser]
Hi All,

I have a form which has multiple post data for each input field (I.E iput with the name of price may appear many times). I want to input each row with a new id for each record


Snippet from controller
Code:
$data =array(
            'orderid' => $id,
            'price' => $this->input->post('price'),
            'qty' => $this->input->post('qty'),
            'name' => $this->input->post('name'),
            'des' => $this->input->post('des'),
        );
        
$this->smy_model->insert_data($data);

snippet from view

Code:
<tr>
<td>&lt;input type="text" name="price"&gt;&lt;/td>
<td>&lt;input type="text" name="qty"&gt; </td>
<td>&lt;input type="text" name="name"&gt;&lt;/td>
<td> &lt;input type="text" name="des"&gt;&lt;/td>
</tr>
<tr>
<td>&lt;input type="text" name="price"&gt;&lt;/td>
<td>&lt;input type="text" name="qty"&gt; </td>
<td>&lt;input type="text" name="name"&gt;&lt;/td>
<td> &lt;input type="text" name="des"&gt;&lt;/td>
</tr>
<tr>
<td>&lt;input type="text" name="price"&gt;&lt;/td>
<td>&lt;input type="text" name="qty"&gt; </td>
<td>&lt;input type="text" name="name"&gt;&lt;/td>
<td> &lt;input type="text" name="des"&gt;&lt;/td>
</tr>

essentially i need to insert a new row of data for each row of inputs. how ever i am not sure how to loop the inserts for each input.

can some one point me in the right direction?
#2

[eluser]pickupman[/eluser]
If you have inputs in the same form with the same name the need to have brackets [].
Code:
&lt;input type="text" name="price[]" /&gt;
&lt;input type="text" name="qty[]" /&gt;
&lt;input type="text" name="name[]" /&gt;
&lt;input type="text" name="des[]" /&gt;

You can loop through the POST data inserting each row.
Code:
$price = $this->input->post('price');
$qty   = $this->input->post('qty');
for($i = 0; $i < count($price); $i++){
  $data = array('price' => $price[$i],
                'qty'   => $qty[$i]);
  $this->smy_model->insert($data);
}
#3

[eluser]snowstar[/eluser]
thanks for that, it seams to be working, how-ever im getting this error back



A Database Error Occurred

Error Number: 1054

Unknown column 'Array' in 'field list'

INSERT INTO `sales` (`clientid`, `userid`, `date`, `totalprice`, `paymenttype`, `chequeid`, `bank`, `branch`, `prodid`, `adsource`) VALUES ('', '1', '2010-11-22', '248.00', 'Master Card', '', '', '', Array, 'Unknown')

in the same function i am also doing a couple of other inserts too. Should i be doing it diffrently?

this is how my inserts look

Code:
$data = array(
    
            'data' => $this->input->post('data'),
'data1' => $this->input->post('data1'),

            );
        
        $this->my_model->add($data);
#4

[eluser]pickupman[/eluser]
Well, you cant' add a array to the DB, so you will need to figure out what variable you are passing in your insert method of your model. You will need to make sure you are passing the correct field. Beyond that, it's hard to offer much more without seeing the model or the structure of the table.
#5

[eluser]danmontgomery[/eluser]
You can serialize arrays to store them in the DB...




Theme © iAndrew 2016 - Forum software by © MyBB