簡體   English   中英

Codeigniter:如何從數組獲取數據並將其存儲在數據庫中

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

我有一個數組看起來像:

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"
        )

)

我必須將file_name的值存儲到數據庫表中。 我的模特看起來:

public function galleryimages($image_data)
    {


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

        );

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

我必須將file_name中的所有值存儲到表galleryimage中。 我怎樣才能實現呢? 模型中需要什么所有修改。

您可以使用insert_batch通過一個查詢插入所有行。

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

如果您可以在項目中執行以下操作:

//get the numbers of elements of the array
$array_len = count($image_data);

//create the $data array, which I suppose you want to have a associative  key named 'images' to use it to insert to the database table. 
for($i = 0; $i < $array_len; $i++) {
    $data['images'][$i] = $image_data[$i]['file_name'];
}

//then insert each file name into the db. 
foreach($data['images'] as $image) {
    $this->db->insert('gallery', $image);
}

我假設您發布的var_dump()輸出來自名為$ image_data的數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM