1

saving post value to an array.

$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 Structure.

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
        )

)

Insert array values to database table.

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

I got this following error and the error is :

the values are returning as array.

INSERT INTO abc_invoiceitems (invoice_id, order_id, invoiceitems_servicetype, invoiceitems_quantity, invoiceitems_unitprice, invoiceitems_notes) VALUES ('21', '1', Array, Array, Array, Array)

3
  • 1
    So what did you expect? Have you ever worked with MySQL? It's a relational database, not document-oriented. If you want to save multidimensional arrays to a database MongoDB is the way to go, or use some ORM like Doctrine. Commented Feb 5, 2013 at 9:52
  • You could of course serialize the array. Commented Feb 5, 2013 at 9:52
  • I want to add it to MySQL. @vlakarados Commented Feb 5, 2013 at 9:53

2 Answers 2

4

Fetch post values:

$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');

Loop through Key values:

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);
  }
Sign up to request clarification or add additional context in comments.

Comments

1

How about turning the arrays into serialized strings?

$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);

When you want to pull the data back out of the database, you can unserialize the strings.

6 Comments

Interesting solution, Jezen! Want to mention works only when you don't need to do JOIN's on data in these arrays.
But i need each array values save to separate rows.
You're right. You would need to distribute the datasets into relative tables.
@DOCTYPE I think you're going to need to revise your database structure.
Can we do it with Foreach Loop.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.