Codeigniter:如何从数组中获取数据并将其存储在数据库中

时间:2016-05-27 11:04:55

标签: php mysql codeigniter

我有一个数组看起来像:

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 中的所有值存储到表image的{​​{1}}中。我怎样才能成功呢?模型中需要进行哪些修改。

2 个答案:

答案 0 :(得分:1)

您可以使用insert_batch通过一个查询插入所有行。

var container   = $(".container"),
    background  = $(".background");

// ...

// Inside an each() method – check if is container OR background
if (this.is(container, background)) { // Not working
    // do something
}

// I know I could do it like this ...
if (this.is(container) || this.is(background)) {
    // do something
}

// ... but I want it shorter

答案 1 :(得分:0)

如果你可以在你的项目中做这样的事情:

//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的数组。