1
我有一個插入最後一個id的問題。當發送圖像數據到模型,我已經能夠。但是當他們在這裏與insert_id()我合併時遇到麻煩。插入許多圖像和其他數據到數據庫中,並得到插入最後一個id與codeigniter
我的觀點:
<?php echo form_open_multipart('produk_adm/proses_tambah_produk');?>
<div class="col-md-6 input group">
<label>Product Name :</label>
<input type="text" name="name" class="form-control" id="name" required>
<br>
</div>
<div class="col-md-6 input group">
<label>Image :</label> <i style="color:red;">*max 1 MB</i>
<input type="file" name="image" class="form-control" id="gambar">
<br>
</div>
<div class="col-md-6 input group">
<label>Point :</label>
<input type="number" name="point" class="form-control" id="point" value="0" required>
<br>
</div>
<div class="col-md-6 input group">
<label>Additional images :</label> <i style="color:red;">*max 1 MB</i>
<input type="file" name="add[]" class="form-control" id="gambar">
<br>
</div>
<div class="col-md-6 input group">
<label>Additional images :</label> <i style="color:red;">*max 1 MB</i>
<input type="file" name="add[]" class="form-control" id="gambar">
<br>
</div>
<div class="col-md-6 input group">
<label>Additional images :</label> <i style="color:red;">*max 1 MB</i>
<input type="file" name="add[]" class="form-control" id="gambar">
<br>
</div>
<?php echo form_close();?>
我的控制器:
function proses_tambah_produk(){
$config['upload_path'] = 'assets/img/produk';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = 1000;
$config['overwrite'] = TRUE;
//$config['max_width'] = 1024;
//$config['max_height'] = 768;
$this->load->library('upload', $config);
$files = $_FILES;
$count = count($_FILES['add']['name']);
for($i=0; $i<$count; $i++)
{
$_FILES['add']['name']= $files['add']['name'][$i];
$_FILES['add']['type']= $files['add']['type'][$i];
$_FILES['add']['tmp_name']= $files['add']['tmp_name'][$i];
$_FILES['add']['error']= $files['add']['error'][$i];
$_FILES['add']['size']= $files['add']['size'][$i];
$this->upload->do_upload('add');
$upload_data = $this->upload->data();
$name_array[] = $upload_data['file_name'];
$fileName = $upload_data['file_name'];
$images[] = $fileName;
$result = $files['add']['name'][$i];
//print_r($result);
}
$target = $this->input->post('nama');
if($this->input->post()){
$this->upload->do_upload('image');
$imagex = $_FILES['image']['name'];
$data = $this->input->post();
$data['nama_depan'] = $this->data['nama_depan'];
$this->produk_adm->add($data, $imagex, $result);
log_helper("product", "adding Product ".$target."");
redirect('trueaccon2194/produk');
}else{
log_helper("product", "failed");
redirect('trueaccon2194/produk/tambah_produk');
}
}
和我的模型:
function add($data, $imagex, $result){
$data_produk = array(
'nama_produk' => $data['nama'],
'slug' => $data['slug'],
'milik' => $data['miliknya'],
'jenis' => $data['jenisnya'],
'artikel' => $data['artikel'],
'merk' => $data['merknya'],
'keterangan' => $data['editor1'],
'tags' => $data['tags'],
'kategori' => $data['kategori'],
'parent' => $data['parent'],
'harga_retail' => $data['retail'] - ($data['diskon']/100 * $data['retail']),
'harga_odv' => $data['odv'],
'harga_net' => $data['retail'] - ($data['diskon']/100 * $data['retail']),
'diskon' => $data['diskon'],
'stok' => $data['stok'],
'berat' => $data['berat'],
'gambar' => $imagex,
'point' => $data['point'],
'status' => $data['aktif'],
'dibuat' => $data['nama_depan'],
'tgl_dibuat' => date('Y-m-d H:i:s'),
);
$this->db->insert('produk', $data_produk);
// MY PROBLEM HERE //
$last_insert_id = $this->db->insert_id();
$count = count($result);
for($i=0; $i<$count; $i++){
$results = array(
'id_product' => $last_insert_id,
'image_product' => $result[$i],
);
$this->db->insert('product_image_additional', $results);return $this->db->insert_id();
//print_r($results);
}
}
如何獲得多個圖像名稱並投入陣列和INSERT_ID()?