2016-05-27 2 views
0

でそれを格納するために:私は、データベースのテーブルにfile_nameにの値を格納する必要が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" 
     ) 

) 

私のモデルになります。

public function galleryimages($image_data) 
    { 


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

     ); 

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

私はテーブルgalleryimagefile_nameに内のすべての値を格納する必要があります。どうすればそれを可能にすることができますか?モデルに必要なすべての変更。

+0

何が欲しいですか? 'image'の2つの値に対して2つのエントリ、'、 'で区切られた2つの値を持つ1つのエントリ? – Poonam

+0

実際に私は複数の画像をアップロードしようとしていますので、file_nameのすべての値を保存する必要があります。行ごとに行を –

答えて

1

insert_batchを使用すると、すべての行を1つのクエリで挿入できます。

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

ありがとう、その仕事に感謝します。 –

+0

おかげで、ありがとう。 – Tpojka

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という名前の配列から来ました。

関連する問題