Abin Jacob Abin Jacob - 6 months ago 11
SQL Question

Codeigniter: how to get data from array and store it in the database

I have an array looks like:

Array
(
[0] => Array
(
[file_name] => Chrysanthemum13.jpg
[file_type] => image/jpeg
[file_path] => D:/wamp/www/multipleImage/upload/
[full_path] => D:/wamp/www/multipleImage/upload/Chrysanthemum13.jpg
[raw_name] => Chrysanthemum13
[orig_name] => Chrysanthemum.jpg
[client_name] => Chrysanthemum.jpg
[file_ext] => .jpg
[file_size] => 858.78
[is_image] => 1
[image_width] => 1024
[image_height] => 768
[image_type] => jpeg
[image_size_str] => width="1024" height="768"
)

[1] => Array
(
[file_name] => Desert6.jpg
[file_type] => image/jpeg
[file_path] => D:/wamp/www/multipleImage/upload/
[full_path] => D:/wamp/www/multipleImage/upload/Desert6.jpg
[raw_name] => Desert6
[orig_name] => Desert.jpg
[client_name] => Desert.jpg
[file_ext] => .jpg
[file_size] => 826.11
[is_image] => 1
[image_width] => 1024
[image_height] => 768
[image_type] => jpeg
[image_size_str] => width="1024" height="768"
)

)


I have to store values of file_name into the database table.
My Model looks:

public function galleryimages($image_data)
{


$data = array(
'image' => $image_data['file_name'],

);

$this->db->insert('gallery', $data);
}


I have to store all values in the file_name into the
image
of the table
gallery
. How can I make it possible? What all modifications are needed in the model.

Answer

You can use insert_batch to insert all rows by one query.

 public function galleryimages($image_data) {
   $data = array_map(
                function($i) { return array('image' => $i['file_name']); },
                $image_data);
   $this->db->insert_batch('gallery', $data);
 }