Reputation: 527
I am new to Codeigniter and have managed to build an app where I have used 'implode' method to insert multiple files in db table in single column.
I am trying to figure out how to delete a single selected file from this column. I think I should be using something like 'Explode' or I am not sure.
In column files appear as:
image1.jpg,image2.jpg,image3.jpg,image4.jpg,image5.jpg
So far I have controller:
function property_image_delete() {
$id = $this->uri->segment(3);
$image = $this->uri->segment(4);
$data['images_urls'] = $this->my_listings_model->get_property_all_images_url($id);
$this->my_listings_model->delete_selected_property_image($id, $image, $data);
$data['title'] = 'Image Deleted';
$this->load->view('view');
url appear as:
localhost/dashboard/property_image_delete/1/image.jpg
Model:
function delete_selected_property_image($id, $image, $data) {
$this->db->select('property_images', $image);
$this->db->where('property_ref_id', $id);
$this->db->delete('vbc_property_images');
unlink('uploads/property-images/'.$image);
return TRUE;
}
function get_property_all_images_url($id) {
$this->db->select('property_images');
$this->db->from('vbc_property_images');
$this->db->where('property_ref_id', $id);
$query = $this->db->get();
$query_result = $query->result_array();
if (empty($query_result)) {
return FALSE;
}
elseif (count($query_result) > 1) {
return 0;
}
else{
$rowId = explode(',',$query_result[0]['property_images']);
return $rowId;
}
}
Upvotes: 0
Views: 350
Reputation: 572
Controller
function property_image_delete() {
$id = $this->uri->segment(3);
$image = $this->uri->segment(4);
$data['images_urls'] = $this->my_listings_model->get_property_all_images_url($id);
// $data['images_urls'] // <-- !!! is an array?
$this->my_listings_model->delete_selected_property_image($id, $image, $data['images_urls']);
$data['title'] = 'Image Deleted';
$this->load->view('view');
}
Update your model function!
function delete_selected_property_image($id, $image,$urls) {
$this->db->select('property_images', $image);
$this->db->where('property_ref_id', $id);
$query = $this->db->get('vbc_property_images');
if ($query && $query->num_rows() > 0){
$row = $quer->row();
$images = explode(',',$row->property_images);
if(($key = array_search($image, $images)) !== false) {
unset($images[$key]);
unlink($urls[$image]); // <--- **EDITED**
}
else
return false;
$images = implode(',',images);
$data = array(
'property_images' => $images
);
$this->db->where('property_ref_id', $id);
$this->db->update('vbc_property_images',$data);
return true;
}
return false;
}
Upvotes: 0
Reputation: 16117
After your comments, i am sharing a basic idea for this, you can implement this with your CI Queries.
// a file that you want to delete.
$Delete = "image3.jpg";
// Existing Column Data , that you get from your DATABASE by using SELECT Query
$yourColumnValue = "image1.jpg,image2.jpg,image3.jpg,image4.jpg,image5.jpg";
// explode with , you will get this data in an array
$explodeArr = explode(",", $yourColumnValue);
$updatedColumn = "";
foreach ($explodeArr as $key => $value) {
if($value != $Delete){
$updatedColumn[] = $value; // store data that you dont need to delete
}
}
What is the result of $updatedColumn:
echo "<pre>";
print_r($updatedColumn);
Array
(
[0] => image1.jpg
[1] => image2.jpg
[2] => image4.jpg
[3] => image5.jpg
)
// final updated data with comma seperated.
$newUpdatedData = implode(",", $updatedColumn);
And the result of $newUpdateData should be:
image1.jpg,image2.jpg,image4.jpg,image5.jpg
Now Use CI UPDATE Statement:
$this->db->where('property_ref_id', $id);
$this->db->update(array('vbc_property_images'=>$newUpdatedData));
What should be happened?
After this you don't need to DELETE
any record, you can just update the existing record by using UPDATE
Statement.
Upvotes: 1
Reputation: 1461
Try this. In the controller:
function property_image_delete($id, $image) {
$this->my_listings_model->delete_selected_property_image($id, $image);
$data['title'] = 'Image Deleted';
$this->load->view('view');
}
Then in your model:
function delete_selected_property_image($id, $image) {
//get the string of images names
$this->db->select('property_images');
$this->db->where('property_ref_id', $id);
$query = $this->db->get('vbc_property_images');
$result = $query->result();
//turn the string into an array with explode
$images_array = explode(',', $result->property_images);
//find and remove the unwanted image name from the array
if(($key = array_search($image, $images_array )) !== false) {
unset($images_array [$key]);
}
//update the database with the imploded new array
$this->db->where('property_ref_id', $id);
$this->db->update(array('vbc_property_images'=>implode(',', $images_array)));
//delete the file
unlink("uploads/property-images/{$id}/{$image}");
return TRUE;
}
Upvotes: 0