1

投稿値を配列に保存します。

$data2 = array(
  'invoice_id' =>($id),
  'order_id' => $this->input->post('order_id'),
  'invoiceitems_servicetype' =>$this->input->post('type'),
  'invoiceitems_quantity' => $this->input->post('quantity'),
  'invoiceitems_unitprice' => $this->input->post('rate'),
  'invoiceitems_notes' => $this->input->post('description')
      );

 $this->db->insert('abc_invoiceitems', $data2);

print_r($data2);

配列構造。

Array
(
    [invoice_id] => 21
    [order_id] => 1
    [invoiceitems_servicetype] => Array
        (
            [0] => Order
            [1] => Miscellaneous
            [2] => Order
        )

    [invoiceitems_quantity] => Array
        (
            [0] => 5
            [1] => 64
            [2] => 88
        )

    [invoiceitems_unitprice] => Array
        (
            [0] =>  5
            [1] =>  6
            [2] =>  8
        )

    [invoiceitems_notes] => Array
        (
            [0] => test Data1
            [1] => test Data2
            [2] => test Data3
        )

)

配列値をデータベース テーブルに挿入します。

TABLE NAME --- abc_invoiceitems 
 +------------+----------+--------------------------+-----------------------+------------------------+--------------------+
 | invoice_id | order_id | invoiceitems_servicetype | invoiceitems_quantity | invoiceitems_unitprice | invoiceitems_notes |
 +------------+----------+--------------------------+-----------------------+------------------------+--------------------+

次のエラーが発生しました。エラーは次のとおりです。

値は配列として返されます。

INSERT INTO abc_invoiceitems( invoice_id, order_id, invoiceitems_servicetype, invoiceitems_quantity, invoiceitems_unitprice, invoiceitems_notes) 値 ('21', '1', 配列, 配列, 配列, 配列)

4

2 に答える 2

4

投稿値を取得します。

$temp =count($this->input->post('type'));//counting number of row's
$type= $this->input->post('type');
$quantity = $this->input->post('quantity');
$rate = $this->input->post('rate');
$description = $this->input->post('description');

キー値をループします。

for($i=0; $i<$temp;$i++){
  $data2 = array(
  'invoice_id' =>($id),
  'order_id' => $this->input->post('order_id'),
  'invoiceitems_servicetype' =>$type[$i],
  'invoiceitems_quantity' => $quantity[$i],
  'invoiceitems_unitprice' => $rate[$i],
  'invoiceitems_notes' => $description[$i]
      );
   $this->db->insert('abc_invoiceitems', $data2);
  }
于 2013-02-05T10:23:38.547 に答える
1

配列をシリアル化された文字列に変換するのはどうですか?

$data2 = array(
    'invoice_id' =>($id),
    'order_id' => $this->input->post('order_id'),
    'invoiceitems_servicetype' => serialize($this->input->post('type')),
    'invoiceitems_quantity' => serialize($this->input->post('quantity')),
    'invoiceitems_unitprice' => serialize($this->input->post('rate')),
    'invoiceitems_notes' => serialize($this->input->post('description'))
);

$this->db->insert('abc_invoiceitems', $data2);

データベースからデータを取り出したい場合は、文字列のシリアル化を解除できます。

于 2013-02-05T09:56:22.283 に答える