Я хочу обновить несколько изображений в codeigniter, когда я отправляю форму, а также загружать новые изображения при отправке. Это работает только для загрузки новых нескольких изображений
это мой файл контроллера
публичная функция product_edit ($ product_id) {
$data["data"] = $this->Menu_management_model->getCat();
$this->load->view('header',$data);
$category_id= $this->uri->segment(4);
$viewData['data'] = $this->Menu_management_model->getProduct($id='',$product_id);
$viewData['image'] = $this->Menu_management_model->getProductPhoto($id='',$product_id);
$this->load->view('products/product_edit', $viewData);
$updateData = array(
'product_name' => $this->input->post('product_name'),
'product_description'=> $this->input->post('product_description'),
);
if($this->input->post('update_product')){
$sub_category_id= $this->input->post('sub_category_id');
$files = $_FILES;
if(!empty($files['product_photo']['name'])){
$count = count($_FILES['product_photo']['name']);
for($i=0; $i<$count; $i++){
$_FILES['product_photo']['name']= $files['product_photo']['name'][$i];
$_FILES['product_photo']['tmp_name']= $files['product_photo']['tmp_name'][$i];
$_FILES['product_photo']['size']= $files['product_photo']['size'][$i];
$config['upload_path'] = 'assets/uploads/products/';
$config['allowed_types'] = 'jpg|png|jpeg';
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->upload->do_upload('product_photo');
$fileName = $this->upload->data('file_name');
$images[] = $fileName;
}
$fileName = implode(',',$images);
$update= $this->Menu_management_model->updateProduct($product_id,$updateData,$fileName,$sub_category_id,$category_id);
}
}
это моя модель
открытая функция updateProduct ($ product_id, $ updateData, $ filename, $ sub_category_id, $ category_id) {
$update= $this->db->update('products', $updateData, array('product_id'=>$product_id));
if($filename!='' ){
$filename1 = explode(',',$filename);
foreach($filename1 as $file){
$file_data = array(
'product_photo' => $file,
'product_id' => $product_id,
'sub_category_id'=>$sub_category_id,
'category_id'=>$category_id
);
$update = $this->db->insert('product_images', $file_data);
}
}
return $update?true:false;
}
это моя страница просмотра, поэтому, пожалуйста, проверьте это и помогите мне
<form id="add_produc" method="post" class="addProduct" name="add_produc" action="" enctype="multipart/form-data" >
<table class="table table-bordered">
<tr>
<th class="head" colspan="2">Edit</th>
</tr>
<tr>
<td><label for="Name">Name:</label></td>
<td>
<input type="text" name="product_name" value="<?= $data['product_name']?>" id="product_name" class="form-control" placeholder="Name">
</td>
</tr>
<tr>
<td> <label for="Description">Description:</label> </td>
<td>
<input type="text" name="product_description" value="<?= $data['product_description']?>" id="product_description" class="form-control" placeholder="description">
</td>
</tr>
<tr >
<td><label for="Photo">Photo:</label> </td>
<td>
<div id="addField0">
<input type="file" name="product_photo[]" id="product_photo" class="form-control" accept="image/*"><br>
</div>
<button id="add-more-edit" name="add-more-edit" class="btn btn-primary">Add More</button>
</td>
</tr>
<?php
if(!empty($image)){
foreach ($image as $result){
?>
<tr class='imagelocation<?= $result->product_image_id ?>'>
<td colspan="2" align='center'>
<input type="text" name="product_image_id" value="<?= $result->product_image_id?>" id="product_image_id" class="form-control">
<input type="file" name="image" id="image" class="form-control images<?php echo $result->product_image_id ?>" accept="image/*" onchange="javascript:editimage(<?php echo $result->product_image_id ?>)">
<img class='img-rounded' height="50" width="50" src="<?= base_url('/assets/uploads/products/'.$result->product_photo)?>" style="vertical-align: middle;">
<span style="cursor:pointer;" onclick="javascript:deleteimage(<?php echo $result->product_image_id ?>)">X</span>
</td>
</tr>
<?php
}
}else{
echo '<div class="empty">No Products Found...!</div>';
}
?>
<tr>
<td colspan="2">
<input type="hidden" name="sub_category_id" value="<?= $data['sub_category_id']?>" id="sub_category_name" class="form-control">
<input type="submit" name="update_product" id="update_product" class="btn btn-info btn-small" value="Update">
</td>
</tr>
</table>
</form>