如何从db中删除选定的文件路径,其中“内爆”#9;插入

时间:2016-01-13 09:24:28

标签: php codeigniter codeigniter-3

我是Codeigniter的新手,并设法构建了一个应用程序,我已经使用了'内爆'#39;在单个列中的db表中插入多个文件的方法。

我试图找出如何从此列中删除单个选定文件。我想我应该使用类似于'爆炸'或者我不确定。

列文件显示为:

  

image1.jpg,image2.jpg,image3.jpg,image4.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');

网址显示为:

  

本地主机/仪表板/ 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;
}
}

3 个答案:

答案 0 :(得分:1)

在您发表评论后,我正在为此分享一个基本想法,您可以使用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更新声明:

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

应该怎么办?

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

答案 1 :(得分:0)

试试这个。在控制器中:

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

答案 2 :(得分:0)

控制器

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