如何从数据库中删除选定的文件路径,其中插入时使用了 'implode' 方法
How to delete selected file path from from db where 'implode' method was used when inserted
我是 Codeigniter 的新手,我已经成功地构建了一个应用程序,我使用 'implode' 方法在 db table 的单列中插入多个文件。
我想弄清楚如何从此列中删除单个选定文件。我想我应该使用 'Explode' 之类的东西,否则我不确定。
列文件显示为:
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');
url 显示为:
localhost/dashboard/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;
}
}
试试这个。在控制器中:
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;
}
在你发表评论后,我将分享一个基本的想法,你可以用你的 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
语句更新现有记录。
我是 Codeigniter 的新手,我已经成功地构建了一个应用程序,我使用 'implode' 方法在 db table 的单列中插入多个文件。
我想弄清楚如何从此列中删除单个选定文件。我想我应该使用 'Explode' 之类的东西,否则我不确定。
列文件显示为:
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');
url 显示为:
localhost/dashboard/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;
}
}
试试这个。在控制器中:
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;
}
在你发表评论后,我将分享一个基本的想法,你可以用你的 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
语句更新现有记录。