如何从数据库中删除选定的文件路径,其中';内爆';插入时使用方法


How to delete selected file path from from db where 'implode' method was used when inserted

我是Codeigniter的新手,并且已经成功地构建了一个应用程序,在该应用程序中,我使用"内爆"方法在单列中的数据库表中插入多个文件。

我正试图弄清楚如何从这个专栏中删除一个选定的文件。我想我应该使用类似"爆炸"的东西,否则我不确定。

在列中,文件显示为:

image_1.jpg、image_2.jpg、image_3.jpg、image_4.jpg、image5.jpg

到目前为止,我有控制器:

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显示为:

localhost/ddashboard/property_image_delete/1/image.jpg

型号:

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;
}
}

在您发表评论后,我将分享这方面的基本想法,您可以通过CI查询实现这一点。

// 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
    }
}

$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);

$newUpdateData的结果应该是:

image1.jpg,image2.jpg,image4.jpg,image5.jpg

现在使用CI UPDATE语句:

$this->db->where('property_ref_id', $id);
$this->db->update(array('vbc_property_images'=>$newUpdatedData));

该怎么办

在此之后,您不需要DELETE任何记录,只需使用UPDATE语句更新现有记录即可。

试试这个。在控制器中:

function property_image_delete($id, $image) {    
    $this->my_listings_model->delete_selected_property_image($id, $image);
    $data['title'] = 'Image Deleted';
    $this->load->view('view');
}

然后在您的模型中:

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;
}

控制器

   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');
     }

更新您的模型功能!

 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;
    }